var KBEngine = require("kbengine");

// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
window.app = {
    player : [
        {
            id : 9527,
            name : "9527",
            gender: 0,
            head: "https://dn-qn.move-it.club/FrRjuH4-RslUaAhofSB2NdMn9WMx",
        },
        {
            id : 9526,
            name : "9526",
            gender: 1,
            head: "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=857896439,118415596&fm=27&gp=0.jpg",
        }
    ]
    ,
    callback:function(id,name){
        console.log("1111111111111===",id,name);
        this.player[0].name = name;
    }
}
cc.Class({
    extends: cc.Component,

    properties: {
        //人物数量
        avatarCount: {
            // ATTRIBUTES:
            default: 0,
            type:cc.Integer,
        },
        //是否可以开始
        isCanStart: {
            // ATTRIBUTES:
            default: false,
            type:cc.Integer,
        },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        utils.redytoLoadScene("worldScene")
    },


    start () {
          this.matchText =  UtilsNode.getNode("matchText",this.node)
          //时间相关
          this.matchTextc = this.matchText.getComponent("matchText")


          this.head =  UtilsNode.getNode("head",this.node)
          // 头像相关
          this.headc = this.head.getComponent("head")

          this.click =  UtilsNode.getNode("click",this.node)
          //可以点击的 开始匹配按钮和 取消按钮
          this.clickc =  this.click.getComponent("click")



        this.initListener();

        this.installEvents();

    },

        /* -----------------------------------------------------------------------/
    							KBEngine 事件响应
    /------------------------------------------------------------------------ */
    installEvents: function () {
        KBEngine.Event.register("onAvatarMatchingSuccess", this, "onAvatarMatchingSuccess");
    },
    onAvatarMatchingSuccess:function(id1,name1,id2,name2){
        console.log("onAvatarEnterGame============",id1,name1,id2,name2);
        this.timeId = setTimeout(function () {
            app.player[1].id = id2;
            app.player[1].name = name2;
            this.succeed(app.player[1]);
        }.bind(this),1000)
    },

    initListener: function () {

        //需要 子节点初始化完毕回调里面设置头像等数据
        this.headc.setLoadSucceedLishener(function () {
            this.headc.setHeadData(0,app.player[0]);
        }.bind(this))


        this.clickc.setStartBtnLishener(function () {
            // console.log("开始匹配");
            this.headc.startMatch();
            this.matchTextc.startCountDownTime();

            if (KBEngine.app)
               KBEngine.app.player().joinRoom();

            // this.timeId = setTimeout(function () {
            //     this.succeed(app.player[1]);
            // }.bind(this),2000)
        }.bind(this))
        this.clickc.setCanceltBtnLishener(function () {
            // console.log("停止匹配");
            clearTimeout(this.timeId);

            
            if (KBEngine.app)
                KBEngine.app.player().levelRoom();

            this.headc.cancelMatch();
            this.matchTextc.stopCountDownTime();
        }.bind(this))
    },
    succeed(playerData){

        this.headc.setHeadData(1,playerData);

        this.clickc.succeed();
        this.matchTextc.succeed();
        this.headc.succeedMatch()


        setTimeout(function () {
            utils.toLoadScene("worldScene")   
        }.bind(this),2000)
    },
    // update (dt) {


    // },
});
