cc.Class({
    extends: cc.Component,
    properties: {
        account:null,
	    userId:null,
		userName:null,
		lv:0,
		exp:0,
		coins:0,
		gems:0,
		pass:null,
        ip:"",
        sex:0,
        roomData:null,

        id           :null,
        phone        :null,
        playername         :null,
        avatar       :null,
        playeruuid         :null,
        deleted_at   :null,
        created_at   :null,
        updated_at   :null,
        status       :null,
        amount       :null,
        frozen_amount:null,
        status       :null,
        msg          :null,
        
        oldRoomId:null,
        bObserver:false,
    },
    
    guestAuth:function(){
        // var account = cc.args["account"];
        // if(account == null){
        //     account = cc.sys.localStorage.getItem("account");
        // }
        
        // if(account == null){
        //     account = Date.now();
        //     cc.sys.localStorage.setItem("account",account);
        // }

        var onLogin = function(ret){
            if(ret.status !== 1){
                console.log(ret.msg);
            }
            else{
                console.log(ret);
                self.token = ret.data.token
                // self.account = ret.name;
                // self.userId = ret.id;
                // self.avatar = ret.avatar;
                // self.phone = ret.phone;
                // self.amount = ret.amount;
                // self.frozen_amount = ret.frozen_amount;
                cc.director.loadScene("hall");
            }
        };
        
        cc.vv.http.sendRequest("/hideLogin",null,onLogin);
    },
    
    onAuth:function(ret){
        var self = cc.vv.userMgr;
        if(ret.errcode !== 0){
            console.log(ret.errmsg);
        }
        else{
            self.account = ret.account;
            self.sign = ret.sign;
            cc.vv.http.url = "http://" + cc.vv.SI.hall;
            self.login();
        }   
    },
    
    login:function(data){
        var self = this;
        var onLogin = function(ret){
            if(ret.status !== 1){
                console.log(ret.msg);
            }
            else{
                console.log(ret);
                self.token = ret.data.token
                // self.account = ret.name;
                // self.userId = ret.id;
                // self.avatar = ret.avatar;
                // self.phone = ret.phone;
                // self.amount = ret.amount;
                // self.frozen_amount = ret.frozen_amount;
                cc.director.loadScene("hall");
            }
        };
        // cc.vv.wc.show("正在登录游戏");
        cc.vv.http.sendRequest("/sys/user/login",data,onLogin);
    },
    
    create:function(data){
        var self = this;
        var onCreate = function(ret){
            console.log('create: ' + ret.errcode);
            
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                self.account = data['user']
                self.pass = data['pass']
                self.phone = data['usphoneer']
                self.login();
            }
        };
        
        // var data = {
        //     account:this.account,
        //     sign:this.sign,
        //     name:name
        // };
        cc.vv.http.sendRequest("/sys/user/register",data,onCreate);    
    },

    agentLogin:function(gameid,callback){
        var onEnter = function(ret){
            if(ret.status !== 1){
                console.log(ret.msg);
                callback(ret)
            }
            else{
                console.log(ret);
                self.gametoken = ret.data.token
                callback(ret)
                // cc.director.loadScene("hall");
            }
        }
        var data = {
            token :this.token,
            id :gameid
        };
        cc.vv.http.sendRequest("/api/agent/login",data,onEnter)
    },
    
    enterRoom:function(roomId,callback){
        var self = this;
        var serverType = this.getServerTypeByRid(roomId);
        var onEnter = function(ret){
            if(ret.errcode !== 0){
                if(ret.errcode == -1){
                    setTimeout(function(){
                        self.enterRoom(roomId,callback);
                    },5000);
                }
                else{
                    cc.vv.wc.hide();
                    if(callback != null){
                        callback(ret);
                    }
                }
            }
            else{
                cc.vv.wc.hide();
                if(callback != null){
                    callback(ret);
                }
                if(serverType == 'SSS_SERVER_TYPE'){
                    cc.vv.sssNetMgr.connectGameServer(ret);
                }
                else if(serverType == 'SSP_SERVER_TYPE'){
                    cc.vv.sspNetMgr.connectGameServer(ret);
                }
            }
        };
        var isObserver = cc.vv.userMgr.bObserver;
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            serverType:serverType,
            roomid:roomId,
            isObserver:isObserver
        };
        cc.vv.wc.show("正在进入房间 " + roomId);
        cc.vv.http.sendRequest("/enter_private_room",data,onEnter);
    },
    getHistoryList:function(callback){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                console.log(ret.history);
                if(callback != null){
                    callback(ret.history);
                }
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
        };
        cc.vv.http.sendRequest("/get_history_list",data,onGet);
    },
    getSSPGamesOfRoom:function(uuid,callback){
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                console.log(ret.data);
                callback(ret.data);
            }
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            uuid:uuid,
        };
        cc.vv.http.sendRequest("/get_SSPgames_of_room",data,onGet);
    },
    getSSPDetailOfGame:function(uuid,index,callback){ //四色牌
        var self = this;
        var onGet = function(ret){
            if(ret.errcode !== 0){
                console.log(ret.errmsg);
            }
            else{
                console.log(ret.data);
                callback(ret.data);
            }       
        };
        
        var data = {
            account:cc.vv.userMgr.account,
            sign:cc.vv.userMgr.sign,
            uuid:uuid,
            index:index,
        };
        cc.vv.http.sendRequest("/get_detail_of_SSPgame",data,onGet);
    },
    getServerTypeByRid:function(rid){
        if((rid >= 200000)&&(rid < 300000)){
            return "SSS_SERVER_TYPE";
        }
        else if((rid >= 300000)&&(rid < 400000)){
            return "SSP_SERVER_TYPE";
        }
        else{
            return "";
        }
    }
});
