package ludo

import (
	"fmt"
	"server/msg"
	"strings"

	"github.com/name5566/leaf/log"
)

// 添加完成的玩家
func (room_info *RoomInfoWrapper) addFinishColor(color msg.RoleType) {
	color_data := room_info.getColorDataByColor(color)
	color_data.IsFinish = true
	room_info.FinishColors = append(room_info.FinishColors, color_data)
}

// 根据颜色获取阵容数据
func (room_info *RoomInfoWrapper) getColorDataByColor(color msg.RoleType) *msg.ColorData {
	for i := 0; i < len(room_info.Colors); i++ {
		element := room_info.Colors[i]
		if element.MColor == color {
			return element
		}
	}
	return nil
}

// 获取完成的阵营
func (room_info *RoomInfoWrapper) getFinishColors() []*msg.ColorData {
	return room_info.FinishColors
}

// 获取没有被踢出去的玩家
func (room_info *RoomInfoWrapper) getNotKickColors() []*msg.ColorData {
	temp := make([]*msg.ColorData, 0, 1)
	for i := 0; i < len(room_info.Colors); i++ {
		element := room_info.Colors[i]
		if !element.IsKick {
			temp = append(temp, element)
			break
		}
	}
	return temp
}

// 获取被踢出去的玩家
func (room_info *RoomInfoWrapper) getKickColors() []*msg.ColorData {
	temp := make([]*msg.ColorData, 0, 3)
	for i := 0; i < len(room_info.Colors); i++ {
		element := room_info.Colors[i]
		if element.IsKick {
			temp = append(temp, element)
			break
		}
	}
	return temp
}

// 更新角色位置
func (room_info *RoomInfoWrapper) updateRoleStep(role_id string, step int32) {
	role_data := room_info.getRoleDataById(role_id)
	role_data.OldSetp = role_data.Step
	role_data.Step = step
}

// 是否结束游戏
func (room_info *RoomInfoWrapper) IsOverGame() bool {
	colors := room_info.Colors
	if len(colors) == 1 { //说明就剩下一个人了
		return true
	}
	count := 0
	for i := 0; i < len(colors); i++ {
		element := colors[i]
		if element.IsFinish || element.IsKick {
			count++
		}
	}
	return count == len(colors)
}

func (room_info *RoomInfoWrapper) getColors() []*msg.ColorData {
	return room_info.Colors
}

// 开始下个玩家的回合
func (room_info *RoomInfoWrapper) setNextPlayerRound() {
	room_info.CurRoundColor = room_info.findNextColor()
	fmt.Printf("setNextPlayerRound = %d\n", room_info.CurRoundColor)
	if room_info.CurRoundColor == msg.RoleType_ROLE_TYPE_UNKNOWN {
		log.Error("setNextPlayerRound error")
	}
	room_info.notify_update_room_info()
}

// 找到下一个没有完成的玩家
func (room_info *RoomInfoWrapper) findNextColor() msg.RoleType {
	cur := room_info.CurRoundColor
	colors := room_info.getColors()

	i := roleIndexOf(colors, room_info.getColorDataByColor(cur))
	if i == (len(colors) - 1) {
		i = 0
	} else {
		i = (i + 1)
	}

	for index := i; index < len(colors); index++ {
		element := colors[index]
		if !element.IsFinish && !element.IsKick {
			return element.MColor
		}
	}

	for index := 0; index < i; index++ {
		element := colors[index]
		if !element.IsFinish && !element.IsKick {
			return element.MColor
		}
	}
	return msg.RoleType_ROLE_TYPE_UNKNOWN
}

// 当前玩家是否是机器人
func (room_info *RoomInfoWrapper) containsRobot(color msg.RoleType) bool {
	color_data := room_info.getColorDataByColor(color)
	return strings.Contains(color_data.MId, "robot")
}

// 获取一个颜色的玩家有多少个*不在家*的角色
func (room_info *RoomInfoWrapper) getRoleNotAtHomeByColor(color msg.RoleType) []*msg.RoleData {
	temp := make([]*msg.RoleData, 0, 4)
	roles := room_info.Roles
	for i := range roles {
		element := roles[i]
		if element.MColor == color && element.Step != 0 {
			temp = append(temp, element)
		}
	}
	return temp
}

// 根据操作获取最后一个操作回合数据
func (room_info *RoomInfoWrapper) getLastRoundByOpt(opt msg.OptType) *msg.Round {
	length := len(room_info.Rounds)
	for i := (length - 1); i >= 0; i-- {
		element := room_info.Rounds[i]
		if opt == element.Opt {
			return element
		}
	}
	return nil
}

// 获取地图位置
func (room_info *RoomInfoWrapper) getMapPosByColorAndStep(color msg.RoleType, step int32) string {
	if color == msg.RoleType_RED {
		return redRoad[step]
	} else if color == msg.RoleType_GREEN {
		return greenRoad[step]
	} else if color == msg.RoleType_BLUE {
		return blueRoad[step]
	} else if color == msg.RoleType_YELLOW {
		return yellowRoad[step]
	}
	return ""
}

// 获取目标路上所有角色
func (room_info *RoomInfoWrapper) getRoadRoles(key1 string) []*msg.RoleData {
	temp := make([]*msg.RoleData, 0, 10) // 初始长度 0，容量 10
	for i := 0; i < len(room_info.Roles); i++ {
		element := room_info.Roles[i]
		role_road_key := room_info.getMapPosByColorAndStep(element.MColor, element.Step)
		if key1 == role_road_key {
			temp = append(temp, element)
		}
	}
	return temp
}

// 是否是无敌的点
func (room_info *RoomInfoWrapper) isWuDiRoad(key1 string) bool {
	list := gameRule.WU_DI_ROAD
	for i := 0; i < len(list); i++ {
		element := list[i]
		if key1 == element {
			return true
		}
	}
	return false
}

// 判断目标非自己阵营的角色
func (room_info *RoomInfoWrapper) getRolesByColorRoles(color msg.RoleType, roles []*msg.RoleData) []*msg.RoleData {
	temp := make([]*msg.RoleData, 0, 10) // 初始长度 0，容量 10
	for i := range roles {
		element := roles[i]
		if element.MColor != color {
			temp = append(temp, element)
		}
	}
	return temp
}

// 将一批角色踢回家
func (room_info *RoomInfoWrapper) kickRolesBackHome(roles []*msg.RoleData) {
	for i := range roles {
		role_data := roles[i]
		role_data.Step = 0
	}
}

// 移动的目标是否是终点
func (room_info *RoomInfoWrapper) moveTargetIsEnd(color msg.RoleType, key1 string) bool {
	if color == msg.RoleType_BLUE {
		return key1 == "blue_des"
	} else if color == msg.RoleType_YELLOW {
		return key1 == "yellow_des"
	} else if color == msg.RoleType_GREEN {
		return key1 == "green_des"
	} else if color == msg.RoleType_RED {
		return key1 == "red_des"
	}
	return false
}

// 根据颜色获取有多少个完成竞赛的角色
func (room_info *RoomInfoWrapper) getFinishRolesByColor(color msg.RoleType) []*msg.RoleData {
	temp := make([]*msg.RoleData, 0, 10)
	if color == msg.RoleType_BLUE {
		temp = room_info.getRoadRoles("blue_des")
	} else if color == msg.RoleType_YELLOW {
		temp = room_info.getRoadRoles("yellow_des")
	} else if color == msg.RoleType_GREEN {
		temp = room_info.getRoadRoles("green_des")
	} else if color == msg.RoleType_RED {
		temp = room_info.getRoadRoles("red_des")
	}
	return temp
}

// 根据颜色获取对应颜色的所有角色
func (room_info *RoomInfoWrapper) getRoleDataByColor(color msg.RoleType) []*msg.RoleData {
	list := make([]*msg.RoleData, 0, 4) // 初始长度 0，容量 10
	for _, v := range room_info.Roles {
		if v.MColor == color {
			list = append(list, v)
		}
	}
	return list
}

// 根据id获取角色
func (room_info *RoomInfoWrapper) getRoleDataById(role_id string) *msg.RoleData {
	for i := 0; i < len(room_info.Roles); i++ {
		element := room_info.Roles[i]
		if element.MId == role_id {
			return element
		}
	}
	return nil
}

// 记录每个回合
func (room_info *RoomInfoWrapper) addRound(rd *msg.Round) {
	room_info.Rounds = append(room_info.Rounds, rd)
}

// 根据骰子数获取角色列表
func (room_info *RoomInfoWrapper) getRolesBySzNumber(color msg.RoleType, szNumber int32) []*msg.RoleData {
	temp := make([]*msg.RoleData, 0, 4) // 初始长度 0，容量 10
	list := room_info.getRoleDataByColor(color)
	for i := 0; i < len(list); i++ {
		element := list[i]
		des_step := element.Step + szNumber
		if des_step < gameRule.MAX_STEP {
			temp = append(temp, element)
		}
	}
	return temp
}

// 根据筛子数判定角色是否可以移动
func (room_info *RoomInfoWrapper) roleIsCanMove(role_id string, szNumber int32) bool {
	role_data := room_info.getRoleDataById(role_id)
	des_step := role_data.MSeat + szNumber
	return des_step <= gameRule.MAX_STEP
}

// 获取可以移动的玩家
func (room_info *RoomInfoWrapper) getCanMoveRoles(roles []*msg.RoleData, szNumber int32) []*msg.RoleData {
	temp := make([]*msg.RoleData, 0, 4)
	for i := 0; i < len(roles); i++ {
		element := roles[i]
		if room_info.roleIsCanMove(element.MId, szNumber) {
			temp = append(temp, element)
		}
	}
	return temp
}

// 设置当前玩家操作类型
func (room_info *RoomInfoWrapper) setCurColorOpt(opt *msg.NotifyPlayerOpt) {
	room_info.CurColorOptData = opt
}

// 将玩家踢出房间
func (room_info *RoomInfoWrapper) kickColor(color msg.RoleType) {
	color_data := room_info.getColorDataByColor(color)
	color_data.IsKick = true
}

// 获取房间倒计时的key
func (room_info *RoomInfoWrapper) GetRoomTimeOutKey() string {
	return fmt.Sprintf("%d_TimeOut", room_info.Id)
}

// 获取房间ID
func (room_info *RoomInfoWrapper) GetRoomId() int32 {
	return room_info.Id
}
