var mogobe = require("../Mgobe")
cc.Class({
    extends: cc.Component,

    properties: {
        
        animationBg: {
            default: null,
            type:cc.Node,
        },
        player1: {
            default: null,
            type:cc.Node,
        },
        player2: {
            default: null,
            type:cc.Node,
        },
        player3: {
            default: null,
            type:cc.Node,
        },
        player4: {
            default: null,
            type:cc.Node,
        },
        titleTxt: {
            default: null,
            type:cc.Node,
        },
        timeTxt: {
            default: null,
            type:cc.Node,
        },
        waitTxt: {
            default: null,
            type:cc.Node,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.init();

        return cc.loader.loadRes("/cacert", cc.Asset, (err, asset) =>
        {
            console.log("加载证书结束 " + (!err));

            if (err) {
                return;
            }

            mogobe.cacertNativeUrl = asset.nativeUrl;

            mogobe.initSDK(function () {
                mogobe.matchPlayers();
            }.bind(this));
        });
        // mogobe.initSDK();
        // mogobe.matchPlayers(1);
        
        // webView.init(function () {
        //     this.initPlayer1();
        // }.bind(this));

        let interval = 1;  // 以秒为单位的时间间隔let
        let repeat = cc.macro.REPEAT_FOREVER;  // 重复次数
        let delay = 0;  // 开始延时

        this.countTime = this.schedule(function()
        {
            this.timeTxt.getComponent(cc.Label).string = this.updateTime(this.matchTime);
            this.matchTime++;

            if (this.matchTime>15)
            {
                this.unschedule(this.countTime);
                this.titleTxt.getComponent(cc.Label).string = "匹配成功";
                this.timeTxt.active = false;
                this.waitTxt.active = false;

                if (this.nowPlayerNum<4){
                    //Test//
                    // webView.getAiInfo(function () {
                    //     this.resetUIByData({ webView.rivalAvatarUrl,
                    //         webView.rivalUserName,
                    //         webView.rivalGender,});
                    // }.bind(this));
                }
            }

        }.bind(this), interval, repeat, delay);
    },
    init () {
        this.matchTime = 0;
        this.nowPlayerNum = 1;

        this.name1 = this.player1.getChildByName('Name');
        this.gender1 = this.player1.getChildByName('Gender');
        this.avatarSp1 = this.player1.getChildByName('Mask').getChildByName('AvatarSp');
    },
    setGender(gender,bBoy)
    {
        this.iconBoy = gender.getChildByName('IconBoy');
        this.iconGirl = gender.getChildByName('IconGirl');
        if(bBoy)
        {
            this.iconBoy.active = true;
            this.iconGirl.active = false;
        }
        else
        {
            this.iconBoy.active = false;
            this.iconGirl.active = true;
        }
    },
    initPlayer1() {
        this.name1.getComponent(cc.Label).string = webView.userName;
        this.setGender(this.gender1,webView.gender);
        cc.loader.load(webView.avatarUrl, function (err, texture)
        {
            let frame = new cc.SpriteFrame(texture);
            if (err) {
                console.log('头像:', err);
            }
            this.avatarSp1.getComponent(cc.Sprite).spriteFrame = frame;
        }.bind(this));
    },
    updateTime(t) {
        let theTime = Math.floor(t);
        let theTime1 = 0;// 分

        if (theTime > 60)
        {
            theTime1 = parseInt(theTime / 60);
            theTime = parseInt(theTime % 60);
        }
        let result = '';
        
        result = (theTime1 < 10 ? "0" + theTime1 : theTime1) + ":" + (theTime < 10 ? "0" + theTime : theTime);
        return result;
    },
    initOtherPlayer(data)
    {
        if(!data) return;

        this.nowPlayerNum++;
        
        let player = this["player"+this.nowPlayerNum];
        let name = player.getChildByName('Name');
        let gender = player.getChildByName('Gender');
        let avatarSp = player.getChildByName('Mask').getChildByName('AvatarSp');

        player.getChildByName("DotAnimation").active = false;
        name.getComponent(cc.Label).string = data.userName;
        this.setGender(gender,data.gender);

        cc.loader.load(data.avatarUrl, function (err, texture)
        {
            let frame = new cc.SpriteFrame(texture);
            if (err) {
                console.log('头像:', err);
            }
            avatarSp.getComponent(cc.Sprite).spriteFrame = frame;

            if (this.nowPlayerNum==4){
                this.scheduleOnce(()=>
                {
                    cc.director.loadScene("Game");
                }, 4)
            }
        
        }.bind(this));
    },
});
