package config

import (
	"errors"
	"fmt"
	"github.com/BurntSushi/toml"
	"os"
)

/********start-配置信息*********/

//Baiduco 百度上链
type Mysql struct {
	Db         string
	DbHost     string
	DbPort     string
	DbUser     string
	DbPassWord string
	DbName     string
}

type Redis struct {
	RedisDB     string
	RedisAddr   string
	RedisPW     string
	RedisDBNAme string
}

type Chain struct {
	IP                   string
	AdminMnemonicWords   string `toml:"MnemonicWords"`
	AdminContractAccount string `toml:"ContractAccount"`
	ContractName         string
	ContractType         string
}
type System struct {
	Mode    string
	Version string
	Nacos   bool
}

type Mobile struct {
	SK          string
	AK          string
	URL         string
	DingDingKey string
}
type Mail struct {
	IsProd   bool
	Username string
	Password string
	Domain   string
}

type Config struct {
	Mysql    Mysql    `toml:"mysql"`
	Redis    Redis    `toml:"redis"`
	Chain    Chain    `toml:"chain"`
	System   System   `toml:"system"`
	Mobile   Mobile   `toml:"mobile"`
	Mail     Mail     `toml:"mail"`
	AlMobile AlMobile `toml:"almobile"`
}
type AlMobile struct {
	AS  string
	AK  string
	URL string
}

/********start-配置信息*********/

var AppConfig *Config

func newConfig() *Config {
	return new(Config)
}

func LoadEnv(path string) (*Config, error) {
	_, err := os.Stat(path)
	if err != nil {
		return nil, err
	}

	AppConfig = newConfig()
	if _, err := toml.DecodeFile(path, AppConfig); err != nil {
		return nil, err
	}

	return AppConfig, nil
}

func LoadEnvFromFileInfo(data string) (*Config, error) {

	fmt.Println(data)
	if data == "" {
		return nil, errors.New("nacos 配置文件为空")
	}

	AppConfig = newConfig()
	if _, err := toml.Decode(data, AppConfig); err != nil {
		return nil, err
	}

	return AppConfig, nil
}
