package conf

import (
	"encoding/xml"
	"io/ioutil"
	_ "strings"

	"common/logger"
)

type MysqlConf struct {
	Ip           string `xml:",attr"`
	Port         int    `xml:",attr"`
	User         string `xml:",attr"`
	Pwd          string `xml:",attr"`
	Database     string `xml:",attr"`
	LoadInterval int    `xml:",attr"`
}

type ListenAddr struct {
	Host       string `xml:",attr"`
	Test       int    `xml:",attr"`
	SourceType uint32 `xml:",attr"`
}

type ListenRpcAddr struct {
	Host string `xml:",attr"`
}

type ZooKeeper struct {
	Host string `xml:",attr"`
}

type ClientVersion struct {
	Lastest string `xml:",attr"`
}

type Alliance struct {
	ClubNumMax int `xml:",attr"`
}

type Jackpot struct {
	Award2ClubPercent int `xml:",attr"`
}

type RedisConf struct {
	Host     string `xml:",attr"`
	Db       uint32 `xml:",attr"`
	Password string `xml:",attr"`
}

type TexasConf struct {
	GameDB                  MysqlConf           `xml:"GameDB"`
	ListenAddrConf          ListenAddr          `xml:"ListenAddr"`
	ListenRpcAddrConf       ListenRpcAddr       `xml:"ListenRpcAddr"`
	ZooKeeperConf           ZooKeeper           `xml:"ZooKeeper"`
	VersionConf             ClientVersion       `xml:"ClientVersion"`
	BlindConf               Blinds              `xml:"Blinds"`
	HandlevelConf           Handlevels          `xml:"Handlevels"`
	AllianceConf            Alliance            `xml:"Alliance"`
	JackpotConf             Jackpot             `xml:"Jackpot"`
	EventReportHttpAddrConf EventReportHttpAddr `xml:"EventReportHttpAddr"`
	ServerHttpAddr          ServerHttpAddrConf  `xml:"ServerHttpAddr"`
	Redis                   RedisConf           `xml:"Redis"`
}

type Blinds struct {
	XMLName   xml.Name `xml:"Blinds"`
	BlindList []Blind  `xml:"Blind"`
}

type Blind struct {
	XMLName     xml.Name `xml:"Blind"`
	ID          int      `xml:",attr"`
	Scale       int64    `xml:",attr"`
	DrawinAmout int64    `xml:",attr"`
}

type Handlevels struct {
	XMLName       xml.Name    `xml:"Handlevels"`
	HandlevelList []Handlevel `xml:"Handlevel"`
}

type Handlevel struct {
	XMLName      xml.Name `xml:"Handlevel"`
	ID           int      `xml:",attr"`
	AwardPercent int      `xml:",attr"`
}
type EventReportHttpAddr struct {
	Host          string `xml:",attr"`
	CurrencyUri   string `xml:",attr"`
	EventUri      string `xml:",attr"`
	FairPlayUri   string `xml:",attr"`
	DeviceInfoUri string `xml:",attr"`
}

var (
	config = new(TexasConf)
)

func GetRedisConf() RedisConf {
	return config.Redis
}

func GetGameDBConf() MysqlConf {
	return config.GameDB
}
func GetSourceType() uint32 {
	return config.ListenAddrConf.SourceType
}
func GetSocketListenAddr() string {
	return config.ListenAddrConf.Host
}

func GetRpcListenAddr() string {
	return config.ListenRpcAddrConf.Host
}
func GetEventReportHttpAddrConf() EventReportHttpAddr {
	return config.EventReportHttpAddrConf
}

func GetLastestVersion() string {
	return config.VersionConf.Lastest
}

func GetAllianceClubNumMax() int {
	return config.AllianceConf.ClubNumMax
}

func GetAward2ClubPercent() int {
	return config.JackpotConf.Award2ClubPercent
}

func LoadConf(filename string) error {
	content, err := ioutil.ReadFile(filename)
	if err != nil {
		logger.Notic("read file:%v error:", filename, err)
		return err
	}
	logger.Info("conf xml:%v", string(content))
	err = xml.Unmarshal(content, config)
	if err != nil {
		logger.Notic("decode xml error:%v", err)
		return err
	}
	DumpConf()
	return nil
}

func DumpConf() {
	logger.Info("--------------config dump----start--------")
	logger.Info("Player Mysql ip:%v port: %V user:%v pwd:%v database:%v", config.GameDB.Ip, config.GameDB.Port, config.GameDB.User, config.GameDB.Pwd, config.GameDB.Database)
	logger.Info("ListenAddr:%v", config.ListenAddrConf.Host)
	logger.Info("VersionConfg:%v", config.VersionConf.Lastest)
	logger.Info("Blinds:%+v", config.BlindConf)
	logger.Info("config.BlindConf:%+v", config.BlindConf)
	logger.Info("config.HandlevelConf:%+v", config.HandlevelConf)
	logger.Info("AllianceConf:%+v", config.AllianceConf.ClubNumMax)
	logger.Info("RedisConf:+%v", config.Redis)
	logger.Info("--------------config dump----end--------")
}

func GetBlindConf() Blinds {
	return config.BlindConf
}

func GetBlindByIndex(idx uint32) Blind {
	for _, value := range config.BlindConf.BlindList {
		if value.ID == int(idx) {
			return value
		}
	}
	return config.BlindConf.BlindList[0]
}

func GetHandlevelByIndex(idx uint32) Handlevel {
	for _, value := range config.HandlevelConf.HandlevelList {
		if value.ID == int(idx) {
			return value
		}
	}
	return config.HandlevelConf.HandlevelList[0]
}

func GetHandlevels() []Handlevel {
	return config.HandlevelConf.HandlevelList
}

func IsTest() bool {
	if config.ListenAddrConf.Test == 1 {
		return true
	}
	return false
}

func GetServerHttpAddrConf() string {
	return config.ServerHttpAddr.Host
}

type ServerHttpAddrConf struct {
	Host string `xml:",attr"`
}
