package config

import (
	"gopkg.in/yaml.v2"
	"os"
	"sync"
)

var (
	instance *config
	once     sync.Once
)

func init() {
	once.Do(func() {
		var conf config
		path, _ := os.Getwd()
		filePath := path + "/config/config.yaml"
		if f, err := os.Open(filePath); err != nil {
			panic(err)
		} else {
			err := yaml.NewDecoder(f).Decode(&conf)
			if err != nil {
				panic(err)
			}
		}
		instance = &conf
	})
}

// 获取配置文档实例
func Instance() *config {
	return instance
}

type config struct {
	Server      server      `yaml:"server"`
	Database    database    `yaml:"database"`
	Redis       redis       `yaml:"redis"`
	Logger      logger      `yaml:"logger"`
	Minio       minio       `yaml:"minio"`
	Foreign     foreign     `yaml:"foreign"`
	MonitNotice monitNotice `yaml:"monit-notice"`
	Es          es          `yaml:"es"`
}

type server struct {
	Address     string `yaml:"address"`
	CodeEnabled bool   `yaml:"code_enabled"`
	TokenSign   string `yaml:"token_sign"`
}

type database struct {
	Host     string `yaml:"host"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`
	Port     string `yaml:"port"`
	Name     string `yaml:"name"`
	Timezone string `yaml:"timezone"`
}

type redis struct {
	Host     string `yaml:"host"`
	Password string `yaml:"password"`
}

type logger struct {
	Switch    int    `yaml:"switch"`
	Path      string `yaml:"path"`
	Name      string `yaml:"name"`
	Level     string `yaml:"level"`
	DbShowLog int    `yaml:"db_show_log"`
}

type minio struct {
	Endpoint  string `yaml:"endpoint"`
	Link      string `yaml:"link"`
	AccessKey string `yaml:"access_key"`
	SecretKey string `yaml:"secret_key"`
}

type foreign struct {
	IotEdgeUrl        string `yaml:"iot_edge_url"`
	IpCastEdgeUrl     string `yaml:"ip_cast_edge_url"`
	SecurityRewindUrl string `yaml:"security_rewind_url"`
}

type email struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	UserName string `yaml:"user-name"`
	PassWord string `yaml:"pass-word"`
}
type tencent struct {
	AppID             string `yaml:"app_id"`
	SecretID          string `yaml:"secret_id"`
	SecretKey         string `yaml:"secret_key"`
	SmsKey            string `yaml:"sms_key"`
	WarnSmsTemplateID string `yaml:"warn_sms_template_id"`
}
type sms struct {
	Tencent tencent `yaml:"tencent"`
}
type monitNotice struct {
	Email email `yaml:"email"`
	Sms   sms   `yaml:"sms"`
}
type es struct {
	URL string `yaml:"es_url"`
}
