package config

import (
	"errors"
	"fmt"
	"fonchain-fiee/pkg/common"
	"github.com/BurntSushi/toml"
	"gopkg.in/ini.v1"
	"os"
	"strconv"
)

var (
	AppMode          string
	HttpPort         string
	Key              string
	Cert             string
	IsHttps          bool
	RunHttps         bool
	Domain           string
	OssDomain        string
	ServerDM         string
	Env              string
	DriverUrl        string
	AppointUrl       string
	MaterialHost     string
	BosAk            string
	BosSk            string
	Endpoint         string
	BosBucketName    string
	BosBaseDir       string
	BosUrl           string
	BosHttp          string
	BosDomain        string
	ProjectMapDir    string
	JaegerHost       string
	JaegerOpen       bool
	Cron             bool
	DingAccessToken  string
	DingUrl          string
	HolidayID        string
	HolidaySecret    string
	HolidaySingleUrl string
	HolidayMultiUrl  string

	ErpAk string
	ErpSk string

	// rabbitmq
	RabbitmqUser         string
	RabbitmqPassword     string
	RabbitmqHost         string
	RabbitmqPort         int
	RabbitmqVhost        string
	RabbitmqVhostArtwork string

	RedisDB   int
	RedisAddr string
	RedisPw   string

	//shop配置
	ShopHost string
	//ShopDetail string

	// 地图
	MapAppCode string
	// es
	ElasticHost    string
	SendIndexesUrl string
	ChatGptHost    string

	ApiHost string

	Level      string
	Filename   string
	MaxSize    string
	MaxAge     string
	MaxBackups string

	YearGiftCheckHost string
	Aliyun            struct {
		AccessKeyId     string
		AccessKeySecret string
	}
	Blockchain struct {
		Key    string
		PubKey string
	}
)

/********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 System struct {
	Mode        string
	AppMode     string
	Version     string
	HttpPort    string
	Host        string
	RedirectUri string
	Domain      string
	ErpHost     string
}
type Oss struct {
	AccessKeyId     string
	AccessKeySecret string
	Endpoint        string
	BucketName      string
	BaseDir         string
	CdnHost         string
}
type Mobile struct {
	SK          string
	AK          string
	URL         string
	DingDingKey string
}
type Ai struct {
	Host     string
	TelNum   string
	Password string
}

type Config struct {
	Mysql  Mysql  `toml:"mysql"`
	Redis  Redis  `toml:"redis"`
	System System `toml:"system"`
	Mobile Mobile `toml:"mobile"`
	Ai     Ai     `toml:"ai"`
	Oss    Oss    `toml:"oss"`
}

/********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
}
func GetOptions1() {
	iniConf, err := common.GetConf()
	if err != nil {
		panic("iniConf error")
	}
	file, err := ini.Load(iniConf)
	if err != nil {
		fmt.Println("服务器配置文件读取错误，请检查文件路径:", err)
	}
	LoadServer(file)
}
func LoadServer(file *ini.File) {
	AppMode = file.Section("service").Key("AppMode").String()
	IsHttps, _ = file.Section("service").Key("IsHTTPS").Bool()
	RunHttps, _ = file.Section("service").Key("RunHTTPS").Bool()
	Key = file.Section("service").Key("Key").String()
	Cert = file.Section("service").Key("Cert").String()
	HttpPort = file.Section("service").Key("HttpPort").String()
	Domain = file.Section("service").Key("Domain").String()
	OssDomain = file.Section("service").Key("OssDomain").String()
	ServerDM = file.Section("service").Key("ServerDM").String()
	DriverUrl = file.Section("service").Key("DriverUrl").String()
	Env = file.Section("service").Key("Env").String()
	JaegerHost = file.Section("service").Key("JaegerHost").String()
	JaegerOpen, _ = file.Section("service").Key("JaegerOpen").Bool()
	Cron, _ = file.Section("service").Key("Cron").Bool()
	YearGiftCheckHost = file.Section("service").Key("YearGiftCheckHost").String()

	ProjectMapDir = file.Section("file").Key("ProjectMapDir").String()

	AppointUrl = file.Section("service").Key("AppointUrl").String()
	MaterialHost = file.Section("service").Key("MaterialHost").String()

	BosAk = file.Section("bos").Key("Ak").String()
	BosSk = file.Section("bos").Key("Sk").String()
	BosBucketName = file.Section("bos").Key("BucketName").String()
	BosBaseDir = file.Section("bos").Key("BosBaseDir").String()
	Endpoint = file.Section("bos").Key("Endpoint").String()
	BosUrl = file.Section("bos").Key("BosUrl").String()
	BosHttp = file.Section("bos").Key("BosHttp").String()
	BosDomain = file.Section("bos").Key("BosDomain").String()
	DingUrl = file.Section("ding").Key("Url").String()
	DingAccessToken = file.Section("ding").Key("AccessToken").String()

	//加载获取万年历配置
	HolidayID = file.Section("holiday").Key("ID").String()
	HolidaySecret = file.Section("holiday").Key("Secret").String()
	HolidaySingleUrl = file.Section("holiday").Key("SingleUrl").String()
	HolidayMultiUrl = file.Section("holiday").Key("MultiUrl").String()

	// 加载 rabbitmq 配置
	RabbitmqUser = file.Section("rabbitmq").Key("User").String()
	RabbitmqPassword = file.Section("rabbitmq").Key("Password").String()
	RabbitmqHost = file.Section("rabbitmq").Key("Host").String()
	RabbitmqPort, _ = file.Section("rabbitmq").Key("Port").Int()
	RabbitmqVhost = file.Section("rabbitmq").Key("Vhost").String()
	RabbitmqVhostArtwork = file.Section("rabbitmq").Key("VhostArtwork").String()

	//加载 redis
	dbStr := file.Section("redis").Key("RedisDBNAme").String()
	RedisDB, _ = strconv.Atoi(dbStr)
	RedisAddr = file.Section("redis").Key("RedisAddr").String()
	RedisPw = file.Section("redis").Key("RedisPW").String()

	ShopHost = file.Section("shop").Key("ShopHost").String()
	//ShopDetail = file.Section("shop").Key("ShopDetail").String()

	// 地图
	MapAppCode = file.Section("service").Key("MapAppCode").String()

	// 地图
	ElasticHost = file.Section("es").Key("ElasticHost").String()
	SendIndexesUrl = file.Section("api").Key("SendIndexesUrl").String()
	ChatGptHost = file.Section("api").Key("chatGptHost").String()
	ErpAk = file.Section("api").Key("erpak").String()
	ErpSk = file.Section("api").Key("erpsk").String()
	ApiHost = file.Section("service").Key("ApiHost").String()

	// zap
	Level = file.Section("zap").Key("Level").String()
	Filename = file.Section("zap").Key("Filename").String()
	MaxSize = file.Section("zap").Key("MaxSize").String()
	MaxAge = file.Section("zap").Key("MaxAge").String()
	MaxBackups = file.Section("zap").Key("MaxBackups").String()
	Aliyun.AccessKeyId = file.Section("aliyun").Key("accessKeyId").String()
	Aliyun.AccessKeySecret = file.Section("aliyun").Key("accessKeySecret").String()
	Blockchain.PubKey = file.Section("blockchain").Key("PubKey").String()
	Blockchain.Key = file.Section("blockchain").Key("Key").String()
}
