import RoundBox from "../../Script/module/Tool/RoundBox";
import NumberToImage from "../../Script/NumberToImage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CareerItem 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;
    @property(cc.SpriteAtlas)
    UI2: cc.SpriteAtlas = null;
    randerChildren: any;

    @property(cc.Prefab)
    iconPrefab: cc.Prefab = null;

    /**数据改变时调用 */
    public dataChanged() {
        this.randerChildren = [];

        for (let i = 4; i < 12; i++) {
            this.node.children[i].getChildByName("name").getComponent(cc.Label).string = "";
            this.node.children[i].getChildByName("level").getComponent(cc.Label).string = "";
            this.node.children[i].getChildByName("mask").getChildByName("icon").getComponent(cc.Sprite).spriteFrame = null;
            this.node.children[i].getChildByName("kong").active = true;
        }
        if (this.data != undefined) {
            this.node.opacity = 255;
            this.node.getChildByName("rank").getComponent(cc.Label).string = this.data.count + "";
            this.node.getChildByName("cityName").getComponent(cc.Sprite).spriteFrame = this.UI2.getSpriteFrame(this.data.name);
            this.node.getChildByName("ranking").getChildByName("one").active = false;
            this.node.getChildByName("ranking").getChildByName("two").active = false;
            this.node.getChildByName("ranking").getChildByName("three").active = false;
            this.node.getChildByName("ranking").getChildByName("nomal").active = false;
            if (this.data.rank == 1) {
                this.node.getChildByName("ranking").getChildByName("one").active = true;
            }
            else if (this.data.rank == 2) {
                this.node.getChildByName("ranking").getChildByName("two").active = true;
            }
            else if (this.data.rank == 3) {
                this.node.getChildByName("ranking").getChildByName("three").active = true;
            }
            else {
                this.node.getChildByName("ranking").getChildByName("nomal").active = true;
                NumberToImage.numberToImageNodes4(this.data.rank, 43, 15, "company_", this.node.getChildByName("ranking").getChildByName("nomal").getChildByName("rank"), true);
                let rank = this.node.getChildByName("ranking").getChildByName("nomal").getChildByName("rank");
                if (rank && rank.children) {
                    if (rank.children.length > 0) {
                        for (let i = 0; i < rank.children.length; i++) {
                            rank.children[i].color = cc.Color.WHITE;
                        }
                    }
                }

            }
            this.node.zIndex = this.data.rank;
            if (this.randerChildren) {
                if (this.randerChildren.length == 0) {
                    this.randerChildren = [];
                    let lengthTemp = 0;
                    if (this.data.rankingData) {
                        if (this.data.rankingData.length) {
                            lengthTemp = this.data.rankingData.length;
                        }
                    }

                    let length = lengthTemp + 4;
                    if (length > 12) length = 12;
                    for (let i = 4; i < length; i++) {
                        this.randerChildren.push(this.node.children[i]);
                        let username = cc.fx.GameTool.subName(this.data.rankingData[i - 4].username, 5);
                        if (username == "user") username = "匿名玩家";
                        // console.log("用户名字:", username);
                        this.node.children[i].getChildByName("kong").active = false;
                        this.node.children[i].getChildByName("name").getComponent(cc.Label).string = username + "";
                        this.node.children[i].getChildByName("level").getComponent(cc.Label).string = this.data.rankingData[i - 4].addLevel;
                        if (this.data.rankingData[i - 4].useravatar == "" || this.data.rankingData[i - 4].useravatar == null || this.data.rankingData[i - 4].useravatar == undefined
                        ) {
                            // this.firstRender.children[i].getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.defaultsprite;
                        }
                        else if (this.data.rankingData[i - 4].useravatar == "0" || this.data.rankingData[i - 4].useravatar == "1" || this.data.rankingData[i - 4].useravatar == "2"
                            || this.data.rankingData[i - 4].useravatar == "3" || this.data.rankingData[i - 4].useravatar == "4" || this.data.rankingData[i - 4].useravatar == "5" || this.data.rankingData[i - 4].useravatar == "6"
                            || this.data.rankingData[i - 4].useravatar == "7" || this.data.rankingData[i - 4].useravatar == "8" || this.data.rankingData[i - 4].useravatar == "9" ||
                            this.data.rankingData[i - 4].useravatar == "10" || this.data.rankingData[i - 4].useravatar == "11" || this.data.rankingData[i - 4].useravatar == "12" || this.data.rankingData[i - 4].useravatar == "13" || this.data.rankingData[i - 4].useravatar == "14"
                            || this.data.rankingData[i - 4].useravatar == "15" || this.data.rankingData[i - 4].useravatar == "16" || this.data.rankingData[i - 4].useravatar == "17"
                        ) {
                            this.node.children[i].getChildByName("mask").getChildByName("icon").active = false;
                            this.node.children[i].getChildByName("mask").getChildByName("sp").active = true;
                            let useravatar = this.data.rankingData[i - 4].useravatar;
                            if (this.node.children[i]) {
                                //this.firstRender.children[i].getChildByName("mask").getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.UI.getSpriteFrame(useravatarTemp);
                                let actionName = cc.fx.GameTool.getActionName(useravatar);
                                this.node.children[i].getChildByName("mask").getChildByName("sp").scale = 0.85;
                                cc.fx.GameTool.loadSpineSimple(this.node.children[i].getChildByName("mask").getChildByName("sp"), actionName);
                                // this.node.children[i].getChildByName("mask").getChildByName("icon").width = 150;
                                // this.node.children[i].getChildByName("mask").getChildByName("icon").height = 150;
                            }
                            // let useravatar = this.data.rankingData[i - 4].useravatar;
                            // let useravatarTemp = "icon_" + useravatar;
                            // this.node.children[i].getChildByName("mask").getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.UI.getSpriteFrame(useravatarTemp);
                        }
                        else {
                            this.node.children[i].getChildByName("mask").getChildByName("icon").active = true;
                            this.node.children[i].getChildByName("mask").getChildByName("sp").active = false;
                            this.setPic(this.data.rankingData[i - 4].useravatar, this.node.children[i].getChildByName("mask").getChildByName("icon"));
                        }
                    }
                }
            }
        }
        else {
            this.node.opacity = 0;
        }
    }



    public setPic(url, node) {
        // 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 && node) {
                // this.node.getChildByName("pic").active = true;

                var sprite = node.getComponent(cc.Sprite);
                sprite.spriteFrame = new cc.SpriteFrame(texture);
            }
            else {
                // console.log("加载图片失败" + url);
            }
        })
    }


}
