
import NumberToImage from "../../NumberToImage";
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() {
        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);

        // console.log("查看排行", this.data.endLevelNum);
        if (this.data.endLevelNum && Number(this.data.endLevelNum) > 1) {
            let endlessNode = this.node.getChildByName("endlessNode");
            endlessNode.active = true;
            NumberToImage.numberToImageNodes5(this.data.endLevelNum - 1, 30, 15, "Black", endlessNode.getChildByName("levelNum"), true);
        } else {
            this.node.getChildByName("endlessNode").active = false;
        }
        // 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
        ) {
            if (this.node.getChildByName("pic")) {
                this.node.getChildByName("pic").getChildByName("pic").active = true;
                this.node.getChildByName("pic").getChildByName("sp").active = false;
                this.node.getChildByName("pic").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" || this.data.useravatar == "4" || this.data.useravatar == "5" || this.data.useravatar == "6"
            || this.data.useravatar == "7" || this.data.useravatar == "8" || this.data.useravatar == "9" || this.data.useravatar == "10"
            || this.data.useravatar == "11" || this.data.useravatar == "12" || this.data.useravatar == "13" || this.data.useravatar == "14"
            || this.data.useravatar == "15" || this.data.useravatar == "16" || this.data.useravatar == "17"
        ) {
            this.node.getChildByName("pic").getChildByName("pic").active = false;
            this.node.getChildByName("pic").getChildByName("sp").active = true;

            let actionName = cc.fx.GameTool.getActionName(this.data.useravatar);
            this.node.getChildByName("pic").getChildByName("sp").scale = 0.6;
            cc.fx.GameTool.loadSpineSimple(this.node.getChildByName("pic").getChildByName("sp"), actionName);
            // this.node.getChildByName("pic").getChildByName("pic").getComponent(cc.Sprite).spriteFrame = this.ui.getSpriteFrame(useravatarTemp);
        }

        else {
            this.node.getChildByName("pic").getChildByName("pic").active = true;
            this.node.getChildByName("pic").getChildByName("sp").active = false;
            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 && self.node) {
                this.node.getChildByName("pic").active = true;
                var sprite = self.node.getChildByName("pic").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)
            }
        })
    }


}
