package teen

import (
	"math/rand"
	"server/game"
	"server/game/room"
	"server/msg"
	"sync"
	"time"

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

// 人机操作
func robotOpt(r *room.Room, sitPos int32, optType msg.PlayerOptType) {
	if optType == msg.PlayerOptType_OPT_SELECT {
		go robotSelect(r, sitPos)
	}
	if optType == msg.PlayerOptType_OPT_SELECTSITESHOW {
		go robotAcceptSiteShow(r, sitPos)
	}

}

// 机器人选择操作
func robotSelect(r *room.Room, sitPos int32) {
	log.Debug("robotSelect, sitPos: %d", sitPos)

	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		time.Sleep(time.Second * 1)
		randTime := rand.Intn(2) + 1 // 随机决定操作

		player := r.GetPlayerBySitPos(sitPos)

		// 判断是否看牌
		if !player.IsSeen {
			log.Debug("!player.IsSeen: %d", !player.IsSeen)

			// 没有看牌状态
			// 三分之一概率选择闷牌跟注，四分之三选择看牌
			if randTime == 4 {
				log.Debug("robotBlindChaal: %d", !player.IsSeen)

				robotBlindChaal(r, sitPos) // 闷牌跟注
			} else {
				log.Debug("robotSeen: %d", !player.IsSeen)

				robotSeen(r, sitPos) // 看牌
				// 执行看牌后的操作
				robotPostSeenActions(r, sitPos)
			}
		} else {
			// 已看牌状态
			robotPostSeenActions(r, sitPos)
		}

		wg.Done()
	}()
	wg.Wait()
}

// 执行机器人看牌后的操作
func robotPostSeenActions(r *room.Room, sitPos int32) {
	log.Debug("robotPostSeenActions, sitPos: %d", sitPos)

	// 获取当前玩家
	player := r.GetPlayerBySitPos(sitPos)
	if player == nil {
		log.Debug("Player %d not found for post-seen actions.", sitPos)
		return
	}

	// 判断可执行操作
	possibleActions := []string{}
	if isValidForChaal(r, sitPos) {
		possibleActions = append(possibleActions, "OPT_CHAAL")
	}
	if isValidForPacked(r, sitPos) {
		possibleActions = append(possibleActions, "OPT_PACKED")
	}
	if isValidForSiteShow(r, sitPos) {
		possibleActions = append(possibleActions, "OPT_INIRIATESITESHOW")
	}
	if isValidForShow(r, sitPos) {
		possibleActions = append(possibleActions, "OPT_SHOW")
	}
	// 测试比牌
	// possibleActions = append(possibleActions, "OPT_SITESHOW")
	previousSitPos := getRoomSitPos(r, sitPos, -1)
	var previousPlayer *room.Player
	var i = 0
	for _, player := range r.Players {
		if player.SitPos == previousSitPos {
			previousPlayer = player
		}
		if !player.IsPacked {
			i += 1
		}
	}
	// 随机选择一个可执行操作
	if len(possibleActions) > 0 {
		action := possibleActions[rand.Intn(len(possibleActions))]
		//如果符合比牌的逻辑那么直接
		// 测试
		if !previousPlayer.IsRobot && !previousPlayer.IsPacked && i > 2 && previousPlayer.IsSeen {
			action = "OPT_INIRIATESITESHOW"
		}
		if previousPlayer.IsRobot && !previousPlayer.IsPacked && i > 2 && previousPlayer.IsSeen {
			action = "OPT_INIRIATESITESHOW"
		}
		performRobotAction(r, sitPos, action)
	} else {
		// 如果没有可执行操作，选择弃牌
		log.Debug("No valid actions available for robot, choosing PACKED.")
		robotPacked(r, sitPos)
	}
}

// 机器人执行具体操作
func performRobotAction(r *room.Room, sitPos int32, action string) {
	switch action {
	case "OPT_CHAAL":
		log.Debug("robotChaal, sitPos: %d", sitPos)
		robotChaal(r, sitPos)
	case "OPT_PACKED":
		// log.Debug("robotPacked, sitPos: %d", sitPos)
		robotPacked(r, sitPos)
	case "OPT_INIRIATESITESHOW":
		log.Debug("robotInitiateiteShow, sitPos: %d", sitPos)
		robotInitiateiteShow(r, sitPos)

	case "OPT_SHOW":
		log.Debug("robotShow, sitPos: %d", sitPos)
		robotShow(r, sitPos)
	default:
		log.Debug("Invalid action selected for robot.")
	}
}

// 判断是否可以跟注
func isValidForChaal(r *room.Room, sitPos int32) bool {
	// 判断是否符合跟注条件
	return true
}

// 判断是否可以加倍跟注
func isValidForDoubleChaal(r *room.Room, sitPos int32) bool {
	// 判断是否符合加倍跟注条件
	return true
}

// 判断是否可以弃牌
func isValidForPacked(r *room.Room, sitPos int32) bool {
	// 判断是否符合弃牌条件
	return true
}

// 判断是否可以发起比牌
func isValidForSiteShow(r *room.Room, sitPos int32) bool {
	// 判断是否符合比牌条件

	return checkSiteShow(r, sitPos)
}

// 判断是否可以亮牌
func isValidForShow(r *room.Room, sitPos int32) bool {
	// 判断是否符合亮牌条件
	return checkShow(r, sitPos)
}

// 机器人看牌
func robotSeen(r *room.Room, sitPos int32) {
	log.Debug("robotSeen, sitPos: %d", sitPos)

	player := r.GetPlayerBySitPos(sitPos)
	if player == nil {
		log.Debug("Player %d not found.", sitPos)
		return
	}

	// 设置玩家为看牌状态
	player.IsSeen = true
	recvPlayerOptAction(r, sitPos, &msg.PlayerOpt{OptType: msg.PlayerOptType_OPT_SEEN})
	log.Debug("Player %d has seen their cards.", sitPos)

}

// 闷牌跟注
func robotBlindChaal(r *room.Room, sitPos int32) {
	log.Debug("robotBlindChaal, sitPos: %d", sitPos)
	// 实现闷牌跟注的操作
	log.Debug("robotChaal, sitPos: %d", sitPos)

	// 获取1-5秒的随机时间
	randTime := rand.Intn(5) + 1

	// 获取房间中的最低和最高下注金额
	minBet := int(r.GameRound.ResBet.MinBlindBet)
	maxBet := int(r.GameRound.ResBet.MaxBlindBet)

	// 随机选择最低或最高下注金额
	randBet := minBet
	if rand.Intn(2) == 1 {
		randBet = maxBet // 50% 概率选择最高下注金额
	}

	game.Module.Skeleton.AfterFunc(time.Second*time.Duration(randTime), func() {
		recvPlayerOptAction(r, sitPos, &msg.PlayerOpt{
			OptType:   msg.PlayerOptType_OPT_BLIND,
			BetAmount: int32(randBet), // 传递随机选择的下注金额
		})
	})
}

// 跟注
func robotChaal(r *room.Room, sitPos int32) {
	log.Debug("robotChaal, sitPos: %d", sitPos)
	// 获取1-5秒的随机时间
	randTime := rand.Intn(5) + 1

	// 获取房间中的最低和最高下注金额
	minBet := int(r.GameRound.ResBet.MinCheelBet)
	maxBet := int(r.GameRound.ResBet.MaxCheelBet)

	// 随机选择最低或最高下注金额
	randBet := minBet
	if rand.Intn(2) == 1 {
		randBet = maxBet // 50% 概率选择最高下注金额
	}

	game.Module.Skeleton.AfterFunc(time.Second*time.Duration(randTime), func() {
		recvPlayerOptAction(r, sitPos, &msg.PlayerOpt{
			OptType:   msg.PlayerOptType_OPT_CHAAL,
			BetAmount: int32(randBet), // 传递随机选择的下注金额
		})
	})
	// 实现跟注的操作
}

// 弃牌
func robotPacked(r *room.Room, sitPos int32) {
	log.Debug("robotPacked, sitPos: %d", sitPos)
	//获取1-5的随机数
	randTime := rand.Intn(5) + 1
	game.Module.Skeleton.AfterFunc(time.Second*time.Duration(randTime), func() {
		recvPlayerOptAction(r, sitPos, &msg.PlayerOpt{OptType: msg.PlayerOptType_OPT_PACKED})
	})
	// 实现弃牌的操作
}

// 亮牌
func robotShow(r *room.Room, sitPos int32) {
	log.Debug("robotShow, sitPos: %d", sitPos)
	//获取1-5的随机数
	randTime := rand.Intn(5) + 1
	game.Module.Skeleton.AfterFunc(time.Second*time.Duration(randTime), func() {
		recvPlayerOptAction(r, sitPos, &msg.PlayerOpt{OptType: msg.PlayerOptType_OPT_SHOW})
	})
	// 实现亮牌的操作
}

// 机器人发起SiteShow
func robotInitiateiteShow(r *room.Room, sitPos int32) {
	log.Debug("robotSiteShow, sitPos: %d", sitPos)
	//获取1-5的随机数
	randTime := rand.Intn(5) + 1
	game.Module.Skeleton.AfterFunc(time.Second*time.Duration(randTime), func() {
		recvPlayerOptAction(r, sitPos, &msg.PlayerOpt{OptType: msg.PlayerOptType_OPT_SITESHOW})
	})
	// 实现亮牌的操作
}

// 机器人选择是否同意比牌
func robotAcceptSiteShow(r *room.Room, sitPos int32) {

	// 假设机器人有50%的几率同意比牌，50%的几率拒绝
	agreeToShow := rand.Intn(2) == 0 // 0 表示同意比牌，1 表示拒绝比牌

	if agreeToShow {
		// 机器人同意比牌
		log.Debug("Robot at SitPos %d agrees to show cards", sitPos)
		// 这里可以调用比牌的函数
		robotAgreeSiteShow(r, sitPos)
		// 比如：startShowCards(r, sitPos)  // 这个函数是示例，实际需要根据你的需求调用比牌逻辑
	} else {
		// 机器人拒绝比牌
		log.Debug("Robot at SitPos %d refuses to show cards", sitPos)
		// 这里可以调用拒绝比牌的函数
		robotRefuseSiteShow(r, sitPos)
		// 比如：refuseShowCards(r, sitPos)  // 这个函数是示例，实际需要根据你的需求调用拒绝比牌的逻辑
	}
}

func robotAgreeSiteShow(r *room.Room, sitPos int32) {
	//获取1-5的随机数
	randTime := rand.Intn(5) + 1
	game.Module.Skeleton.AfterFunc(time.Second*time.Duration(randTime), func() {
		recvPlayerOptAction(r, sitPos, &msg.PlayerOpt{OptType: msg.PlayerOptType_OPT_AGREESITESHOW})
	})
}
func robotRefuseSiteShow(r *room.Room, sitPos int32) {
	//获取1-5的随机数
	randTime := rand.Intn(5) + 1
	game.Module.Skeleton.AfterFunc(time.Second*time.Duration(randTime), func() {
		recvPlayerOptAction(r, sitPos, &msg.PlayerOpt{OptType: msg.PlayerOptType_OPT_REFUSESITESHOW})
	})

}
