
import NumberToImage from "../../NumberToImage";
import List, { ListType } from "./List";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemRender extends cc.Component {
    /**数据 */
    public data: any = null;
    /**索引 0表示第一项*/
    public itemIndex: number = 0;

    @property(cc.SpriteFrame)
    defaultsprite: cc.SpriteFrame = null;

    @property(cc.SpriteAtlas)
    ui: cc.SpriteAtlas = null;

    /**数据改变时调用 */
    public dataChanged() {
        console.log("用户数据:", this.data);
        let useravatarIcon = this.data.useravatarIcon;
        useravatarIcon = "kuang_" + (parseInt(useravatarIcon) + 1);
        this.node.getChildByName("icon").getComponent(cc.Sprite).spriteFrame =
            this.ui.getSpriteFrame(useravatarIcon);
        console.log("用户头像框:", this.data.useravatarIcon, useravatarIcon);
        this.data.username = cc.fx.GameTool.subName(this.data.username, 7);
        let name = this.data.username;
        if (name == "user") name = "匿名玩家";
        // this.node.getChildByName("rankLab").getComponent(cc.Label).string = this.data.rank + "";
        this.node.getChildByName("nameLab").getComponent(cc.Label).string = name + "";
        // this.node.getChildByName("totalLab").getComponent(cc.Label).string = this.data.levelAmount;

        NumberToImage.numberToImageNodes3((this.data.rank), 43, 15, "rank_", this.node.getChildByName("rankLab"), true);
        NumberToImage.numberToImageNodes3(this.data.levelAmount, 43, 15, "level_", this.node.getChildByName("totalLab"), true);

        // this.node.getChildByName("timeLab").getComponent(cc.Label).string = timeTemp + "";
        this.node.getChildByName("rank").getChildByName("one").active = false;
        this.node.getChildByName("rank").getChildByName("two").active = false;
        this.node.getChildByName("rank").getChildByName("three").active = false;
        if (this.data.rank == 1) {
            this.node.getChildByName("rank").getChildByName("one").active = true;
            this.node.getChildByName("rankLab").active = false;
        }
        else if (this.data.rank == 2) {
            this.node.getChildByName("rank").getChildByName("two").active = true;
            this.node.getChildByName("rankLab").active = false;
        }
        else if (this.data.rank == 3) {
            this.node.getChildByName("rank").getChildByName("three").active = true;
            this.node.getChildByName("rankLab").active = false;
        } else {
            this.node.getChildByName("rankLab").active = true;
        }
        if (this.data.useravatar == "" || this.data.useravatar == null || this.data.useravatar == undefined
        ) {
            this.node.getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.defaultsprite;
        }
        else if (this.data.useravatar == "0" || this.data.useravatar == "1" || this.data.useravatar == "2"
            || this.data.useravatar == "3") {
            let useravatar = this.data.useravatar;
            let useravatarTemp = "icon_" + useravatar;
            console.log("用户头像:", useravatarTemp);
            this.node.getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.ui.getSpriteFrame(useravatarTemp);
        }

        else this.setPic();
    }

    public setPic() {
        // console.log("设置头像:", this.data.useravatar);
        // this.node.getChildByName("pic").getChildByName("icon").active = false;
        this.node.getChildByName("pic").active = false;
        var self = this;
        let url = this.data.useravatar;

        cc.assetManager.loadRemote(url, { ext: '.png' }, (err, texture: cc.Texture2D) => {
            if (texture) {
                this.node.getChildByName("pic").active = true;
                var sprite = this.node.getChildByName("pic").getComponent(cc.Sprite);
                sprite.spriteFrame = new cc.SpriteFrame(texture);
                // console.log(this.data.rank,"设置头像成功",err);
            }
            else {
                // console.log("设置头像失败",url);
                // console.log(err, texture)
            }
        })
    }


}
