// 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

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        let layout = UtilsNode.getNode("layout",this.node);
        let one = UtilsNode.getNode("one",layout);
        let two = UtilsNode.getNode("two",layout);
        let reset  = this.resetbutton = UtilsNode.getNode("reset",layout);
        app.titlelayout = this;
        UtilsNode.setOn(one,function () {
            app.bg.one();
        }.bind(this));
        UtilsNode.setOn(two,function () {
            app.bg.two();
        }.bind(this));
        UtilsNode.setOn(reset,function () {
            app.bg.reset();
            this.hideButton(false);
        }.bind(this));
        let info = this.info = UtilsNode.getNode("info",layout);

        let info1  = UtilsNode.getNode("info1",info);
        let info2  = UtilsNode.getNode("info2",info);

        // this.x = this.info.x;
        // this.y = this.info.y;



        this.infos = [];

        this.infos.push(info1);
        this.infos.push(info2);

        this.infosp = [];

        this.infosp.push({x:info1.x,y:info1.y});
        this.infosp.push({x:info2.x,y:info2.y});




        let title = this.title = UtilsNode.getNode("title",layout);
        let progresslayout = UtilsNode.getNode("progresslayout",title);
        let progress1 = UtilsNode.getNode("progress1",progresslayout);
        let progress2 = UtilsNode.getNode("progress2",progresslayout);
        let player_showbg1 = UtilsNode.getNode("player_showbg1",progresslayout);
        let player_showbg2 = UtilsNode.getNode("player_showbg2",progresslayout);

        this.playerprogress = [];
        this.playerprogress.push(progress1);
        this.playerprogress.push(progress2);

        this.player_showbgs = [];
        this.player_showbgs.push(player_showbg1);
        this.player_showbgs.push(player_showbg2);

        this.fristEndTag = false;



    },
    //重置
    _init : function(){
        this.showinfoinit();
        this.setInitProgress();
    },


    // startGame : function () {
    //     this.resetbutton.active = false;
    // },
    hideButton : function (b) {
        if (b) {
            setTimeout(function () {
                this.resetbutton.active = b;
            }.bind(this),1000)
        }else{
            this.resetbutton.active = b;
        }



        // this.showinfoinit();
    },
    showinfo: function (b,type,num) {


        if (!b) {

            this.infos[type].active = b;
            return;
        }else{
            if (!this.infos[type].active) {
                this.infos[type].active = b;
                this.info.stopAllActions();
                this.infos[type].x = this.infosp[type].x;
                this.infos[type].y = this.infosp[type].y;
                let myAction = cc.repeatForever(cc.sequence(cc.moveBy(0.5, cc.v2(0,-20)),cc.moveBy(0.5, cc.v2(0,+20))));
                this.infos[type].runAction(myAction);
            }
            this.infosTitle = [];

            let infobg_lose = UtilsNode.getNode("infobg_lose",this.infos[type]);
            let infobg_win = UtilsNode.getNode("infobg_win",this.infos[type]);


            // this.infosTitle.push(infobg_win);
            // this.infosTitle.push(infobg_lose);
                let text = UtilsNode.getNode("text",this.infos[type]);
                let title = "";
                if (type == 0) {
                    // title = "对手落后"+num+"步";
                    title = num;
                    infobg_lose.active = false;
                    infobg_win.active = true;
                }else if (type == 1) {
                    // title = "你落后"+num+"步";
                    title = num;
                    infobg_lose.active = true;
                    infobg_win.active = false;
                }

                text.getComponent(cc.Label).string = title;

        }


    },
    showinfoinit: function () {

        for (var i = 0; i <  this.infos.length; i++) {
            this.infos[i].active = false;
        }


    },
    setProgress : function (playertype,num,max) {

        let temp = UtilsNode.getPercentage(num,max);
        this.playerprogress[playertype].getComponent(cc.Label).string = temp+"%";
        // console.log("当前进来是啥",playertype,num,max,"百分比",UtilsNode.getPercentage(num,max));
        if (temp>=80) {
            this.setFristEnd(playertype);
        }
    },

    setInitProgress : function () {
        for (let i = 0; i <this.playerprogress.length ; i++) {
            this.playerprogress[i].getComponent(cc.Label).string = 0+"%";
        }
    },
    //只显示一次的 快到终点提示
    setFristEnd : function (type) {
        // this.player_showbgs
        if (!this.fristEndTag) {
            if (type == 0) {
                app.bgm.playkuaidaozhongdian();
            }else{
                app.bgm.playkuaidaozhongdian();
            }

            this.fristEndTag = true;
            this.player_showbgs[type].active = true;
            setTimeout(function () {
                this.player_showbgs[type].active = false;
            }.bind(this),2000)
        }
    },
    // update (dt) {},
});
