cc.Class({
    extends: cc.Component,

    properties: {
        //中间加分条
        UIPKScore: {
            default: null,
            type: cc.Prefab
        },
        //胜利
        UIWin: {
            default: null,
            type: cc.Prefab
        },
        //失败
        UILose: {
            default: null,
            type: cc.Prefab
        },
        //撒花
        Fireworks: {
            default: null,
            type: cc.Prefab
        },
        //完美
        UIPerfect: {
            default: null,
            type: cc.Prefab
        },
        //左面距离头像
        LeftRivalHead: {
            default: null,
            type: cc.Prefab
        },
        //右面距离头像
        RightRivalHead: {
            default: null,
            type: cc.Prefab
        },
        //跳远 扔标枪 的计数板
        UIDistance: {
            default: null,
            type: cc.Prefab
        },
        //底部普通加速带提示
        TipOfNormalAcceleration: {
            default: null,
            type: cc.Prefab
        },
        //底部长加速带提示
        TipOfLongAcceleration: {
            default: null,
            type: cc.Prefab
        },
        ReadyGoPrefab: {
            default: null,
            type: cc.Prefab
        },

        RivelHeadImage: null,

        UITop: null,

        VideoStatesLeft: cc.Node,
        VideoStatesRight: cc.Node,

        MyHead: cc.Node,
        RiveelHead: cc.Node,
    },

    onLoad() {
        this.UITop = cc.find('Canvas/UITop');
    },

    // start() {
    // this.setTopMyScore("9");
    // this.setTopRivalScore("9");
    // this.hideTimerPerfect(3);
    // this.hideTimerMiddlePKScore("7","8",5);
    // this.hideTimerDistanceNum("121.00",8);
    // this.hideTimerTimeNum("8.20",2)

    // },
    setScoreNumImage: function (ScoreNum, numNode) {
        var imgName = "";
        if (ScoreNum <= 5) {
            imgName = "Nums/0to3White/" + ScoreNum;
        }
        var self = numNode;
        cc.loader.loadRes(imgName, cc.SpriteFrame, function (err, spriteFrame) {
            self.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        });
    },
    setMiddleMyScore: function (aPrefab, myScore) {//设置自己的分数（图片中部）
        this.setScoreNumImage(myScore, aPrefab.getChildByName('MiddleMyScore'));
    },
    setMiddleRivalScore: function (aPrefab, rivalScore) {//设置对手的分数（图片中部）
        this.setScoreNumImage(rivalScore, aPrefab.getChildByName('MiddleRivalScore'));
    },
    setTopMyScore: function (ScoreNum) {//设置顶部自己分数图片string
        this.UITop.getChildByName('TopMyScore').getComponent('TopScore').setScoreNumImage(ScoreNum);
    },
    setTopRivalScore: function (ScoreNum) {//设置顶部对手PK分数图片string
        this.UITop.getChildByName('TopRivalScore').getComponent('TopScore').setScoreNumImage(ScoreNum);
    },
    setDistanceNum: function (distanceNum) {//成绩：米数string
        cc.find('UIDistance').getChildByName('DistanceNum').getComponent(cc.Label).string = distanceNum + "M";
    },
    setTimeNum: function (secondNum) {//成绩：秒数string
        cc.find('UIDistance').getChildByName('TimeNum').getComponent(cc.Label).string = secondNum + "s";
    },
    //以上是设置数值，以下是设置某UI显示某秒后消失的计时器
    setUIActiveTimer: function (actor, timerSecond) {//UI显示消失计时器
        actor.active = true;
        /**
         * timerSecond 表示此UI显示timerSecond秒后消失。当timerSecond为0的时候，此UI一直显示，不会消失。
         */
        if (timerSecond > 0) {
            this.scheduleOnce(function () {
                actor.active = false;
            }, timerSecond);
        }
    },
    setUIActiveTimer_destroy: function (actor, timerSecond) {//UI显示消失计时器
        /**
         * timerSecond 表示此UI显示timerSecond秒后消失。当timerSecond为0的时候，此UI一直显示，不会消失。
         */
        if (actor == null) {
            return;
        }
        if (timerSecond > 0) {
            this.scheduleOnce(function () {
                actor.destroy();
            }, timerSecond);
        }
    },
    setUIActiveShow: function setUIActiveTimer(actor) {
        //UI显示消失计时器
        actor.active = true;
    },
    displayTimerMiddlePKScore: function (myScore, rivalScore, timerSecond) {
        // console.log("my is "+myScore+" and rival is "+rivalScore);
        var aPrefab = cc.instantiate(this.UIPKScore);
        aPrefab.setOpacity(0);
        aPrefab.parent = cc.director.getScene();//中部PK积分显示后隐藏
        // console.log("This scene is "+cc.director.getScene().name);
        aPrefab.anchorX = 0;
        this.setMiddleMyScore(aPrefab, myScore);
        this.setMiddleRivalScore(aPrefab, rivalScore);
        this.setUITimer(aPrefab, timerSecond);
        aPrefab.setOpacity(255);
        // this.scheduleOnce(function() {
        //     aPrefab.setOpacity(255);
        // }, 0.5);

    },
    hideMiddlePKScore: function () {
        var UIScore = cc.find('UIPKScore');
        if (UIScore) {
            var animState = UIScore.getComponent(cc.Animation).play('UIPKScoreFadeOut');
            var duration = animState.duration;
            this.scheduleOnce(function () {
                // console.log('UIScore=',UIScore.name);
                UIScore.destroy();
            }, duration);
        }
    },
    hideTimerDistanceNum: function (distanceNum, timerSecond) {//成绩米数显示后隐藏
        this.setDistanceNum(distanceNum);
        this.setUIActiveTimer(cc.find('UIDistance'), timerSecond)
    },
    hideTimerTimeNum: function (timeNum, timerSecond) {//成绩秒数显示后隐藏
        this.setDistanceNum(timeNum);
        this.setUIActiveTimer(cc.find('UIDistance'), timerSecond)
    },
    hideTimerPerfect: function (timerSecond) {//pererfect显示后隐藏
        if (cc.find('UIPerfect') != null) {
            cc.find('UIPerfect').destroy();
        }
        var aPrefab = cc.instantiate(this.UIPerfect);
        aPrefab.parent = cc.director.getScene();//中部PK积分显示后隐藏
        // aPrefab.anchorY = 0.5;
        this.setUITimer(aPrefab, timerSecond);

        // cc.find('UIPerfect').active = true;
        aPrefab.getChildByName('perfect').getComponent(cc.Animation).play('PerfectMoveUp');
    },
    displayRivalHead: function (isLeft) {//当对手与自己不在一个屏幕的时候，显示对手头像
        if (isLeft) {
            if (cc.find("RightRivalHead") != null) {
                cc.find('RightRivalHead').destroy();
            }
            if (cc.find("LeftRivalHead") == null) {
                // cc.find('LeftRivalHead').destroy();
                var aPrefab = cc.instantiate(this.LeftRivalHead);
                aPrefab.parent = cc.director.getScene();
                aPrefab.getChildByName('HeadMask').getChildByName('tx').getComponent(cc.Sprite).spriteFrame = this.RivelHeadImage;
            }
        } else {
            if (cc.find("LeftRivalHead") != null) {
                cc.find('LeftRivalHead').destroy();
            }
            if (cc.find("RightRivalHead") == null) {
                // cc.find('LeftRivalHead').destroy();
                var aPrefab = cc.instantiate(this.RightRivalHead);
                aPrefab.parent = cc.director.getScene();
                aPrefab.getChildByName('HeadMask').getChildByName('tx').getComponent(cc.Sprite).spriteFrame = this.RivelHeadImage;
            }
        }

        // cc.find("LeftRivalHead").active = isLeft;
        // cc.find("RightRivalHead").active = !isLeft;
    },
    hideRivalHead: function () {//当对手与自己在一个屏幕的时候，隐藏对手头像
        if (cc.find("LeftRivalHead") != null) {
            cc.find('LeftRivalHead').destroy();
        }
        if (cc.find("RightRivalHead") != null) {
            cc.find('RightRivalHead').destroy();
        }
        // cc.find("LeftRivalHead").active = false;
        // cc.find("RightRivalHead").active = false;
    },
    displayUIWin: function () {//显示胜利界面
        var aPrefab = cc.instantiate(this.UIWin);
        aPrefab.parent = cc.director.getScene();//中部PK积分显示后隐藏
        // cc.find('UIWin').active = true;
    },
    displayUILose: function () {//显示失败界面
        var aPrefab = cc.instantiate(this.UILose);
        aPrefab.parent = cc.director.getScene();//中
        // cc.find('UILose').active = true;
    },
    displayUIDistance: function () {//显示跳远 扔标枪

        var aPrefab = cc.instantiate(this.UIDistance);
        if (cc.find("UIDistance") != null) {
            return aPrefab
        }
        aPrefab.parent = cc.director.getScene();//中
        // cc.find('UILose').active = true;
        return aPrefab;
    },
    hideUIDistance: function () {//当对手与自己在一个屏幕的时候，隐藏对手头像
        if (cc.find("UIDistance") != null) {
            cc.find('UIDistance').destroy();
        }
    },
    playFireworks: function () {
        // cc.find('Fireworks').active = true;
        var aPrefab = cc.instantiate(this.Fireworks);
        aPrefab.parent = cc.director.getScene();//
    },

    hideTimerTipOfNormalAcceleration: function (timerSecond) {//开局时加速带提示
        var aPrefab = cc.instantiate(this.TipOfNormalAcceleration);
        aPrefab.parent = cc.director.getScene();
        aPrefab.anchorX = 0;
        aPrefab.x = 360;
        aPrefab.y = 640;
        this.setUITimer(aPrefab, timerSecond);
    },
    hideTimerTipOfLongAcceleration: function (timerSecond) {//长加速带提示
        var aPrefab = cc.instantiate(this.TipOfLongAcceleration);
        aPrefab.parent = cc.director.getScene();
        aPrefab.anchorX = 0;
        aPrefab.x = 360;
        aPrefab.y = 640;
        this.setUITimer(aPrefab, timerSecond);
    },

    setImage_Player1: function (src) {
        cc.loader.load({ url: src, type: 'jpg' }, function (err, texture) {
            var frame = new cc.SpriteFrame(texture);
            if (err) {
                console.log('玩家1的头像:', err);
            }

            //cc.find('Canvas/UITop/MyHeadMask').getChildByName('MyHead').getComponent(cc.Sprite).spriteFrame = frame;
            this.MyHead.getComponent(cc.Sprite).spriteFrame = frame;
        }.bind(this));
    },
    setImage_Player2: function (src) {
        // console.log('玩家2的头像：',src);
        // var self = this;
        cc.loader.load({ url: src, type: 'jpg' }, function (err, texture) {
            var frame = new cc.SpriteFrame(texture);
            if (err) {
                console.log('玩家2的头像:', err);
            }
            // cc.find('Canvas/UITop/RivalHeadMask').getChildByName('RivalHead').getComponent(cc.Sprite).spriteFrame = frame;
            this.RiveelHead.getComponent(cc.Sprite).spriteFrame = frame;
            this.RivelHeadImage = frame;
        }.bind(this));
    },
    setName_Player1: function (name) {
        // cc.find('UITop').getChildByName('MyName').getComponent(cc.Label).String = name;
        var MyName = this.UITop.getChildByName('MyName');
        var myLabel = MyName.getComponent(cc.Label);
        // myLabel.string = this.subString('HH昵称最多七1个字',14);
        myLabel.string = this.subString(name, 14);
    },
    setName_Player2: function (name) {
        //todo 设置文字的坑
        // cc.find('UITop').getChildByName('RivalName').getComponent(cc.Label).String  = name;
        var RivalName = this.UITop.getChildByName('RivalName');
        var myLabel = RivalName.getComponent(cc.Label);
        myLabel.string = this.subString(name, 14);
    },
    subString: function (str, subNum) {//截取str的subNum个字符。中文算2个，中文之外算1个。
        var currentNum = 0;
        var subStr = '';
        for (var i = 0; i < str.length; i++) {
            if (currentNum <= subNum) {
                if (currentNum + 3 > subNum) {
                    subStr += "...";
                    break;
                }
                else {
                    var strI = str[i];
                    if (/^[\u4e00-\u9fa5]*$/.test(strI)) {//中文字符
                        currentNum += 2;
                        subStr += strI;
                        // cc.log(strI+'++++++++++');
                    }
                    else {//除了中文外的其它字符
                        currentNum += 1;
                        subStr += strI;
                        // cc.log(strI+'*********');
                    }
                }
            }
        }
        if (str.length) {

        }
        return subStr;
    },
    setName_Gender1: function (name) {
        // cc.find('UITop').getChildByName('MyName').getComponent(cc.Label).String = name;
        var MyName = this.UITop.getChildByName('GenderPlayer1');
        var src = "nan";
        if (name == "1") {

        } else {
            src = "nv";
        }
        cc.loader.loadRes(src, cc.SpriteFrame, function (err, spriteFrame) { //这个方法
            MyName.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        });

        // cc.loader.load(src,function (err, texture) {
        //
        //     var frame=new cc.SpriteFrame(texture);
        //     MyName.getComponent(cc.Sprite).spriteFrame=frame;
        // });
    },
    setName_Gender2: function (name) {
        //todo 设置文字的坑
        // cc.find('UITop').getChildByName('RivalName').getComponent(cc.Label).String  = name;
        var MyName = this.UITop.getChildByName('GenderPlayer2');
        var src = "nan";
        if (name == "1") {

        } else {
            src = "nv";
        }
        cc.loader.loadRes(src, cc.SpriteFrame, function (err, spriteFrame) { //这个方法
            MyName.getComponent(cc.Sprite).spriteFrame = spriteFrame;
        });

    },


    setUITimer: function (actor, timerSecond) {//UI显示消失计时器
        // actor.active=true;
        /**
         * timerSecond 表示此UI显示timerSecond秒后消失。当timerSecond为0的时候，此UI一直显示，不会消失。
         */
        if (timerSecond > 0) {
            this.scheduleOnce(function () {
                if (actor.name != "") {
                    actor.destroy();
                    // console.log(actor.name+" destroy.");
                }
            }, timerSecond);
        }
    },

    resetGame: function () {//再来一次游戏
        var Hero = cc.find("Hero");
        var NetworkSocket = Hero.getComponent('NetworkSocket');
        NetworkSocket.endGame();
        // if (cc.director.isPaused()) {
        //     //暂停游戏
        //     cc.director.resume();
        // } else {
        //     //暂停游戏
        //     cc.director.pause();
        // }

        //测试切换头像
        // var GameStates = cc.find('Canvas').getComponent('GameStates');
        // if (GameStates.bServer) {
        //     console.log(111);

        //     var randomSkin = cc.find("randomSkin");
        //     randomSkin.getComponent("upDateSkin").setRandomSkinss();
        // }
        //重新读取场景
        // cc.director.loadScene("camera_Mutiplayers");
    },
    CreateReadyGo: function () {
        var aPrefab = cc.instantiate(this.ReadyGoPrefab);
        aPrefab.parent = cc.director.getScene();
        aPrefab.anchorX = 0.5;
        aPrefab.x = 355;
        aPrefab.y = 918;
        aPrefab.setLocalZOrder(1000);
        return aPrefab;
    },

    //设置语音状态
    setPlayerVoice(isSelf, isOpen) {
        if (isSelf == 1) {
            //自己
            if (isOpen == 1) {
                this.VideoStatesLeft.getChildByName('Open').active = true;
                this.VideoStatesLeft.getChildByName('Close').active = false;
            } else if (isOpen == 0) {
                this.VideoStatesLeft.getChildByName('Open').active = false;
                this.VideoStatesLeft.getChildByName('Close').active = true;
            }

        } else if (isSelf == 2) {
            //对手
            if (isOpen == 1) {
                this.VideoStatesRight.getChildByName('Open').active = true;
                this.VideoStatesRight.getChildByName('Close').active = false;
            } else if (isOpen == 0) {
                this.VideoStatesRight.getChildByName('Open').active = false;
                this.VideoStatesRight.getChildByName('Close').active = true;
            }
        }
    }


});
