package config

import (
	"log"

	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

func InitConfig(configName, configType, configPath string) {
	viper.Reset()
	viper.SetConfigName(configName)
	viper.SetConfigType(configType)
	viper.AddConfigPath(configPath)
	viper.WatchConfig()
	viper.OnConfigChange(func(in fsnotify.Event) {
		log.Println("config.yml changed ", in.Name)
	})

	err := viper.ReadInConfig()
	if err != nil {
		log.Fatal("InitConfig failed ", err)
	}
}

func UnmarshalKey(key string, val interface{}) error {
	return viper.UnmarshalKey(key, val)
}

func Get(key string) interface{} {
	return viper.Get(key)
}

func GetString(key string) string {
	return viper.GetString(key)
}

func GetStringSclice(key string) []string {
	return viper.GetStringSlice(key)
}

func GetBool(key string) bool {
	return viper.GetBool(key)
}
