package config

import (
	"fmt"
	"fonchain-fiee/pkg/common"
	"github.com/spf13/viper"
	"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
	//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

	//百度地图的ak
	BaiduMapAK string

	// es
	ElasticHost    string
	SendIndexesUrl string
	ChatGptHost    string

	ApiHost string

	Level      string
	Filename   string
	MaxSize    string
	MaxAge     string
	MaxBackups string

	YearGiftCheckHost string
	IsOut             bool
	OutUrl            string
	Aliyun            struct {
		AccessKeyId     string
		AccessKeySecret string
	}
	SendTime      int
	XMChannelId   string
	OPChannelId   string
	OPNotifyLevel int
	Route         string

	PdfHost string

	DefaultSignNo uint64

	// 审批 es index
	ApprovalIndex string

	Webhookkey string
)

var ConfigData = new(AppConfig)

type AppConfig struct {
	Oss struct {
		AccessKeyId     string
		AccessKeySecret string
		Endpoint        string
		BucketName      string
		BaseDir         string
		CdnHost         string
	}
	Rabbitmq struct {
		User           string
		Password       string
		Host           string
		Port           int
		Vhost          string
		VhostArtwork   string
		VhostCopyright string
	}
}

func GetOptions() {
	iniConf, err := common.GetConf()
	if err != nil {
		panic("iniConf error")
	}
	file, err := ini.Load(iniConf)
	if err != nil {
		fmt.Println("服务器配置文件读取错误,请检查文件路径:", err)
	}
	_ = os.MkdirAll("runtime", os.ModePerm)
	LoadServer(file)
	_ = Viper(iniConf)
}

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()
	IsOut, _ = file.Section("service").Key("IsOut").Bool()
	OutUrl = file.Section("service").Key("OutUrl").String()

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

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

	//TODO
	//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()

	//BosUrl = file.Section("bos").Key("BosUrl").String()
	//BosHttp = file.Section("bos").Key("BosHttp").String()
	//BosDomain = file.Section("bos").Key("BosDomain").String()

	//TODO

	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()
	BaiduMapAK = file.Section("service").Key("BaiduMapAK").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()

	Aliyun.AccessKeySecret = file.Section("aliyun").Key("accessKeySecret").String()

	//发送次数
	SendTime, _ = file.Section("send").Key("SendTime").Int()
	OPNotifyLevel, _ = file.Section("send").Key("OPNotifyLevel").Int()
	XMChannelId = file.Section("send").Key("XMChannelId").String()
	OPChannelId = file.Section("send").Key("OPChannelId").String()
	PdfHost = file.Section("pdf").Key("Host").String()
	DefaultSignNo, _ = file.Section("msg").Key("defaultSignNo").Uint64()
	Route = file.Section("send").Key("Route").String()

	// 审批 es index
	ApprovalIndex = file.Section("es").Key("ApprovalIndex").String()

	Webhookkey = file.Section("stripe").Key("Webhookkey").String()
}

func Viper(iniConf string) (err error) {
	viper.SetConfigFile(iniConf)
	err = viper.ReadInConfig()
	if err != nil {
		panic("viper.ReadInConfig failed" + err.Error())
		return
	}
	if err = viper.Unmarshal(ConfigData); err != nil {
		panic("viper.Unmarshal failed" + err.Error())
		return
	}
	return
}