package model

import (
	"encoding/json"
	"os"

	"github.com/globalsign/mgo"
	"mongo.games.com/goserver/core/logger"
)

const TimeFormat = "2006-01-02 15:04:05"

type GameParam struct {
	Observers                  []func()
	NewPlayerCoin              int32    //新建角色初始金豆数量
	VerifyClientVersion        bool     //验证客户端版本信息
	UseEtcd                    bool     //是否使用etcd
	SrvMaintain                bool     //服务器维护切换
	HundredScenePreCreate      bool     //百人场预创建
	GuestDefaultName           string   //游客默认昵称
	SpreadAccountQPT           int32    //流水上报 条数/每次
	FakeVerifyCode             string   //假的验证码
	MorePlayerLimit            int      //moreplayerdata单次最大查询用户量
	SamePlaceLimit             int32    //和同一个人游戏的局数限制
	RobotInviteInitInterval    int64    //机器人邀请的初始间隔时间(s)
	RobotInviteIntervalMax     int64    //机器人邀请的最大间隔时间(s)
	SceneMaxIdle               int64    //空房间最大空闲时间(超过指定时间自动销毁)(s)
	LoginStateCacheSec         int32    //登录态数据缓存时间
	KickoutDefaultFreezeMinute int64    //踢下线默认封号几分钟
	GameConfigGroupUseRobot    bool     //游戏分组使用机器人
	SameIpNoLimit              bool     //同ip不做限制
	PreLoadRobotCount          int      //预加载机器人数量
	QMOptimization             bool     //全民推广流水优化
	EnterAfterStartSwitch      bool     //中途加入开关
	MongoDebug                 bool     //mongodb调试开关
	WhiteHttpAddr              []string //白名单
	RbAutoBalance              bool
	RbAutoBalanceRate          float64
	RbAutoBalanceCoin          int64
	ThirdPltTransferInterval   int32    //查询第三方平台向系统能否转账时间间隔，单位秒
	ThirdPltTransferMaxTry     int32    //查询第三方平台向系统能否转账,最大尝试次数。默认是1次
	ThirdPltReqTimeout         int32    //三方平台接口请求超时时间设置,默认是30s
	IosStablePrizeMinRecharge  int32    //IOS稳定版最低充值需求
	IosStableInstallPrize      int32    //IOS稳定版安装奖励
	ValidDeviceInfo            bool     //是否验证设备信息
	InvalidRobotAccRate        int      //每次更换过期机器人账号的比例，百分比
	InvalidRobotDay            int      //机器人过期的天数
	CreatePrivateSceneCnt      int      //每人可以创建私有房间数量
	PrivateSceneLogLimit       int      //私有房间日志上限
	PrivateSceneFreeDistroySec int      //私有房间免费解散时间，默认600秒
	PrivateSceneDestroyTax     int      //私有房间提前解散税收,百分比
	NumOfGamesConfig           []int32  //私人房间局数
	BacklogGameHorseRaceLamp   int      //游戏内公告储备多少条，超出丢弃
	IsRobFightTest             bool     //是否开启机器人自己对战功能
	BullFightCtrl0108          bool     //牛牛是否使用新功能规则
	OpenPoolRec                bool     //是否打开水池数据记录
	CoinPoolMinOutRate         int32    //水池最小出分
	CoinPoolMaxOutRate         int32    //水池最大出分
	MaxRTP                     float64  //最大rtp
	AddRTP                     float64  //附加rtp
	PlayerWatchNum             int32    //百人游戏允许围观的局数
	NotifyPlayerWatchNum       int32    //百人游戏围观多少局的时候开始提示
	CgAddr                     string   //后台cg工程地址
	ReplayDataUseJson          bool     //回放数据使用json格式
	MaxAudienceNum             int      //最大观战人数
	IsFindRoomByGroup          bool     //查询房间列表时是否使用互通查询，默认是不使用
	NoOpTimes                  int32    //对战场允许托管的局数
	UseBevRobot                bool     //是否使用行为树机器人
	ClosePreCreateRoom         bool     //关闭予创建房间
	CloseQMThr                 bool     //关闭全民三方流水计算
	ErrResetMongo              bool     //发生主从问题，是否重置连接
	InvitePromoterBind         bool     //绑定全民判定是否推广员
	CSURL                      string   //客户端域名
	InitGameHash               []string //初始哈希
	AtomGameHash               []string //原子哈希
	MatchSeasonRankMaxNum      int      //在榜最大人数
	CloseNoviceGame            []int    // 关闭部分新手补偿调控，游戏id
	CloseNovice                bool     // 关闭全部游戏新手补偿调控
	RankSeasonMaxNum           int      // 排位赛排行榜最大人数
	RankPlayerCoinMaxNum       int      // 金币榜最大人数
	CloseRankListRobot         bool     // 关闭排行榜机器人数据
	RankTimeout                int      // 排行榜刷新时间，秒
	AgoraAddress               string   // 客户端语音api服务地址
	InviteUrl                  string   // 邀请活动，玩家分享页面地址
	WinCoinUpdateTime          int      // 收入榜每日几点更新
	RankWinCoinMaxNum          int      // 收入榜最大人数
	RankInviteMaxNum           int32    // 邀请排行榜最大人数
}

var GameParamPath = "../data/gameparam.json"
var GameParamData = &GameParam{}

func InitGameParam() {
	buf, err := os.ReadFile(GameParamPath)
	if err != nil {
		logger.Logger.Error("InitGameParam os.ReadFile error ->", err)
	}

	err = json.Unmarshal(buf, GameParamData)
	if err != nil {
		logger.Logger.Error("InitGameParam json.Unmarshal error ->", err)
	}
	if GameParamData.SpreadAccountQPT == 0 {
		GameParamData.SpreadAccountQPT = 100
	}
	if GameParamData.MorePlayerLimit == 0 {
		GameParamData.MorePlayerLimit = 50
	}
	if GameParamData.SamePlaceLimit > 10 {
		GameParamData.SamePlaceLimit = 10
	}
	if GameParamData.RobotInviteInitInterval == 0 {
		GameParamData.RobotInviteInitInterval = 2
	}
	if GameParamData.RobotInviteIntervalMax == 0 {
		GameParamData.RobotInviteIntervalMax = 10
	}
	if GameParamData.SceneMaxIdle == 0 {
		GameParamData.SceneMaxIdle = 3600
	}
	if GameParamData.LoginStateCacheSec == 0 {
		GameParamData.LoginStateCacheSec = 300
	}
	if GameParamData.KickoutDefaultFreezeMinute == 0 {
		GameParamData.KickoutDefaultFreezeMinute = 5
	}

	if GameParamData.MaxRTP <= 0.00001 {
		GameParamData.MaxRTP = 0.999999
	}

	if GameParamData.AddRTP <= 0.00001 {
		GameParamData.AddRTP = 0.1
	}

	mgo.SetDebug(GameParamData.MongoDebug)
	if GameParamData.RbAutoBalance {
		if GameParamData.RbAutoBalanceRate == 0 {
			GameParamData.RbAutoBalanceRate = 2
		}
		if GameParamData.RbAutoBalanceCoin == 0 {
			GameParamData.RbAutoBalanceCoin = 100000
		}
	}
	if GameParamData.ThirdPltTransferInterval == 0 {
		GameParamData.ThirdPltTransferInterval = 3
	}
	if GameParamData.ThirdPltTransferMaxTry == 0 {
		GameParamData.ThirdPltTransferMaxTry = 1
	}
	if GameParamData.ThirdPltReqTimeout == 0 {
		GameParamData.ThirdPltReqTimeout = 30
	}
	if GameParamData.IosStablePrizeMinRecharge == 0 {
		GameParamData.IosStablePrizeMinRecharge = 5000
	}
	if GameParamData.IosStableInstallPrize == 0 {
		GameParamData.IosStableInstallPrize = 100
	}
	if GameParamData.InvalidRobotDay == 0 {
		GameParamData.InvalidRobotDay = 90
	}
	if GameParamData.CreatePrivateSceneCnt == 0 {
		GameParamData.CreatePrivateSceneCnt = 20
	}
	if GameParamData.PrivateSceneLogLimit == 0 {
		GameParamData.PrivateSceneLogLimit = 7000
	}
	if GameParamData.PrivateSceneFreeDistroySec == 0 {
		GameParamData.PrivateSceneFreeDistroySec = 600
	}
	if GameParamData.PrivateSceneDestroyTax == 0 {
		GameParamData.PrivateSceneDestroyTax = 5
	}
	if len(GameParamData.NumOfGamesConfig) == 0 {
		GameParamData.NumOfGamesConfig = []int32{5, 10, 20, 50}
	}
	if GameParamData.CoinPoolMinOutRate == 0 {
		GameParamData.CoinPoolMinOutRate = 33
	}

	if GameParamData.CoinPoolMaxOutRate == 0 {
		GameParamData.CoinPoolMaxOutRate = 66
	}

	if GameParamData.PlayerWatchNum <= 2 {
		GameParamData.PlayerWatchNum = 20
	}
	if GameParamData.NotifyPlayerWatchNum <= 2 {
		GameParamData.NotifyPlayerWatchNum = 17
	}
	if GameParamData.NotifyPlayerWatchNum >= GameParamData.PlayerWatchNum {
		GameParamData.NotifyPlayerWatchNum = GameParamData.PlayerWatchNum - 3
	}
	if GameParamData.NoOpTimes <= 2 {
		GameParamData.NoOpTimes = 2
	}

	if GameParamData.BacklogGameHorseRaceLamp == 0 {
		GameParamData.BacklogGameHorseRaceLamp = 50
	}
	if GameParamData.MaxAudienceNum == 0 {
		GameParamData.MaxAudienceNum = 20
	}
	for _, v := range GameParamData.Observers {
		v()
	}
	if len(GameParamData.InitGameHash) == 0 {
		GameParamData.InitGameHash = []string{"ff6c5b1daa1068897377f7a64a762eafda4d225f25bf8e3bb476a7c4f2d10468"}
	}
	if len(GameParamData.AtomGameHash) == 0 {
		GameParamData.AtomGameHash = []string{`0ead8d98e67a7c9197a6bb0e664bb84adbeb25e4e0db63d2158e48b98a50534d`}
	}
	if GameParamData.MatchSeasonRankMaxNum == 0 {
		GameParamData.MatchSeasonRankMaxNum = 50
	}
	if GameParamData.RankSeasonMaxNum == 0 {
		GameParamData.RankSeasonMaxNum = 100
	}
	if GameParamData.RankPlayerCoinMaxNum == 0 {
		GameParamData.RankPlayerCoinMaxNum = 100
	}
	if GameParamData.RankTimeout == 0 {
		GameParamData.RankTimeout = 600
	}
	if GameParamData.RankWinCoinMaxNum == 0 {
		GameParamData.RankWinCoinMaxNum = 100
	}
	if GameParamData.RankInviteMaxNum == 0 {
		GameParamData.RankInviteMaxNum = 50
	}
	if GameParamData.WinCoinUpdateTime == 0 {
		GameParamData.WinCoinUpdateTime = 21
	}
}
