package ludo

import (
	agentmanager "server/game/agentManager"
	"server/msg"
	"server/tools"
	"sync"
	"time"

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

type MatchModeItem struct {
	enterNum  int32
	rewardNum int32
	level     int32
}

// GameRule 定义游戏规则
type GameRule struct {
	MAX_STEP      int32
	OPT_TIME      int32
	WU_DI_ROAD    []string
	AN_QUAN_ROAD  []string
	PVP_MODE_LIST []*MatchModeItem
}

type Config struct {
	RoomMap    map[int32]*msg.RoomInfo // Key is room ID
	mu         sync.RWMutex            // Protects RoomMap access
	BattleRoom map[int32]*RoomInfoWrapper
}

var (
	gameConfig *Config
	gameRule   = GameRule{
		PVP_MODE_LIST: makeMatchConfig(),
		OPT_TIME:      10,
		MAX_STEP:      57,
		WU_DI_ROAD: []string{
			"p_2", "p_15", "p_28", "p_41", "p_23", "p_10", "p_36", "p_49",
			"red_1", "red_2", "red_3", "red_4", "red_5", "red_des",
			"green_1", "green_2", "green_3", "green_4", "green_5", "green_des",
			"yellow_1", "yellow_2", "yellow_3", "yellow_4", "yellow_5", "yellow_des",
			"blue_1", "blue_2", "blue_3", "blue_4", "blue_5", "blue_des",
		},
		AN_QUAN_ROAD: []string{
			"p_2", "p_15", "p_28", "p_41", "p_23", "p_10", "p_36", "p_49",
		},
	}

	redRoad = []string{
		"nil", "p_2", "p_3", "p_4", "p_5", "p_6", "p_7", "p_8", "p_9", "p_10",
		"p_11", "p_12", "p_13", "p_14", "p_15", "p_16", "p_17", "p_18",
		"p_19", "p_20", "p_21", "p_22", "p_23", "p_24", "p_25", "p_26",
		"p_27", "p_28", "p_29", "p_30", "p_31", "p_32", "p_33", "p_34",
		"p_35", "p_36", "p_37", "p_38", "p_39", "p_40", "p_41", "p_42",
		"p_43", "p_44", "p_45", "p_46", "p_47", "p_48", "p_49", "p_50",
		"p_51", "p_52", "red_1", "red_2", "red_3", "red_4", "red_5", "red_des",
	}

	greenRoad = []string{
		"nil", "p_15", "p_16", "p_17", "p_18", "p_19", "p_20", "p_21", "p_22",
		"p_23", "p_24", "p_25", "p_26", "p_27", "p_28", "p_29", "p_30",
		"p_31", "p_32", "p_33", "p_34", "p_35", "p_36", "p_37", "p_38",
		"p_39", "p_40", "p_41", "p_42", "p_43", "p_44", "p_45", "p_46",
		"p_47", "p_48", "p_49", "p_50", "p_51", "p_52", "p_53", "p_2",
		"p_3", "p_4", "p_5", "p_6", "p_7", "p_8", "p_9", "p_10", "p_11",
		"p_12", "p_13", "green_1", "green_2", "green_3", "green_4", "green_5", "green_des",
	}

	yellowRoad = []string{
		"nil", "p_28", "p_29", "p_30", "p_31", "p_32", "p_33", "p_34", "p_35",
		"p_36", "p_37", "p_38", "p_39", "p_40", "p_41", "p_42", "p_43",
		"p_44", "p_45", "p_46", "p_47", "p_48", "p_49", "p_50", "p_51",
		"p_52", "p_53", "p_2", "p_3", "p_4", "p_5", "p_6", "p_7", "p_8",
		"p_9", "p_10", "p_11", "p_12", "p_13", "p_14", "p_15", "p_16",
		"p_17", "p_18", "p_19", "p_20", "p_21", "p_22", "p_23", "p_24",
		"p_25", "p_26", "yellow_1", "yellow_2", "yellow_3", "yellow_4", "yellow_5", "yellow_des",
	}

	blueRoad = []string{
		"nil", "p_41", "p_42", "p_43", "p_44", "p_45", "p_46", "p_47", "p_48",
		"p_49", "p_50", "p_51", "p_52", "p_53", "p_2", "p_3", "p_4",
		"p_5", "p_6", "p_7", "p_8", "p_9", "p_10", "p_11", "p_12", "p_13",
		"p_14", "p_15", "p_16", "p_17", "p_18", "p_19", "p_20", "p_21",
		"p_22", "p_23", "p_24", "p_25", "p_26", "p_27", "p_28", "p_29",
		"p_30", "p_31", "p_32", "p_33", "p_34", "p_35", "p_36", "p_37",
		"p_38", "p_39", "blue_1", "blue_2", "blue_3", "blue_4", "blue_5", "blue_des",
	}
)

func (gameRule *GameRule) getMatchByLevle(level int32) *MatchModeItem {
	for i := 0; i < len(gameRule.PVP_MODE_LIST); i++ {
		item := gameRule.PVP_MODE_LIST[i]
		if item.level == level {
			return item
		}
	}
	return nil
}

func makeMatchConfig() []*MatchModeItem {
	list := make([]*MatchModeItem, 0)
	list = append(list, &MatchModeItem{
		enterNum:  500,
		rewardNum: 950,
		level:     1,
	})
	list = append(list, &MatchModeItem{
		enterNum:  1000,
		rewardNum: 1900,
		level:     2,
	})
	list = append(list, &MatchModeItem{
		enterNum:  1500,
		rewardNum: 2850,
		level:     3,
	})
	list = append(list, &MatchModeItem{
		enterNum:  2000,
		rewardNum: 3800,
		level:     4,
	})
	return list
}

func InitGame() {
	gameConfig = &Config{
		RoomMap:    make(map[int32]*msg.RoomInfo),
		BattleRoom: make(map[int32]*RoomInfoWrapper),
	}
	go handleEvents()
	go StartMatchRobotTask()
}

func startGame(room_info *msg.RoomInfo) *RoomInfoWrapper {
	room_info.RoomStatus = msg.RoomStatus_START //状态设置为开始游戏状态
	//给每个玩家分配阵营初始化角色
	initRoles(room_info)
	//设置第一个操作的阵营
	setFirstColor(room_info)
	// player_count := getPlayerCount(room_info)
	room := &RoomInfoWrapper{room_info, tools.NewTimerManager(), time.Now()}
	runBattle(room)
	return room
}

func getAgentByUserId(userId string) gate.Agent {
	ag := agentmanager.GetAgentByUserID(userId)
	if ag == nil {
		log.Error("agent is nil userId = %s ", userId)
		return nil
	}
	return ag
}
