package main

import (
	"mongo.games.com/game/protocol/server"
	"mongo.games.com/game/srvdata"
	"mongo.games.com/game/webapi"
)

var (
	ThirdPltGameMappingConfig = &ThirdPlatformGameMappingConfiguration{
		DB_ThirdPlatformGameMappingMgr: srvdata.PBDB_ThirdPlatformGameMappingMgr,
		GamefreeIdMappingMap:           make(map[int32]*server.DB_ThirdPlatformGameMapping),
	}
)

type ThirdPlatformGameMappingConfiguration struct {
	*srvdata.DB_ThirdPlatformGameMappingMgr
	GamefreeIdMappingMap map[int32]*server.DB_ThirdPlatformGameMapping
}

func (this *ThirdPlatformGameMappingConfiguration) Load(fileFullPath string) error {
	// this.Test()
	var rawMappingInfo = make(map[int32]*webapi.WebAPI_ThirdPlatformGameMapping)
	for _, v := range this.Datas.Arr {
		this.GamefreeIdMappingMap[v.GetSystemGameID()] = v
		rawMappingInfo[v.GetSystemGameID()] = &webapi.WebAPI_ThirdPlatformGameMapping{
			GameFreeID:            v.GetSystemGameID(),
			ThirdPlatformName:     v.GetThirdPlatformName(),
			ThirdGameID:           v.GetThirdGameID(),
			Desc:                  v.GetDesc(),
			ScreenOrientationType: v.GetScreenOrientationType(),
			ThirdID:               v.GetThirdID(),
		}
	}
	webapi.ThridPlatformMgrSington.ThridPlatformMap.Range(func(key, value interface{}) bool {
		value.(webapi.IThirdPlatform).InitMappingRelation(rawMappingInfo)
		return true
	})
	return nil
}

func (this *ThirdPlatformGameMappingConfiguration) Reload(fileFullPath string) error {
	//todo 缓存数据加快查找
	//logger.Logger.Info("=== 缓存三方平台游戏id映射关系数据加快查找===")
	this.GamefreeIdMappingMap = make(map[int32]*server.DB_ThirdPlatformGameMapping)
	var rawMappingInfo = make(map[int32]*webapi.WebAPI_ThirdPlatformGameMapping)
	for _, v := range this.Datas.Arr {
		this.GamefreeIdMappingMap[v.GetSystemGameID()] = v
		rawMappingInfo[v.GetSystemGameID()] = &webapi.WebAPI_ThirdPlatformGameMapping{
			GameFreeID:            v.GetSystemGameID(),
			ThirdPlatformName:     v.GetThirdPlatformName(),
			ThirdGameID:           v.GetThirdGameID(),
			Desc:                  v.GetDesc(),
			ScreenOrientationType: v.GetScreenOrientationType(),
			ThirdID:               v.GetThirdID(),
		}
	}
	webapi.ThridPlatformMgrSington.ThridPlatformMap.Range(func(key, value interface{}) bool {
		value.(webapi.IThirdPlatform).InitMappingRelation(rawMappingInfo)
		return true
	})
	return nil
}

func (this *ThirdPlatformGameMappingConfiguration) Test() {
	var rawMappingInfo = make(map[int32]*webapi.WebAPI_ThirdPlatformGameMapping)
	v := &server.DB_ThirdPlatformGameMapping{
		Id:                    1,
		SystemGameID:          9010001,
		ThirdPlatformName:     "测试平台",
		ThirdGameID:           "901",
		Desc:                  "",
		ScreenOrientationType: 0,
		ThirdID:               901,
	}
	this.GamefreeIdMappingMap[v.GetSystemGameID()] = v
	rawMappingInfo[v.GetSystemGameID()] = &webapi.WebAPI_ThirdPlatformGameMapping{
		GameFreeID:            v.GetSystemGameID(),
		ThirdPlatformName:     v.GetThirdPlatformName(),
		ThirdGameID:           v.GetThirdGameID(),
		Desc:                  v.GetDesc(),
		ScreenOrientationType: v.GetScreenOrientationType(),
		ThirdID:               v.GetThirdID(),
	}

	webapi.ThridPlatformMgrSington.ThridPlatformMap.Range(func(key, value interface{}) bool {
		value.(webapi.IThirdPlatform).InitMappingRelation(rawMappingInfo)
		return true
	})
}

func (this *ThirdPlatformGameMappingConfiguration) FindByGameID(gamefreeId int32) *server.DB_ThirdPlatformGameMapping {
	return this.GamefreeIdMappingMap[gamefreeId]
}

// 包含dg的查询
func (this *ThirdPlatformGameMappingConfiguration) FindSystemGamefreeidByThirdGameInfo(thirdPlt string, inThirdGameId, inThirdGameName string) (gamefreeid int32) {
	if v, exist := webapi.ThridPlatformMgrSington.ThridPlatformMap.Load(thirdPlt); exist {
		return v.(webapi.IThirdPlatform).ThirdGameInfo2GamefreeId(&webapi.WebAPI_ThirdPlatformGameMapping{
			ThirdPlatformName: thirdPlt,
			ThirdGameID:       inThirdGameId,
			Desc:              inThirdGameName,
		})
	}
	return 0
}
func (this *ThirdPlatformGameMappingConfiguration) FindThirdIdByThird(thirdName string) (thirdId int32) {
	if v, exist := webapi.ThridPlatformMgrSington.ThridPlatformMap.Load(thirdName); exist {
		if plt, ok := v.(webapi.IThirdPlatform); ok {
			return int32(plt.GetPlatformBase().BaseGameID)
		}
	}
	return 0
}
func (this *ThirdPlatformGameMappingConfiguration) FindThirdInfoBySystemGameId(systemGameId int32) (*server.DB_ThirdPlatformGameMapping, webapi.IThirdPlatform) {
	info := this.FindByGameID(systemGameId)
	if info != nil {
		if v, exist := webapi.ThridPlatformMgrSington.ThridPlatformMap.Load(info.ThirdPlatformName); exist {
			if plt, ok := v.(webapi.IThirdPlatform); ok {
				return info, plt
			}
		}
	}
	return nil, nil
}

func init() {
	srvdata.DataMgrAfter.RegisterLoader("DB_ThirdPlatformGameMapping.dat", ThirdPltGameMappingConfig)
}
