package thirteen

import (
	rule "mongo.games.com/game/gamerule/thirteen"
	"mongo.games.com/game/proto"
	"mongo.games.com/game/protocol/thirteen"
	"mongo.games.com/game/robot/base"
)

type ThirteenWaterPlayer struct {
	base.BasePlayer
	*thirteen.ThirteenPlayerData
	oldCoin      int64
	OpDelayTimes int32         //本局延迟操作次数
	allcardsO    map[int]int32 //玩家所有牌型
	cardsO       *rule.Group   //确定的牌型信息
	maxcardsO    int32         //最大牌的牌型
}

func NewThirteenWaterPlayer(data *thirteen.ThirteenPlayerData) *ThirteenWaterPlayer {
	p := &ThirteenWaterPlayer{ThirteenPlayerData: data}
	p.Init()
	return p
}

func (p *ThirteenWaterPlayer) Init() {
	p.oldCoin = p.GetCoin()
	p.Clear()
}

func (p *ThirteenWaterPlayer) GetMaxCardsO() {
	max := -1000
	for _, v := range p.allcardsO {
		sum := 0

		if v >= 1000000 {
			p.maxcardsO = v
			break
		}
		a := v / 10000
		b := (v - a*10000) / 100
		c := v - a*10000 - b*100
		s := []int32{a, b, c}
		for _, k := range s {
			if k > rule.PokersTypeZero && k <= rule.PokersTypeOne {
				sum += rule.PokersTypeOne - int(k)
			}
		}
		if max <= sum {
			max = sum
			p.maxcardsO = v
		}
	}
}
func (p *ThirteenWaterPlayer) Clear() {
	p.UnmarkFlag(base.PlayerState_Ready)
	p.UnmarkFlag(base.PlayerState_WaitNext)
	p.UnmarkFlag(base.PlayerState_GameBreak)
	p.OpDelayTimes = 0
	p.allcardsO = make(map[int]int32)
	p.cardsO = &rule.Group{Head: [3]int{-1, -1, -1}, Mid: [5]int{-1, -1, -1, -1, -1}, End: [5]int{-1, -1, -1, -1, -1}, PokerType: -1}
	p.maxcardsO = -1
}

func (p *ThirteenWaterPlayer) MarkFlag(flag int32) {
	myFlag := p.GetFlag()
	myFlag |= flag
	p.Flag = proto.Int32(myFlag)
}

func (p *ThirteenWaterPlayer) UnmarkFlag(flag int32) {
	myFlag := p.GetFlag()
	myFlag &= ^flag
	p.Flag = proto.Int32(myFlag)
}

func (p *ThirteenWaterPlayer) IsMarkFlag(flag int32) bool {
	if (p.GetFlag() & flag) != 0 {
		return true
	}
	return false
}

func (p *ThirteenWaterPlayer) IsOnLine() bool {
	return p.IsMarkFlag(base.PlayerState_Online)
}

func (p *ThirteenWaterPlayer) IsReady() bool {
	return p.IsMarkFlag(base.PlayerState_Ready)
}

func (p *ThirteenWaterPlayer) IsSceneOwner() bool {
	return p.IsMarkFlag(base.PlayerState_SceneOwner)
}
func (this *ThirteenWaterPlayer) IsGameing() bool {
	return !this.IsMarkFlag(base.PlayerState_WaitNext) && !this.IsMarkFlag(base.PlayerState_GameBreak)
}

func (p *ThirteenWaterPlayer) IsRobot() bool {
	player := base.PlayerMgrSingleton.GetPlayer(p.GetSnId())
	return player != nil
}

func (p *ThirteenWaterPlayer) SetFlag(flag int32) {
	p.Flag = proto.Int32(flag)
}

func (p *ThirteenWaterPlayer) GetLastOp() int32 {
	return 0
}

func (p *ThirteenWaterPlayer) SetLastOp(op int32) {
}

func (p *ThirteenWaterPlayer) UpdateCards(cards []int32) {
	if p != nil {
		p.Cards = cards
	}
}
