package fruits

func (w *WinResult) Init() {
	w.EleValue = make([]int32, 15)
	w.WinLine = nil
}

// 玛丽游戏
func (w *WinResult) InitMary() {
	w.MaryOutSide = -1
	w.MaryMidArray = nil
	w.MaryOutRate = 0
	w.MaryMidRate = 0
	w.MaryLianXu = 0
}

// 正常游戏 免费游戏
func (w *WinResult) CreateLine(ele [][]int32, free bool) {
	w.Init()
	w.result(ele, free)
	//Print(w.EleValue)
}
func (w *WinResult) Win() {
	w.getWinLine()
}

// 玛丽游戏
func (w *WinResult) CreateMary(maryGame [][]int32) {
	w.InitMary()
	w.MaryOutSide = RandSliceInt32IndexByWightN(maryGame[0])
	for i := 0; i < 4; i++ {
		ele := RandSliceInt32IndexByWightN(maryGame[1])
		w.MaryMidArray = append(w.MaryMidArray, ele)
	}
	//fmt.Println("外圈元素", w.MaryOutSide)
	//fmt.Println("内圈元素", w.MaryMidArray)
	w.MaryWin()
}
func (w *WinResult) MaryWin() {
	var outRate int64
	switch w.MaryOutSide {
	case Watermelon:
		outRate += 200
	case Grape:
		outRate += 100
	case Lemon:
		outRate += 70
	case Cherry:
		outRate += 50
	case Banana:
		outRate += 20
	case Apple:
		outRate += 10
	case Pineapple:
		outRate += 5
	}
	var flag = w.MaryMidArray[0]
	var n int32
	for _, v := range w.MaryMidArray {
		if flag != v {
			break
		}
		n++
	}
	for _, v := range w.MaryMidArray {
		if w.MaryOutSide == v {
			w.MaryOutRate = outRate
			break
		}
	}
	if n >= 3 {
		if n == 3 {
			w.MaryMidRate = 20
		} else if n == 4 {
			w.MaryMidRate = 500
		}
		w.MaryLianXu = n
	}
	//fmt.Println("外圈倍率:", w.MaryOutRate)
	//fmt.Println("内圈倍率", w.MaryMidRate)
}

func (w *WinResult) result(ele [][]int32, free bool) {
	sl1 := make(map[int]bool)
	sl2 := make(map[int]bool)
	sl3 := make(map[int]bool)
	n := 0
	for i := 0; i < Column; i++ {
		for j := 0; j < Row; j++ {
			val := RandSliceInt32IndexByWightN(ele[j])
			if val == Scatter {
				if !sl1[j] {
					sl1[j] = true
				} else {
					noScatter := make([]int32, len(ele[j]))
					copy(noScatter, ele[j])
					noScatter[Scatter] = 0
					noScatter[Bonus] = 0
					noScatter[Wild] = 0
					val = RandSliceInt32IndexByWightN(noScatter)
				}
			} else if val == Bonus {
				if !sl2[j] {
					sl2[j] = true
				} else {
					noBonus := make([]int32, len(ele[j]))
					copy(noBonus, ele[j])
					noBonus[Scatter] = 0
					noBonus[Bonus] = 0
					noBonus[Wild] = 0
					val = RandSliceInt32IndexByWightN(noBonus)
				}
			} else if val == Wild {
				if !sl3[j] {
					sl3[j] = true
				} else {
					noWild := make([]int32, len(ele[j]))
					copy(noWild, ele[j])
					noWild[Scatter] = 0
					noWild[Bonus] = 0
					noWild[Wild] = 0
					val = RandSliceInt32IndexByWightN(noWild)
				}
			}
			w.EleValue[n] = val
			n++
		}
	}

	if free {
		//免费 不中 玛丽游戏和奖池
		eleVal := make([]int32, len(w.EleValue))
		copy(eleVal, w.EleValue)
		var wls WinResult
		wls.Init()
		wls.EleValue = eleVal
		wls.Win()
		for _, v := range wls.WinLine {
			flag := v.Lines[0]
			if flag == Scatter {
				noScatter := make([]int32, len(ele[2]))
				copy(noScatter, ele[2])
				noScatter[Wild] = 0
				noScatter[Scatter] = 0
				vv := RandSliceInt32IndexByWightN(noScatter)
				w.EleValue[v.Poss[2]] = vv
			} else {
				wildNum := 0
				for _, el := range v.Lines {
					if el == Wild {
						wildNum++
					} else {
						wildNum = 0
					}
				}
				if wildNum >= 3 {
					noWild := make([]int32, len(ele[2]))
					copy(noWild, ele[2])
					noWild[Wild] = 0
					noWild[Scatter] = 0
					w.EleValue[v.Poss[2]] = RandSliceInt32IndexByWightN(noWild)
				}
			}
		}
	}
}

func (w *WinResult) getWinLine() {
	n := 0
	var flag int32 = -1
	for k, cols := range LineWinNum {
		flag = w.EleValue[cols[0]]
		//Bonus Scatter 不参与线数 Bonus下班单独计算
		if flag == Bonus || flag == Scatter {
			continue
		}
		var line []int32
		var pos []int32
		for _, key := range cols {
			//不计算 Bonus
			if (flag == w.EleValue[key] || Wild == w.EleValue[key] || flag == Wild) && w.EleValue[key] != Bonus &&
				w.EleValue[key] != Scatter {
				if Wild != w.EleValue[key] {
					flag = w.EleValue[key]
				}
				n++
				line = append(line, w.EleValue[key])
				pos = append(pos, int32(key))
			} else {
				if n >= 3 || (flag == Banana && n >= 2) {
					w.WinLine = append(w.WinLine, WinLine{
						Lines:  line,
						Poss:   pos,
						LineId: k + 1,
						Rate:   GetRate(flag, n),
					})
				}
				n = 0
				pos = nil
				line = nil
				break
			}
			if n == 5 {
				w.WinLine = append(w.WinLine, WinLine{
					Lines:  line,
					Poss:   pos,
					LineId: k + 1,
					Rate:   GetRate(flag, n),
				})
				n = 0
				pos = nil
				line = nil
			}
		}
	}

	w.getBonusAndScatter()

	//test code
	//if len(w.WinLine) > 0 {
	//	fmt.Println("====== 赢的总线数 =======", len(w.WinLine))
	//	for k, v := range w.WinLine {
	//		fmt.Print(k+1, "  ")
	//		PrintWin(v.Lines)
	//		fmt.Println(k+1, "位置 ", v.Poss, "  中奖线号:", v.LineId, " 线元素:", v.Lines, " 倍率:", v.Rate)
	//	}
	//}
}
func (w *WinResult) getBonusAndScatter() {
	//只计算Bonus和Scatter
	for k, cols := range LineWinNum {
		var n int
		var line []int32
		var pos []int32
		for l, key := range cols {
			if w.EleValue[key] == Bonus {
				n++
				line = append(line, w.EleValue[key])
				pos = append(pos, int32(key))
			} else {
				if n >= 3 {
					w.WinLine = append(w.WinLine, WinLine{
						Lines:  line,
						Poss:   pos,
						LineId: k + 1,
						Rate:   GetRate(Bonus, n),
					})
				}
				n = 0
				pos = nil
				line = nil
				continue
			}
			if l == 4 {
				if n >= 3 {
					w.WinLine = append(w.WinLine, WinLine{
						Lines:  line,
						Poss:   pos,
						LineId: k + 1,
						Rate:   GetRate(Bonus, n),
					})
					n = 0
					pos = nil
					line = nil
				}
			}
		}
		n = 0
		pos = nil
		line = nil
		for l, key := range cols {
			if w.EleValue[key] == Scatter {
				n++
				line = append(line, w.EleValue[key])
				pos = append(pos, int32(key))
			} else {
				if n >= 3 {
					w.WinLine = append(w.WinLine, WinLine{
						Lines:  line,
						Poss:   pos,
						LineId: k + 1,
						Rate:   GetRate(Scatter, n),
					})
				}
				n = 0
				pos = nil
				line = nil
				continue
			}
			if l == 4 {
				if n >= 3 {
					w.WinLine = append(w.WinLine, WinLine{
						Lines:  line,
						Poss:   pos,
						LineId: k + 1,
						Rate:   GetRate(Scatter, n),
					})
					n = 0
					pos = nil
					line = nil
				}
			}
		}
		n = 0
		pos = nil
		line = nil
		for l, key := range cols {
			if w.EleValue[key] == Wild {
				n++
				line = append(line, w.EleValue[key])
				pos = append(pos, int32(key))
			} else {
				if n >= 3 {
					isHave := false
					for _, i2 := range w.WinLine {
						if i2.LineId == k+1 {
							isHave = true
							break
						}
					}
					if !isHave {
						w.WinLine = append(w.WinLine, WinLine{
							Lines:  line,
							Poss:   pos,
							LineId: k + 1,
							Rate:   GetRate(Wild, n),
						})
					}
				}
				n = 0
				pos = nil
				line = nil
				continue
			}
			if l == 4 {
				if n >= 3 {
					isHave := false
					for _, i2 := range w.WinLine {
						if i2.LineId == k+1 {
							isHave = true
							break
						}
					}
					if !isHave {
						w.WinLine = append(w.WinLine, WinLine{
							Lines:  line,
							Poss:   pos,
							LineId: k + 1,
							Rate:   GetRate(Wild, n),
						})
					}
					n = 0
					pos = nil
					line = nil
				}
			}
		}
	}
}
