/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package schema

import (
	"os"
	"path/filepath"

	pb "github.com/go-chassis/cari/discovery"
)

type Config struct {
	SaveDir string
}

type Writer interface {
	Write([]*pb.Schema) error
}

func NewSchemaWriter(cfg Config) Writer {
	switch {
	case len(cfg.SaveDir) == 0:
		return &StdoutWriter{}
	default:
		return &FileWriter{cfg.SaveDir}
	}
}

type StdoutWriter struct {
}

func (w *StdoutWriter) Write(schemas []*pb.Schema) error {
	for _, schema := range schemas {
		_, err := os.Stdout.WriteString(schema.Schema)
		if err != nil {
			return err
		}
	}
	return nil
}

type FileWriter struct {
	Dir string
}

func (w *FileWriter) Write(schemas []*pb.Schema) error {
	err := os.MkdirAll(w.Dir, 0750)
	if err != nil {
		return err
	}
	for _, schemas := range schemas {
		file, err := os.OpenFile(filepath.Join(w.Dir, schemas.SchemaId+".yaml"),
			os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0640)
		if err != nil {
			return err
		}
		_, err = file.WriteString(schemas.Schema)
		if err != nil {
			file.Close()
			return err
		}
		file.Close()
	}
	return nil
}
