package base

import (
	"fmt"
	"time"

	"mongo.games.com/goserver/core/basic"
	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/module"
	"mongo.games.com/goserver/core/task"

	"mongo.games.com/game/common"
	"mongo.games.com/game/model"
	"mongo.games.com/game/protocol/webapi"
)

func init() {
	module.RegisteModule(SysProfitCoinMgr, time.Hour, 0)
}

var SysProfitCoinMgr = &SysProfitCoinManager{}

// SysProfitCoinManager 存储
// 目前只有捕鱼用到
type SysProfitCoinManager struct {
	SysPfCoin *model.SysProfitCoin
}

func (this *SysProfitCoinManager) GetBetAndSysOut(key string) (int64, int64) {
	if data, exist := this.SysPfCoin.ProfitCoin[key]; exist {
		return data.PlaysBet, data.SysPushCoin
	}
	return 1, 0
}

// PushBetAndSysOut 增加玩家总投入，系统总产出
func (this *SysProfitCoinManager) PushBetAndSysOut(key string, bet int64, systemOut int64) {
	data, ok := this.SysPfCoin.ProfitCoin[key]
	if !ok {
		data = new(model.SysCoin)
		this.SysPfCoin.ProfitCoin[key] = data
	}
	data.PlaysBet += bet
	data.SysPushCoin += systemOut
}

func (this *SysProfitCoinManager) Get(key string) *model.SysCoin {
	data, ok := this.SysPfCoin.ProfitCoin[key]
	if !ok {
		data = new(model.SysCoin)
		this.SysPfCoin.ProfitCoin[key] = data
	}
	return data
}

func (this *SysProfitCoinManager) Del(key string) {
	if _, ok := this.SysPfCoin.ProfitCoin[key]; ok {
		delete(this.SysPfCoin.ProfitCoin, key)
	}
}

func (this *SysProfitCoinManager) ModuleName() string {
	return "SysProfitCoinManager"
}

func (this *SysProfitCoinManager) Init() {
	this.SysPfCoin = model.InitSysProfitCoinData(fmt.Sprintf("%d", common.GetSelfSrvId()))
}

func (this *SysProfitCoinManager) Update() {
	this.Save()
}

func (this *SysProfitCoinManager) Shutdown() {
	this.Save()
	module.UnregisteModule(this)
}

func (this *SysProfitCoinManager) Save() {
	data := &model.SysProfitCoin{ //
		ProfitCoin: make(map[string]*model.SysCoin),
		LogId:      this.SysPfCoin.LogId,
		Key:        this.SysPfCoin.Key,
	}
	for i, v := range this.SysPfCoin.ProfitCoin {
		v1 := &model.SysCoin{ //
			PlaysBet:    v.PlaysBet,
			SysPushCoin: v.SysPushCoin,
			//CommonPool:  v.CommonPool,
			//Version:     v.Version,
		}
		data.ProfitCoin[i] = v1
	}
	task.New(nil, task.CallableWrapper(func(o *basic.Object) interface{} {
		err := model.SaveSysProfitCoin(data /*this.SysPfCoin*/) //  fatal error: concurrent map iteration and map write
		if err != nil {
			logger.Logger.Errorf("SaveSysProfitCoin err:%v ", err)
		}
		return err
	}), nil, "SaveSysProfitCoin").Start()
}

type CoinPoolFishListener struct {
}

func (f *CoinPoolFishListener) OnSettingUpdate(oldSetting, newSetting *webapi.CoinPoolSetting) {
	//GameFree := srvdata.PBDB_GameFreeMgr.GetData(newSetting.GetGameFreeId())
	//if GameFree != nil {
	//	gameId := int(GameFree.GetGameId())
	//	if gameId == common.GameId_HFishing || gameId == common.GameId_TFishing {
	//		if oldSetting == nil || oldSetting.GetCtroRate() != newSetting.GetCtroRate() {
	//			keyPf := fmt.Sprintf("%v_%v", newSetting.GetPlatform(), newSetting.GetGameFreeId())
	//			SysProfitCoinMgr.Del(keyPf)
	//		}
	//	}
	//}
}
