package configs

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/spf13/cast"
	"github.com/spf13/viper"

	"os"
	"xorm.io/xorm"
)

var (
	Server           ServerConfiguration
	Manager          ServerManager
	ARKJsonFilePath  string
	ARKBackupDir     string
	ARKAPiZipPath    string
	ArkApiInstallUrl string
	Engine           *xorm.Engine
	//RedisDb *redis.Client
)

type ServerConfiguration struct {
	Port string `mapstructure:"port"`
	Host string `mapstructure:"host"`
}
type ArkShopConfiguration struct {
	ArkShopConfigUrl string `json:"arkShopConfigUrl"` //arkshop 配置文件位置
}
type ServerManager struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

func init() {
	LoadServerConfiguration()
	ConfigInit()
}

func LoadServerConfiguration() {
	//获取项目的执行路径
	path, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	config := viper.New()

	config.AddConfigPath(path + "/configs") //设置读取的文件路径
	config.SetConfigName("config")          //设置读取的文件名
	config.SetConfigType("yaml")            //设置文件的类型
	if err := config.ReadInConfig(); err != nil {
		panic(err)
	}
	portString := config.Get("server.port")
	if cast.ToString(portString) == "" {
		portString = "9777"
	}
	host := config.Get("server.host")

	Server = ServerConfiguration{
		Port: cast.ToString(portString),
		Host: cast.ToString(host),
	}
	//用户账号密码获取
	username := config.Get("server.manager.username")
	password := config.Get("server.manager.password")
	Manager = ServerManager{
		Username: cast.ToString(username),
		Password: cast.ToString(password),
	}

	ARKJsonFilePath = cast.ToString(config.Get("server.file.ark-file"))
	ARKBackupDir = cast.ToString(config.Get("server.file.ark-backup-dir"))
	ARKAPiZipPath = cast.ToString(config.Get("server.file.ark-api"))
	ArkApiInstallUrl = cast.ToString(config.Get("server.file.ark-api-install-url"))

}

func ConfigInit() {
	//获取项目的执行路径
	path, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	config := viper.New()

	config.AddConfigPath(path + "/configs") //设置读取的文件路径
	config.SetConfigName("config")          //设置读取的文件名
	config.SetConfigType("yaml")            //设置文件的类型
	//尝试进行配置读取
	if err := config.ReadInConfig(); err != nil {
		panic(err)
	}
	fmt.Println(fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8", config.Get("mysql.user"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database")))
	Engine, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8", config.Get("mysql.user"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database")))
	Engine.Ping() //连接测试
	//TODO  显示sql
	Engine.ShowSQL(true)
	Engine.Logger().ShowSQL(true)
	//RedisDb = redis.NewClient(&redis.Options{
	//	Addr:     fmt.Sprintf("%s:%s", config.Get("redis.host"), config.Get("redis.port")), // Redis地址
	//	Password: "",                                                                       // Redis密码，如果没有则为空字符串
	//	DB:       0,                                                                        // 使用默认DB
	//})
	////开启debug模式
	//
	//Config = config
	fmt.Println("xorm 数据库orm框架初始化成功")
	//PayConfigInit()
}
