package fruits

import (
	"math"

	"mongo.games.com/game/gamerule/fruits"
	"mongo.games.com/game/gamesrv/base"
	"mongo.games.com/game/model"
)

var ElementsParams = [][]int32{}

type FruitsPlayerData struct {
	*base.Player
	roomid        int32 //房间ID
	result        *fruits.WinResult
	betIdx        int               //下注索引
	betCoin       int64             //下注金额
	oneBetCoin    int64             //单注
	cpCtx         model.CoinPoolCtx //水池环境
	leaveTime     int32             //离开时间
	winCoin       int64             //本局输赢
	freeTimes     int               //免费游戏
	maryFreeTimes int               //玛丽游戏
	gameState     int               //当前游戏模式
	///当局数值
	winLineRate       int64 //线倍率
	JackPotRate       int64 //获取奖池的百分比 5% 10% 15%
	JackPot7Rate      int64 //元素jackpot的倍率
	freeWinTotalRate  int64 //免费游戏赢的倍率
	maryWinTotalRate  int64 //玛丽游戏赢的倍率
	winNowJackPotCoin int64 //当局奖池爆奖
	winNowAllRate     int64 //当局赢得倍率
	//////////////////////////
	//统计数值
	winLineCoin    int64 //线赢金额
	winJackPotCoin int64 //爆奖
	winEle777Coin  int64 //元素数量
	winFreeCoin    int64 //免费游戏
	//winMaryCoin    int64 //玛丽游戏
	winOutMaryCoin int64 //玛丽游戏外圈
	winMidMaryCoin int64 //玛丽游戏内圈
	winFreeTimes   int   //当局获得的免费次数
	//log
	nowFreeTimes int //当前第几轮免费游戏
	nowMaryTimes int //当前第几轮小玛丽游戏
	wl           []model.FruitsWinLine
	maryInNum    int //玛丽游戏连续数量
	taxCoin      int64
	//调控
	preWinRate   int64
	preNeedRate  int64
	pre30WinRate int64
	preEleVal    []int32
	//mary
	preMaryOutSide  int32
	preMaryMidArray []int32
	noWinTimes      int
	//
	startCoin int64
	testNum   int
	testIdx   int
	isf       bool
	freeTotal int
	weightPos int32 //当局权重
}

func (p *FruitsPlayerData) init() {
	p.roomid = 0
	p.result = new(fruits.WinResult)
}
func (p *FruitsPlayerData) Clear() {
	p.gameState = fruits.Normal
	p.startCoin = p.Coin
	p.winCoin = 0
	p.JackPotRate = 0
	p.JackPot7Rate = 0
	p.winLineRate = 0
	p.maryWinTotalRate = 0
	p.freeWinTotalRate = 0
	p.winNowAllRate = 0
	p.winNowJackPotCoin = 0
	p.winFreeTimes = 0
	p.maryInNum = 0
	p.taxCoin = 0
	if p.freeTimes == 0 {
		p.winLineCoin = 0
		p.winFreeCoin = 0
		p.winJackPotCoin = 0
		p.winEle777Coin = 0
		p.nowFreeTimes = 0
	}
	if p.maryFreeTimes == 0 {
		//p.winMaryCoin = 0
		p.winOutMaryCoin = 0
		p.winMidMaryCoin = 0
		p.nowMaryTimes = 0
		p.winOutMaryCoin = 0
		p.winMidMaryCoin = 0
	}
	p.weightPos = 0
}
func (p *FruitsPlayerData) TestCode(eleLineAppearRate [][]int32, sceneEx *FruitsSceneData) bool {
	//if sceneEx.DbGameFree.GetId() == 3060004 {
	//	p.result.CreateLine(eleLineAppearRate, p.gameState == fruits.FreeGame)
	//	if p.testIdx == 1 {
	//		//test mary
	//		if p.testNum%5 == 0 && p.gameState != fruits.FreeGame {
	//			for i := 0; i < 5; i++ {
	//				p.result.EleValue[i] = fruits.Wild
	//			}
	//			p.testIdx = 2
	//		}
	//		p.testNum++
	//		if p.testNum >= 100000 {
	//			p.testNum = 0
	//		}
	//		if p.gameState == fruits.FreeGame && !p.isf {
	//			p.isf = true
	//			for i := 0; i < 5; i++ {
	//				p.result.EleValue[i] = fruits.Bonus
	//			}
	//		}
	//	} else if p.testIdx == 0 {
	//		//test free
	//		if p.testNum%5 == 0 && p.gameState == fruits.Normal {
	//			for i := 0; i < 5; i++ {
	//				p.result.EleValue[i] = fruits.Bonus
	//			}
	//			p.testIdx = 1
	//			p.isf = false
	//		}
	//		if p.gameState == fruits.Normal {
	//			p.testNum++
	//			if p.testNum >= 100000 {
	//				p.testNum = 0
	//			}
	//		}
	//	} else {
	//		if p.testNum%5 == 0 && p.gameState == fruits.Normal {
	//			for i := 0; i < 5; i++ {
	//				p.result.EleValue[i] = fruits.Bonus
	//				p.result.EleValue[i+5] = fruits.Wild
	//			}
	//			p.testIdx = 0
	//		}
	//		if p.gameState == fruits.Normal {
	//			p.testNum++
	//			if p.testNum >= 100000 {
	//				p.testNum = 0
	//			}
	//		}
	//	}
	//	p.result.Win()
	//	return true
	//}
	return false
}

// 正常游戏 免费游戏
func (p *FruitsPlayerData) CreateResult(eleLineAppearRate [][]int32, sceneEx *FruitsSceneData) {
	//if len(p.preEleVal) > 0 {
	//	p.result.EleValue = make([]int32, 15)
	//	copy(p.result.EleValue, p.preEleVal)
	//	p.preEleVal = nil
	//}

	if !p.TestCode(eleLineAppearRate, sceneEx) {
		for i := 0; i < 10; i++ {
			p.result.CreateLine(eleLineAppearRate, p.gameState == fruits.FreeGame)
			p.result.Win()
			var isScatter bool
			var isNeed bool = true
			for _, wl := range p.result.WinLine {
				flag := wl.Lines[0]
				if flag == fruits.Scatter {
					isScatter = true
					JackPotVal := sceneEx.jackpot.GetTotalSmall()
					if JackPotVal > 0 {
						var winjackpot int64
						switch len(wl.Poss) {
						case 3:
							winjackpot = int64(math.Ceil(float64(JackPotVal) * 0.1 / float64(fruits.NowByte)))
						case 4:
							winjackpot = int64(math.Ceil(float64(JackPotVal) * 0.2 / float64(fruits.NowByte)))
						case 5:
							winjackpot = int64(math.Ceil(float64(JackPotVal) * 0.4 / float64(fruits.NowByte)))
						}
						if winjackpot < int64(sceneEx.DbGameFree.GetJackpotMin())*fruits.NowByte {
							isNeed = false
							break
						}
					}
				}
			}
			if !isScatter || (isScatter && isNeed) {
				break
			}
		}
	}
	//线倍率
	var rate int64
	for _, wl := range p.result.WinLine {
		//Wild 元素 按线的连续给玛丽游戏
		flag := wl.Lines[0]
		var NowWildNum int
		var MaxLinXu int
		for _, n := range wl.Lines {
			if flag != n && flag == fruits.Wild {
				flag = n
			}
			if n == fruits.Wild {
				NowWildNum++
				if NowWildNum > MaxLinXu {
					MaxLinXu = NowWildNum
				}
			} else {
				NowWildNum = 0
			}
		}
		if MaxLinXu >= 3 {
			newM := model.FruitsWinLine{
				Id:       wl.LineId,
				EleValue: fruits.Wild,
				Num:      MaxLinXu,
				Rate:     0,
				WinCoin:  0,
			}
			if MaxLinXu == 3 {
				p.maryFreeTimes += 1
				newM.WinFreeGame = 0
			} else if MaxLinXu == 4 {
				p.maryFreeTimes += 2
				newM.WinFreeGame = 1
			} else if MaxLinXu >= 5 {
				p.maryFreeTimes += 3
				newM.WinFreeGame = 2
			}
			p.wl = append(p.wl, newM)
		}

		//统计线倍率
		rate += wl.Rate

		//Bonus 元素 按线给倍率 按线给免费
		if flag == fruits.Bonus {
			n := len(wl.Lines)
			newL := model.FruitsWinLine{
				Id:       wl.LineId,
				EleValue: fruits.Bonus,
				Num:      n,
				Rate:     wl.Rate,
				WinCoin:  p.oneBetCoin * wl.Rate,
			}
			if n == 3 {
				p.freeTimes += 5
				p.winFreeTimes += 5
				newL.WinFreeGame = 3
			} else if n == 4 {
				p.freeTimes += 10
				p.winFreeTimes += 10
				newL.WinFreeGame = 4
			} else if n == 5 {
				p.freeTimes += 20
				p.winFreeTimes += 20
				newL.WinFreeGame = 5
			}
			p.wl = append(p.wl, newL)
		} else if flag == fruits.Scatter {
			JackPotVal := sceneEx.jackpot.GetTotalSmall()
			if JackPotVal > 0 {
				var winjackpot int64
				switch len(wl.Poss) {
				case 3:
					p.JackPotRate += 10
					winjackpot = int64(math.Ceil(float64(JackPotVal) * 0.1 / float64(fruits.NowByte)))
				case 4:
					p.JackPotRate += 20
					winjackpot = int64(math.Ceil(float64(JackPotVal) * 0.2 / float64(fruits.NowByte)))
				case 5:
					p.JackPotRate += 40
					winjackpot = int64(math.Ceil(float64(JackPotVal) * 0.4 / float64(fruits.NowByte)))
				}
				p.wl = append(p.wl, model.FruitsWinLine{
					Id:          wl.LineId,
					EleValue:    fruits.Scatter,
					Num:         len(wl.Poss),
					Rate:        wl.Rate,
					WinCoin:     winjackpot,
					WinFreeGame: -1,
				})
				p.JackPot7Rate += wl.Rate
				p.winNowJackPotCoin += winjackpot
			}
		}

	}
	//赢的线钱
	p.winLineCoin += p.oneBetCoin * rate
	p.winLineRate = rate
	//元素输赢
	p.winEle777Coin += p.oneBetCoin * p.JackPot7Rate
}

// 玛丽游戏
func (p *FruitsPlayerData) CreateMary() {
	//if len(p.preMaryMidArray) > 0 {
	//	p.result.MaryMidArray = make([]int32, 4)
	//	copy(p.result.MaryMidArray, p.preMaryMidArray)
	//	p.result.MaryOutSide = p.preMaryOutSide
	//	p.preMaryMidArray = nil
	//}

	//idx := p.result.MaryOutSide
	//maryOutSide := fruits.MaryEleArray[idx]

	//if maryOutSide == fruits.Bomb {
	//	p.maryFreeTimes = 0
	//	rate = 0
	//}

	if p.result.MaryOutSide == fruits.Bomb {
		p.maryFreeTimes--
		if p.maryFreeTimes < 0 {
			p.maryFreeTimes = 0
		}
	}

	p.maryWinTotalRate = p.result.MaryOutRate + p.result.MaryMidRate
	p.winOutMaryCoin += p.oneBetCoin * p.result.MaryOutRate
	p.winMidMaryCoin += p.oneBetCoin * p.result.MaryMidRate
	//p.winMaryCoin += p.winOutMaryCoin + p.winMidMaryCoin
}
