const { ccclass, property } = cc._decorator;

export enum ARTNUM {
    Null = 0,
    /**关卡数目 */
    LevelNum = 1,
    /**目标分数 */
    TargetScore = 2,
    /**获取的分数 */
    GetScore = 3,
    /**消除得分 */
    EliminateScore = 4,
    /** */
    Test = "sdasdsadasds",
    you = "asdadsadsadasdsadsada"
}

@ccclass
export default class ArtNum extends cc.Component {

    @property({
        type: cc.Enum(ARTNUM),
        displayName: "艺术字类型",
        tooltip: "需要选择的艺术字类型",
        serializable: true,
    })

    public artNum: ARTNUM = null;
    public url: string = "artNum/"
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        //LogUtil.log("this.url",this.url);
    }

    // update (dt) {}

    /**初始化 */
    init(num: number) {

        this.url = `game/texture/artNum/${ARTNUM[this.artNum]}/`

        let numStr = num.toString();
        let numStrArr = numStr.split("");
        let numNodeArr = this.node.children;

        if (numStrArr.length > this.node.childrenCount) {
            for (var i = 0; i < numStrArr.length; i++) {
                let numChar = numStrArr[i];
                let numNode = numNodeArr[i];
                if (numNode) {
                    if (numNode.active == false) {
                        numNode.active = true;
                    }
                    let spr_num: cc.Sprite = numNode.getComponent(cc.Sprite);
                    this.loadSprite(`${this.url}${numChar}`,spr_num);
                }
                else {
                    numNode = new cc.Node();
                    let spr_num: cc.Sprite = numNode.addComponent(cc.Sprite);
                    this.loadSprite(`${this.url}${numChar}`,spr_num);
                    this.node.addChild(numNode);
                }
            }
        }
        else {
            for (var i = 0; i < numNodeArr.length; i++) {
                let numNode = numNodeArr[i];
                if (i < numStrArr.length) {
                    let numChar = numStrArr[i];
                    if (numNode) {
                        if (numNode.active == false) {
                            numNode.active = true;
                        }
                        let spr_num = numNode.getComponent(cc.Sprite);
                        this.loadSprite(`${this.url}${numChar}`,spr_num);
                    }
                    else {
                        numNode = new cc.Node();
                        let spr_num = numNode.addComponent(cc.Sprite);
                        this.loadSprite(`${this.url}${numChar}`,spr_num);
                        this.node.addChild(numNode);
                    }
                }
                else {
                    if (numNode) {
                        numNode.active = false;
                    }
                }
            }
        }
    }

    /**
     * 加载sprite
     * @param url 路径
     * @param spr 图片资源
     */
    loadSprite( url: string,spr: cc.Sprite) {
        mk.loader.load(url, cc.SpriteFrame).then((spriteFrame) => {
            spr.spriteFrame = spriteFrame;
        })
    }
}

