import List from "./List";
const { ccclass, property } = cc._decorator;
//排行榜
@ccclass
export default class RankManager extends cc.Component {
    @property(cc.Sprite)        //用户头像换图
    phone: cc.Sprite = null;
    private rankList: List;     //排行榜

    listData: any;              //总列表信息
    selfData: any;              //自己信息
    rankNumber: number;         //用户自己排名 有可能不在排行榜内99+
    rankTotal: number;          //获取排行榜用户数量 现在为100

    onLoad() {
        // this.init();
    }
    //初始化数据
    init(data) {
        this.rankList = cc.find("ScrollView", this.node).getComponent(List);
        this.listData = data;
        this.selfData = null;
        this.rankNumber = 100;
        this.rankTotal = 100;
        this.rankList.setData(data);
        this.getRank();
    }

    start() {

    }
    //调用获取排行榜接口
    getRank() {
        let dataFile = {
            length: 100
        }
        cc.fx.GameTool.getRank(dataFile, data => this.getRankData(data));
    }
    //实际设置排行数据
    getRankData(data) {
        if (data) {
            // console.log(data);
            cc.fx.GameTool.getRankData(data, this, 6);
            this.setPic(this.selfData.pic);
        }
    }
    //返回按钮
    backClick() {
        cc.director.loadScene("LoadScene");
    }
    //最上方用户动画

    //设置头像  处理的逻辑比较多，不用公共类的了
    public setPic(pic) {
        this.phone.node.parent.getChildByName("icon").active = false;
        if (this.phone) {
            if (this.phone.node) {
                this.phone.node.active = false;
            }
        }

        fetch(pic)
            .then(response => {
                return response.headers.get('Content-Length');
            })
            .then(errNo => {
                if (errNo == "5093") {
                    this.phone.node.parent.getChildByName("icon").active = true;
                }
            })
            .catch(error => {
                // console.error('Error fetching X-Info:', error);
            });
        var self = this;
        cc.assetManager.loadRemote(pic, { ext: '.png' }, (err, texture: cc.Texture2D) => {

            if (texture) {
                self.phone.node.active = true;
                self.phone.spriteFrame = new cc.SpriteFrame(texture);
            }
            else {
            }

        })
    }


}
