/*
 * Copyright 2021 CloudWeGo Authors
 *
 * Licensed 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 utils

import (
	"io"
	"os"
	"time"

	"gopkg.in/yaml.v3"

	"github.com/cloudwego/kitex/internal/configutil"
)

var (
	_ configutil.Config         = &YamlConfig{}
	_ configutil.RichTypeConfig = &YamlConfig{}
)

// YamlConfig contains configurations from yaml file.
type YamlConfig struct {
	configutil.RichTypeConfig
	data map[interface{}]interface{}
}

// ReadYamlConfigFile creates a YamlConfig from the file.
func ReadYamlConfigFile(yamlFile string) (*YamlConfig, error) {
	fd, err := os.Open(yamlFile)
	if err != nil {
		return nil, err
	}
	defer fd.Close()

	b, err := io.ReadAll(fd)
	if err != nil {
		return nil, err
	}

	data := make(map[interface{}]interface{})
	err = yaml.Unmarshal(b, &data)
	if err != nil {
		return nil, err
	}

	y := &YamlConfig{data: data}
	y.RichTypeConfig = configutil.NewRichTypeConfig(y)
	return y, nil
}

// Get implements the configutil.Config interface.
func (yc *YamlConfig) Get(key string) (interface{}, bool) {
	val, ok := yc.data[key]
	return val, ok
}

// GetDuration implements the configutil.RichTypeConfig interface.
// It converts string item to time.Duration.
func (yc *YamlConfig) GetDuration(key string) (time.Duration, bool) {
	if s, ok := yc.data[key].(string); ok {
		val, err := time.ParseDuration(s)
		if err != nil {
			return 0, false
		}
		return val, true
	}
	return 0, false
}
