package config

import (
	"encoding/json"
	"game/common/config"
	"game/common/config/game"
	"game/common/constant"
	"github.com/fox/fox/db"
	"github.com/fox/fox/log"
	"sync"
)

const (
	Hundred  = 100   // 放大100倍
	RateBase = 10000 // 概率或者权重的底数是1万
)

var (
	Command *config.Command
	Cfg     *config.Common
	mtx     sync.RWMutex
)

func InitLog() {
	log.Open("./log/color.log", log.DebugL)
	log.Info("")
	log.Info("")
	log.Info("")
	log.Info("-----init log success-----")
}

func LoadConfig(GitCommit, GitBranch, BuildDate string) {
	Command = config.ParseCommand()
	rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, constant.Redis0Config)
	if err != nil {
		log.Error(err.Error())
		return
	}
	defer func() { _ = rdb.Close() }()
	Cfg, err = config.LoadCommonConfig(rdb, GitCommit, GitBranch, BuildDate)
	if err != nil {
		log.Error(err.Error())
		return
	}
	log.DebugF("load common config success")
	if err := config.LoadGameConfig(rdb, game.ColorKey, Cfg); err == nil {
		return
	}
}

// 当后台修改配置时，服务收到消息调用该消息。由于同进程内有多服务，所以要加锁，防止数据竞争
func LoadGameConfig() {
	mtx.Lock()
	defer mtx.Unlock()
	rdb, err := db.InitRedis(Command.RedisPassword, Command.RedisHost, Command.RedisPort, constant.Redis0Config)
	if err != nil {
		log.Error(err.Error())
		return
	}
	defer func() { _ = rdb.Close() }()
	if err := config.LoadGameConfig(rdb, game.ColorKey, Cfg); err == nil {
		return
	}
}

func GetColorConfig() *game.ColorConfig {
	empty := true
	gameCfg := &game.ColorConfig{}
	func() {
		mtx.RLock()
		defer mtx.RUnlock()
		if Cfg.GameJson != "" {
			_ = json.Unmarshal([]byte(Cfg.GameJson), gameCfg)
			empty = false
		}
	}()
	// json不为空，后台有配置，不需要默认配置
	if !empty {
		return gameCfg
	}
	gameCfg.GameTiming = &game.ColorGameTiming{
		// Ready:         100,   // 倒计时321
		Start:         2000,
		Betting:       15000,
		EndBetting:    3000,
		OpenThreeDice: 3000,
		Settle:        7000,
		// Ranking:       1000,
	}

	WinSingleColorWeight := [3]int{50, 25, 25}
	WinSingleColorMul := [3][]*game.ColorMulRate{
		{{Mul: 1 * Hundred, Rate: 100 * Hundred}},

		{{Mul: 2 * Hundred, Rate: 82.5 * Hundred}, {Mul: 3 * Hundred, Rate: 4 * Hundred},
			{Mul: 4 * Hundred, Rate: 3.5 * Hundred}, {Mul: 5 * Hundred, Rate: 3.2 * Hundred},
			{Mul: 6 * Hundred, Rate: 230}, {Mul: 7 * Hundred, Rate: 1.8 * Hundred},
			{Mul: 8 * Hundred, Rate: 1.5 * Hundred}, {Mul: 9 * Hundred, Rate: 1.2 * Hundred}},

		{{Mul: 3 * Hundred, Rate: 94 * Hundred}, {Mul: 9 * Hundred, Rate: 1.4 * Hundred},
			{Mul: 14 * Hundred, Rate: 110}, {Mul: 19 * Hundred, Rate: 0.9 * Hundred},
			{Mul: 29 * Hundred, Rate: 0.8 * Hundred}, {Mul: 49 * Hundred, Rate: 0.7 * Hundred},
			{Mul: 99 * Hundred, Rate: 0.6 * Hundred}, {Mul: 9 * Hundred, Rate: 0.5 * Hundred}},
	}
	WinDoubleColorMul := []*game.ColorMulRate{{Mul: 8 * Hundred, Rate: 66 * Hundred}, {Mul: 11 * Hundred, Rate: 17 * Hundred},
		{Mul: 14 * Hundred, Rate: 9 * Hundred}, {Mul: 19 * Hundred, Rate: 3.5 * Hundred},
		{Mul: 24 * Hundred, Rate: 1.9 * Hundred}, {Mul: 54 * Hundred, Rate: 1.5 * Hundred},
		{Mul: 74 * Hundred, Rate: 1.05 * Hundred}, {Mul: 99 * Hundred, Rate: 0.05 * Hundred}}

	WinThreeColorMul := []*game.ColorMulRate{{Mul: 150 * Hundred, Rate: 84.5 * Hundred}, {Mul: 199 * Hundred, Rate: 5.5 * Hundred},
		{Mul: 299 * Hundred, Rate: 4 * Hundred}, {Mul: 599 * Hundred, Rate: 3 * Hundred},
		{Mul: 799 * Hundred, Rate: 2 * Hundred}, {Mul: 999 * Hundred, Rate: 1 * Hundred}}

	rmConfig := &game.ColorRoomConfig{
		BetList: [][]int64{
			{1000, 2000, 3000, 5000, 10000, 20000},
			{5000, 20000, 30000, 40000, 50000, 800000},
			{10000, 20000, 30000, 50000, 80000, 100000},
		},
		BetLevel:             []int64{0, 10000, 20000},
		WinSingleColorWeight: WinSingleColorWeight[:],
		WinSingleColorMul:    WinSingleColorMul[:],
		WinDoubleColorMul:    WinDoubleColorMul,
		WinThreeColorMul:     WinThreeColorMul,
		InitJackpot:          2000000 * Hundred,
		JackpotRate:          0.05 * Hundred,
		JpXRate:              2 * Hundred,
		JpYRate:              1 * Hundred,
		JpXYRate:             1.5 * Hundred,

		AreaBetLimit:  500000,
		NoBetCountMax: 10,
	}
	gameCfg.Rooms = append(gameCfg.Rooms, rmConfig)
	return gameCfg
}
