
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.Sprite)
    finishiTime: cc.Sprite = null;
    listData: any;
    selfData: any;
    accuracy:number;

    // onLoad () {}
    start () {
        this.count.string = 0 + "";
        this.init();

        // success_${today}_${code}_${matchedReferrer.gameId}
        // const today = new Date().toLocaleDateString();
        // const data = "success_${today}_${code}_${matchedReferrer.gameId}"
 
    }
    //初始化数据
    init(){
        //展示准确率
        this.showAccuracy();
        //展示完成时间
        this.showFinishiTime();
        //上传排行榜
        this.setRank();
        //删除本关卡
        setTimeout(() => {
            cc.fx.GameTool.getCustom(true);
        }, 100);

        var urlNow = window.location.href;
        if(this.containsTrain(urlNow)){
            this.node.getChildByName("btn").getChildByName("again").active = false;
            this.node.getChildByName("btn").getChildByName("rank").active = false;
            this.node.getChildByName("btn").getChildByName("finishi").active = true;
            this.setLocalStorage();

        }
        else{
            this.node.getChildByName("btn").getChildByName("again").active = true;
            this.node.getChildByName("btn").getChildByName("rank").active = true;
            this.node.getChildByName("btn").getChildByName("finishi").active = false;
        }
    }

    setLocalStorage(){
        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}));
    }

    setRank(){
        let data = {
            "totleTimes": cc.fx.GameConfig.GM_INFO.stepTimeList,
            "accuracy": this.accuracy,
        }
        cc.fx.GameTool.setRank(data);
    }

    showFinishiTime(){
        cc.tween(this.finishiTime)
        .delay(1.5)
        .to(0.5,{fillRange:1})
        .start();
        cc.tween(this.time.node)
        .delay(1.7)
        .to(0.3,{opacity:255})
        .call(()=>{
            this.conversionTime();
        })
        .start();
    }

    conversionTime(){
        var minute = 0;
        var second = 0;
        let obj = {a:100};
        let time = parseInt(cc.fx.GameConfig.GM_INFO.stepTimeList/1000 + "");
        cc.tween(obj)
        // .delay(0.1)
        .to(1,{a:time},{
            progress: (start,end,current,ratio)=>{
                current = end*ratio;
                second = current.toFixed(0);
                if(second >= 60){
                    minute = parseInt(second/60+"");
                    second -= 60*minute;
                }
                this.time.string = minute + "分" + second + "秒";
            }
        })
        .call(()=>{
            this.node.getChildByName("btn").active = true;
            cc.tween(this.node.getChildByName("btn"))
            .to(0.3,{opacity:255})
            .start();
        })
        .start()
    }
    
    showAccuracy(){
        this.accuracy = parseInt(cc.fx.GameConfig.GM_INFO.successList.length/
            cc.fx.GameConfig.GM_INFO.level*100+"");
        
        console.log("答对的个数:",cc.fx.GameConfig.GM_INFO.successList.length);
        console.log("总数:",cc.fx.GameConfig.GM_INFO.level);
        console.log("准确率:",this.accuracy);
        let obj = {a:100};
        cc.tween(obj)
        .delay(0.5)
        .to(1,{a:this.accuracy},{
            progress: (start,end,current,ratio)=>{
                current = end*ratio;
                current = current.toFixed(0);
                this.count.string = current + "";
            }
        })
        .start()
    }

    jumpFinishi(){
        let url = "https://train.sparkus.cn/poster/game/" + cc.fx.GameConfig.GM_INFO.scode + "?suc=1";
        window.location.href = url;
    }

    //判断来源
    containsTrain(str) {
        return /from=train/i.test(str);
    }

    //打开排行榜
    openRank(){
        cc.director.loadScene("RankScene");
    }
    //重新开始玩
    again(){
        cc.director.loadScene("GameScene");
    }

    update(dt: number): void {
        // this.count.string = this.accuracy + "";
    }
}
