package qznn

import "github.com/sirupsen/logrus"

func WinLevel(leftNum, rightNum []uint32) (bool, bool, int) {
	leftNum = TransCardNum(leftNum)
	rightNum = TransCardNum(rightNum)

	pointLeft := GetNiuniuPoint(leftNum)
	pointRight := GetNiuniuPoint(rightNum)

	maxLeft, suitLeft := FindMaxCardInList(leftNum)
	maxRight, suitRight := FindMaxCardInList(rightNum)

	isWuhuaLeft := IsWuhuaWuXiaoSiZha(leftNum)
	isWuhuaRight := IsWuhuaWuXiaoSiZha(rightNum)

	logrus.Infof("left is wx:%v, right is wx:%v", isWuhuaLeft, isWuhuaRight)

	if isWuhuaLeft && !isWuhuaRight {
		return true, false, 3
	}

	if !isWuhuaLeft && isWuhuaRight {
		return false, false, 0
	}

	if isWuhuaLeft && isWuhuaRight {
		//五花<四炸<五小
		if IsWuhuaNiu(leftNum) && !IsWuhuaNiu(rightNum) {
			return true, false, 3
		}

		if IsSiZha(leftNum) && IsWuhuaNiu(rightNum) {
			return false, false, 3
		}

		if IsSiZha(leftNum) && IsWuXiaoNiu(rightNum) {
			return true, false, 3
		}

		if IsWuXiaoNiu(leftNum) && !IsWuXiaoNiu(rightNum) {
			return false, false, 3
		}

		//相同的情况，比花色
		if maxLeft > maxRight {
			return true, false, 3
		}

		if maxLeft == maxRight {
			if suitLeft > suitRight {
				return true, false, 3
			}

			if suitLeft == suitRight {
				return false, true, 0
			}

			if suitLeft < suitRight {
				return false, false, 0
			}
		}

		if maxLeft < maxRight {
			return false, false, 0
		}
	}

	isLeftWin := false
	if pointLeft > pointRight {
		isLeftWin = true
	}

	if pointLeft == pointRight {
		if maxLeft > maxRight {
			isLeftWin = true
		}

		if maxLeft == maxRight {
			if suitLeft > suitRight {
				isLeftWin = true
			}

			//和的情况
			if suitLeft == suitRight {
				return false, true, 0
			}
		}
	}

	if isLeftWin {
		if pointLeft == 7 || pointLeft == 8 || pointLeft == 9 {
			return true, false, 2
		}

		if pointLeft == 10 {
			return true, false, 3
		}

		return true, false, 1
	}

	return false, false, 0
}

func IsWuhuaNiu(numList []uint32) bool {
	for _, item := range numList {
		if item <= 10 {
			return false
		}
	}

	return true
}

func IsWuXiaoNiu(numList []uint32) bool {
	total := 0
	for _, item := range numList {
		if item >= 5 {
			return false
		}

		total += int(item)
	}

	return total < 10
}

func IsSiZha(numList []uint32) bool {
	numMap := map[uint32]int{}

	for _, item := range numList {
		// if item >= 5 {
		// 	return false
		// }

		numMap[item] = numMap[item] + 1
	}

	if len(numMap) == 1 {
		return true
	}

	for _, item := range numMap {
		if item >= 4 {
			return true
		}
	}
	// if numMap[0] == 4 || numMap[1] == 4 {
	// 	return true
	// }

	return false
}

func IsWuhuaWuXiaoSiZha(numList []uint32) bool {
	if IsWuhuaNiu(numList) {
		return true
	}

	if IsWuXiaoNiu(numList) {
		return true
	}

	if IsSiZha(numList) {
		return true
	}

	return false
}

func IsWuhuaWuXiaoSiZhaDesc(numList []uint32) (bool, string) {
	if IsWuhuaNiu(numList) {
		return true, "五花牛"
	}

	if IsWuXiaoNiu(numList) {
		return true, "五小牛"
	}

	if IsSiZha(numList) {
		return true, "四炸"
	}

	return false, ""
}

func TransCardNum(openNumberList []uint32) []uint32 {
	resultList := make([]uint32, len(openNumberList))
	for index, item := range openNumberList {
		val := item % 13
		if val == 0 {
			val = 13
		}

		resultList[index] = val
	}

	return resultList
}

func FindMaxCardInList(cardList []uint32) (num uint32, suit uint32) {
	num = 0
	suit = 0

	for _, item := range cardList {
		formatItem := item % 13
		if formatItem == 0 {
			formatItem = 13
		}

		if num == formatItem {
			newSuit := (item - 1) / 13
			if newSuit > suit {
				suit = newSuit
			}
		}

		if num < formatItem {
			num = formatItem
			suit = (item - 1) / 13
		}
	}

	return num, suit
}

func GetNiuniuPoint(numList []uint32) int {
	numList = TransCardNum(numList)

	for i := 0; i < len(numList); i++ {
		if numList[i] > 10 {
			numList[i] = 10
		}
	}

	for i := 0; i < 3; i++ {
		for j := i + 1; j < 4; j++ {
			for k := j + 1; k < 5; k++ {
				num1 := numList[i]
				num2 := numList[j]
				num3 := numList[k]

				if (num1+num2+num3)%10 != 0 {
					continue
				}

				pointTotal := uint32(0)
				for f := 0; f < 5; f++ {
					if f != i && f != j && f != k {
						pointTotal += numList[f]
					}
				}

				if pointTotal%10 == 0 {
					return 10
				}

				return int(pointTotal % 10)
			}
		}
	}

	return 0
}
