package base

import (
	"mongo.games.com/game/proto"
	hall_proto "mongo.games.com/game/protocol/gamehall"
	server_proto "mongo.games.com/game/protocol/server"
)

var NilPlayer IPlayer = nil

type IPlayer interface {
	Clear()
	GetSnId() int32
	GetPos() int32
	GetFlag() int32
	SetFlag(flag int32)
	GetCoin() int64
	GetLastOp() int32
	SetLastOp(op int32)
	MarkFlag(flag int32)
	UnmarkFlag(flag int32)
	IsMarkFlag(flag int32) bool
	IsOnLine() bool
	IsReady() bool
	IsSceneOwner() bool
	IsRobot() bool
	UpdateCards(cards []int32)
	UpdateBasePlayers(gameId string, basePlayer *server_proto.RobotData)
	GetBasePlayers(gameId string) *server_proto.RobotData
	LeaveGameMsg(snid int32) bool
	GetOutLimitCoin() int
	GetGameCount() int
	GetLastWinOrLoss() int
	GetTakeCoin() int64
}

type BasePlayer struct {
	BasePlayer    map[string]*server_proto.RobotData
	GameCount     int
	LastWinOrLoss int //0 平局  1 赢取  -1 失败
	TakeCoin      int64
	Scene         interface{}
}

func (this *BasePlayer) UpdateBasePlayers(gameId string, basePlayer *server_proto.RobotData) {
	if this.BasePlayer == nil {
		this.BasePlayer = make(map[string]*server_proto.RobotData)
	}
	this.BasePlayer[gameId] = basePlayer
}

func (this *BasePlayer) GetBasePlayers(gameId string) *server_proto.RobotData {
	if this.BasePlayer == nil {
		this.BasePlayer = make(map[string]*server_proto.RobotData)
	}
	if this.BasePlayer[gameId] == nil {
		this.BasePlayer[gameId] = &server_proto.RobotData{}
	}
	return this.BasePlayer[gameId]
}

func (this *BasePlayer) GetLastWinOrLoss() int {
	return this.LastWinOrLoss
}

func (this *BasePlayer) GetGameCount() int {
	return this.GameCount
}

func (this *BasePlayer) GetTakeCoin() int64 {
	return this.TakeCoin
}

func (this *BasePlayer) GetOutLimitCoin() int {
	return -1
}

func (this *BasePlayer) SendMsg(snid int32, packetid int, data interface{}) bool {
	s := PlayerMgrSingleton.GetPlayerSession(snid)
	if s != nil {
		return s.Send(packetid, data)
	}
	return false
}

func (this *BasePlayer) LeaveGameMsg(snid int32) bool {
	s := PlayerMgrSingleton.GetPlayerSession(snid)
	if s != nil {
		pack := &hall_proto.CSLeaveRoom{
			Mode: proto.Int32(0),
		}
		proto.SetDefaults(pack)
		return s.Send(int(hall_proto.GameHallPacketID_PACKET_CS_LEAVEROOM), pack)
	}
	return false
}
