// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import GameData from "./GameData";
import List from "./ListView/List";
import HttpUtil from "./crypto/serverAPI";
import Rq from "./crypto/serverAPI";

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 = GameData._instance.GM_INFO.total;
        if(GameData._instance.GM_INFO.mean_Time > 10){
            GameData._instance.GM_INFO.mean_Time = (parseInt(Math.random()*10+"")+5)/10
        }
        this.time.string = GameData._instance.GM_INFO.mean_Time + "s";
        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;
        this.getRank();
    }
    //打开排行榜
    openRank(){
        cc.director.loadScene("RankScene");
    }
    //重新开始玩
    again(){
        cc.director.loadScene("GameScene");
    }
    //获取排行榜
    getRank(){
        let postData = {
            "page":1,
            "pageSize":100
        }; 
        HttpUtil.rankData(2,this.getRankData.bind(this),postData);
    }
    //设置排行信息
    getRankData(data){
        if(data){
            this.listData = data.data.list;
            this.selfData = data.data.info;
            let rankData = [];
            let self = false;
            if(this.selfData.nickName.length > 4){
                this.selfData.nickName= this.selfData.nickName.substring(0,4) + "..."
            }
            this.selfNode.getChildByName("name").getComponent(cc.Label).string = this.selfData.nickName;
            this.selfNode.getChildByName("total").getComponent(cc.Label).string = this.selfData.totalSunCount;
            this.setPic(this.selfNode.getChildByName("pic").getChildByName("icon"),this.selfData.pic);
            for(let i=0;i<=this.listData.length-1;i++){
                rankData.push({rank:(i+1), name:this.listData[i].nickName, total:this.listData[i].totalSunCount, pic:this.listData[i].pic});
                if(GameData._instance.GM_INFO.userId == this.listData[i].userId){
                    self = true;
                    this.selfNode.getChildByName("rank").getComponent(cc.Label).string =(i+1) + ""; 
                }
                if(i == (this.listData.length-1) && self == false){
                    this.selfNode.getChildByName("rank").getComponent(cc.Label).string = "99+"; 
                }
                if(i<5) this.setRank(i,this.listData[i]);
            }
        }
    }
    //根据内容填充排行榜
    setRank(num,data){
        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;
        }
        if(hitNode){
            hitNode.active = true;
            if(data.nickName.length > 4){
                data.nickName= data.nickName.substring(0,4) + "..."
            }
            hitNode.getChildByName("name").getComponent(cc.Label).string = data.nickName;
            hitNode.getChildByName("total").getComponent(cc.Label).string = data.totalSunCount;
            this.setPic(hitNode.getChildByName("pic").getChildByName("icon"),data.pic);
        }
    }   
    //设置头像
    public setPic(node,pic){
        node.active = false;
        let url = pic;
        setTimeout(() => {
            fetch(url)
            .then(response => {
                return response.headers.get('Content-Length');
            })
    
            .then(errNo => {
                if(errNo == "5093"){
                    node.active = true;
                }
            })
            .catch(error => {
                console.error('Error fetching X-Info:', error);
            });
        }, 100);
        cc.assetManager.loadRemote(url, {ext:'.jpg'},(err, texture:cc.Texture2D) => {
            if(texture){
                node.active = true;
                node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
            }
            else{
                console.log(err,texture)
            }
        })
    }

}
