
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    @property(cc.Label)
    count: cc.Label = null;
    @property(cc.Label)
    time: cc.Label = null;
    @property(cc.Node)
    selfNode: cc.Node = null;
    @property(cc.Node)
    one: cc.Node = null;
    @property(cc.Node)
    two: cc.Node = null;
    @property(cc.Node)
    three: cc.Node = null;
    @property(cc.Node)
    four: cc.Node = null;
    @property(cc.Node)
    five: cc.Node = null;
    listData: any;
    selfData: any;
    // onLoad () {}
    start () {
        this.count.string = cc.fx.GameConfig.GM_INFO.score + "";
        var yes = 0;
        var successList = cc.fx.GameConfig.GM_INFO.successList;
        if(successList.length > 0){
            var success = 0;
            for(let i=0; i<successList.length; i++){
                if(successList[i] == true) success += 1;
            }
            yes = success/successList.length;
        }
        yes = Math.floor(yes * 1000)/10;
        this.time.string =   yes + "%";
        this.init();
       
    }   
    //初始化数据
    init(){
        this.listData = [];
        this.selfData = null;
        this.one.active = false;
        this.two.active = false;
        this.three.active = false;
        this.four.active = false;
        this.five.active = false;
        var urlNow = window.location.href;
        if(this.containsTrain(urlNow)){
            this.node.getChildByName("again").active = false;
            this.node.getChildByName("back").active = false;
            this.node.getChildByName("finishi").active = true;
            this.setLocalStorage();
        }
        else{
            this.setLocalStorage();
            this.node.getChildByName("again").active = true;
            this.node.getChildByName("back").active = true;
            this.node.getChildByName("finishi").active = false;
        }

        this.getRank();
    }

    setLocalStorage(){
        let timeData = cc.fx.GameConfig.TIME_INFO.totalTime;
        
        const today = new Date().toLocaleDateString();
        var name = `success_${today}_${cc.fx.GameConfig.GM_INFO.scode}_${cc.fx.GameConfig.GM_INFO.gameId}`
        localStorage.setItem(name, JSON.stringify({success:true}));
    }

    //打开排行榜
    openRank(){
        cc.director.loadScene("RankScene");
    }
    //重新开始玩
    again(){
        cc.fx.GameConfig.GM_INFO.round = 0;
        cc.fx.GameConfig.GM_INFO.level = 0;
        cc.fx.GameConfig.GM_INFO.stepTimeList = 0;
        cc.fx.GameConfig.GM_INFO.successList = [];
        cc.fx.GameConfig.GM_INFO.fen = 0;
        cc.fx.GameConfig.GM_INFO.score = 0;
        cc.fx.GameConfig.GM_INFO.min_Time = 0;
        cc.fx.GameConfig.TIME_INFO.totalTime = 120;
        cc.director.loadScene("GameScene");
    }
    //判断来源
    containsTrain(str) {
        return /from=train/i.test(str);
    }
    //获取排行榜
    getRank(){
        //获取排行榜数据  所需数据量
        let dataFile = {
            length:5
        }
         cc.fx.GameTool.getRank(dataFile,data =>this.getRankData(data));
    }
    //打开排行榜
    jumpFinishi(){
        let url = "https://train.sparkus.cn/poster/game/" + cc.fx.GameConfig.GM_INFO.scode + "?suc=1";
        window.location.href = url;
    }
    //设置排行信息
    getRankData(data){
        if(data){
             cc.fx.GameTool.getRankData(data,this,4);
             cc.fx.GameTool.setPic(this.selfNode.getChildByName("pic").getChildByName("icon"),this.selfData.pic);
            var length = this.listData.length-1; if(length > 4) length = 4;
            for(let i=0;i<=length;i++){
                this.setRank(i,this.listData[i]);
            }
        }
    }
    //根据内容填充排行榜
    setRank(num,data){
        if(!data){
            return;
        }

        var hitNode = null;
        if(num == 0){
            hitNode = this.one;
        }
        else if(num == 1){
            hitNode = this.two;
        }
        else if(num == 2){
            hitNode = this.three;
        }
        else if(num == 3){
            hitNode = this.four;
        }
        else if(num == 4){
            hitNode = this.five;
        }

        let trun = num + 1;
        for(let i=0; i< trun; i++){
            if(this.listData[i] && this.listData[i].nickName == cc.fx.GameConfig.GM_INFO.nickName){
                trun = i + 1;
                break;
            }
            else if(!this.listData[i] == null ){
                trun = i * 2;
                break;
            }
            this.node.color = cc.color(this.listData[0],this.listData[1],this.listData[2]);
            cc.tween(this.node)
            .to(0.5,{scale:2})
            .by(1,{opacity:0})
            .delay(0.5)
            .call(() =>{
                this.node.scale = 1;
                this.node.opacity = 255;
                this.node.color = cc.color(255,255,255);
            })
            .start();
        }

        hitNode.getChildByName("num").getComponent(cc.Label).string = num + "";
        cc.tween(hitNode.getChildByName("num").getComponent(cc.Label))
        .to(0.5,{string:trun+""})
        .start();

        let record = cc.fx.GameConfi.GM_INFO.score;
        if(data.score >= record){
            this.selfData = data;
            this.selfNode.active = true;
        }

        if(hitNode){
            hitNode.active = true;
            if(data.nickName.length >= 4)
                data.nickName = cc.fx.GameTool.subName(data.nickName,4);
            hitNode.getChildByName("name").getComponent(cc.Label).string = data.nickName;
            hitNode.getChildByName("total").getComponent(cc.Label).string = data.score;
             cc.fx.GameTool.setPic(hitNode.getChildByName("pic").getChildByName("icon"),data.pic);
        }
    }   
}
