import DataModelBase from '../framework/DataModelBase';
import PlayerInfoItem from 'PlayerInfoItem';
import Common from '../common/Common';

// 对局信息，包括玩家信息、房间信息
export default class PlayerInfoModel extends DataModelBase
{
    constructor(modelName)
    {
        super(modelName);

        this.roomId = -1
        this.tableId = -1
        this.selfOpenId = ''
        this.selfSession_key = ''
        this.selfChairId = -1
        this.selfWXPlayerInfo = null
        this.selfPlayerInfo = null
        this.playerInfoItems =  new Array()
        this.vipInfo = []
        this.selfUserID = ''//玩家自己的userid

    }


    setSelfOpenId(openId)
    {
        this.selfOpenId =  openId
    }

    getSelfOpenId()
    {
        return this.selfOpenId
    }

    setSelfSessionKey(session_key)
    {
        this.selfSession_key =  session_key
    }

    getSelfSessionKey()
    {
        return this.selfSession_key
    }

    setSelfChairId(chairId)
    {
        this.chairId = chairId
    }

    getSelfChairId()
    {
        return this.chairId
    }

    setSelfWXPlayerInfo(info )
    {
        this.selfWXPlayerInfo = info
    }

    getSelfWXPlayerInfo()
    {
        return this.selfWXPlayerInfo
    }



    // 登录服务器的玩家信息
    setSelfPlayerInfo(info)
    {
        this.selfPlayerInfo = info
        this.selfUserID = info.dwUserID
    }

    // 登录服务器的玩家信息
    getSelfPlayerInfo()
    {
        return  this.selfPlayerInfo;
    }

    // 游戏桌子的玩家信息
    setTablePlayerInfo(playerInfo)
    {
        // console.log("setTablePlayerInfo:" + JSON.stringify(playerInfo))
        var found  = false
        for (var i = 0; i < this.playerInfoItems.length; ++i)
        {
            if(this.playerInfoItems[i] && this.playerInfoItems[i].dwUserID === playerInfo.dwUserID)
            {
                this.playerInfoItems.slice(i,1,playerInfo)
                found = true
                break
            }
        }

        if(found === false )
        {
            this.playerInfoItems.push(playerInfo)
        }

    }

    removeTablePlayerInfoByUserID(dwUserID)
    {
        if(dwUserID==null)
        {
            console.log("removeTablePlayerInfoBy dwUserID == null")
            return
        }
        for (var i = 0; i < this.playerInfoItems.length; ++i)
        {
            if(this.playerInfoItems[i] && this.playerInfoItems[i].dwUserID === dwUserID)
            {
                this.playerInfoItems.slice(i,1)
                break
            }
        } 
    }

    getSelfTablePlayer()
    {
        cc.log("当前用户ID==",this.selfUserID);
        return this.getTablePlayerInfoByUserID(this.selfUserID)
    }
    

    // getPlayerInfoByCharId(chairId)
    // {
    //     if(chairId >= 0 && chairId <= 3)
    //     {
    //        return this.playerInfoItems[chairId]
    //     }
    // }

    getTablePlayerInfo(openId)
    {
        var playerInfo = null

        for (var i = 0; i < this.playerInfoItems.length; ++i)
        {
            if(this.playerInfoItems[i] && this.playerInfoItems[i].openId === openId)
            {
                playerInfo = this.playerInfoItems[i]
                break
            }
        }

        return playerInfo
    }

    getTablePlayerInfoByUserID(userId)
    {

        var playerInfo = null

        for (var i = 0; i < this.playerInfoItems.length; ++i)
        {
            if(this.playerInfoItems[i] && this.playerInfoItems[i].dwUserID === userId)
            {
                playerInfo = this.playerInfoItems[i]
                break
            }
        }

        return playerInfo
    }

    

    setVipInfo(order,vipInfo)
    {
        this.vipInfo[order] = vipInfo
    }

    getVipInfo(order)
    {
        return this.vipInfo[order]
    }

}
