package gameServerMgr

import (
	"fmt"

	. "Framework/managecenterModel"
)

var (
	mServerMap = make(map[int32][]*Server, 0)
)

//合作商、服务器组合列表
type PartnerServer struct {
	//合作商Id
	mPartnerId int32

	//服务器Id
	mServerId int32
}

//解析服务器信息
func ParseServerInfo(serverList []*Server) {
	tempServerMap := make(map[int32][]*Server, 0)
	for _, server := range serverList {
		_, exist := tempServerMap[server.PartnerId]
		if !exist {
			tempServerMap[server.PartnerId] = make([]*Server, 0)
		}
		tempServerMap[server.PartnerId] = append(tempServerMap[server.PartnerId], server)
	}
	mServerMap = tempServerMap
}

//获取服务器对象
func GetServerItem(partnerId, serverId int32) (server *Server, exist bool) {
	serverList, existServer := mServerMap[partnerId]
	if !existServer {
		return
	}
	for _, serverItem := range serverList {
		if serverItem.Id == serverId {
			server = serverItem
			exist = true
			break
		}
	}
	return
}

//判断服务器是否存在
func IfServerExists(partnerId, serverId int32) (exists bool) {
	_, exists = GetPartnerItem(partnerId)
	if !exists {
		return
	}
	_, exists = GetServerItem(partnerId, serverId)
	if !exists {
		return
	}

	return
}

//获取服务器名称
func GetServerName(partnerId, serverId int32) (serverName string) {
	_, existPartner := GetPartnerItem(partnerId)
	if !existPartner {
		return
	}
	server, existServer := GetServerItem(partnerId, serverId)
	if !existServer {
		return
	}
	serverName = server.Name

	return
}

//检测是否有游戏版本更新
func CheckNewGameVersion(partnerId, serverId, gameVersionId int32) (gameVersionUrl string, exist bool) {
	server, existServer := GetServerItem(partnerId, serverId)
	if !existServer {
		exist = existServer
		return
	}

	if gameVersionId < server.MinGameVersionId {
		partner, existPartner := GetPartnerItem(partnerId)
		if !existPartner {
			exist = existPartner
			return
		}
		exist = true
		gameVersionUrl = partner.GameVersionUrl
	}

	return
}

//获取合作商、服务器的组合字符串
//字符串格式：PartnerId_ServerId|PartnerId_ServerId|
func GetPartnerServerPairString() (partnerServerPair string) {
	for _, ps := range GetPartnerServerPairList() {
		partnerServerPair += fmt.Sprintf("%d_%d|", ps.mPartnerId, ps.mServerId)
	}
	return
}

//获取合作商、服务器的组合列表
func GetPartnerServerPairList() (partnerServerList []*PartnerServer) {
	for key := range mServerMap {
		for _, server := range mServerMap[key] {
			if server.GroupId == GetServerGroup().Id {
				var ps *PartnerServer = &PartnerServer{server.PartnerId, server.Id}
				partnerServerList = append(partnerServerList, ps)
			}
		}
	}

	return
}

// // 获取合作商、服务器的组合字符串
// // serverGroupId：服务器组Id
// // 返回值
// // 合作商、服务器的组合字符串
// func (this *ManageCenterUtil) GetPartnerServerPairString(serverGroupId int32) string {
// 	var buf bytes.Buffer

// 	serverList := managecenterMgr.GetServerList(serverGroupId)
// 	for _, item := range serverList {
// 		buf.WriteString(fmt.Sprintf("%d_%d|", item.PartnerId, item.Id))
// 	}

// 	return buf.String()
// }

// // 是否是有效的合作商、服务器组合
// // partnerId：合作商Id
// // serverId：服务器Id
// // parnterServerPairString：合作商、服务器的组合字符串,格式为:PartnerId_ServerId|PartnerId_ServerId|
// // 返回值
// // 是否有效
// func (this *ManageCenterUtil) IfValidPartnerServerPair(partnerId, serverId int32, parnterServerPairString string) bool {
// 	if parnterServerPairString == "" {
// 		return false
// 	}

// 	partnerServerPairStringList := strings.Split(parnterServerPairString, "|")
// 	if len(partnerServerPairStringList) == 0 {
// 		return false
// 	}

// 	// 获得玩家的合作商、服务器组合字符串
// 	partnerServerPair := fmt.Sprintf("%d_%d", partnerId, serverId)

// 	// 遍历寻找
// 	for _, item := range partnerServerPairStringList {
// 		if item == partnerServerPair {
// 			return true
// 		}
// 	}

// 	return false
// }
