
import { _decorator, Component, Node, sp, resources, TextAsset, ImageAsset, Asset, macro, dynamicAtlasManager, SpriteFrame, Sprite, Texture2D, rect, assetManager } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Test')
export class Test extends Component {
    skeletonData: sp.SkeletonData | null = null;

    skNode: Node | null = null;

    @property(Node)
    container:Node = null!;

    timer = 0;
    dstrd = false;
    assets: Asset[] = [];
    // texture:Texture2D[] = [];
    index = 0;
    onLoad() {
        macro.CLEANUP_IMAGE_CACHE = true;
        dynamicAtlasManager.enabled = false;
    }
    start() {
        // this.onAdd();
    }

    onAdd(evt?: Event, data?: string) {
        // resources.loadDir('1', (err, assets) => {
        //     assets.forEach((asset, index, arr) => {
        //         asset.addRef();
        //     });
        //     this.assets = assets;
        //     let skJson = JSON.parse(resources.get('1/all_json', TextAsset)!.text);
        //     skJson.skeleton.hash = '1';
        //     let atlas = resources.get('1/all_atlas', TextAsset)!.text;
        //     let imgs: ImageAsset[] = [
        //         resources.get('1/Cboy_atk', ImageAsset)!,
        //         resources.get('1/texiao', ImageAsset)!,
        //         resources.get('1/2700_0_acc', ImageAsset)!,
        //         resources.get('1/biaoqing_0_acc', ImageAsset)!,
        //         resources.get('1/tou_0_acc', ImageAsset)!,
        //         resources.get('1/2600_0_acc', ImageAsset)!
        //     ];
        //     let textureNames = [
        //         "Cboy_atk.png",
        //         "texiao.png",
        //         "2700_0_acc.png",
        //         "biaoqing_0_acc.png",
        //         "tou_0_acc.png",
        //         "2600_0_acc.png"
        //     ];

        //     this.skeletonData = new sp.SkeletonData();
        //     this.skeletonData.skeletonJson = skJson;
        //     this.skeletonData.atlasText = atlas;
        //     this.skeletonData.textures = imgs;
        //     this.skeletonData.textureNames = textureNames;
        //     let node = new Node();
        //     node.setScale(0.35, 0.35);
        //     this.node.addChild(node);
        //     let skeleton = node.addComponent(sp.Skeleton);
        //     skeleton.premultipliedAlpha = false;
        //     skeleton.skeletonData = this.skeletonData;
        //     skeleton.setSkin('2700');
        //     skeleton.setAnimation(0, 'stand_F', true);
        //     this.skNode = node;
        // });

        // let img = this.index++ % 2 == 0 ? 'test111' : 'tongyong';

        resources.load(`2/test111/spriteFrame`, SpriteFrame, (err, sf) => {
            this.assets.push(sf);
            sf.addRef();

            for (let i = 0; i < 5; ++i) {
                let node = new Node();
                let sprite = node.addComponent(Sprite);
                sprite.spriteFrame = sf;
                this.container.addChild(node);
            }
        });

        resources.load(`2/tongyong/spriteFrame`, SpriteFrame, (err, sf) => {
            this.assets.push(sf);
            sf.addRef();

            for (let i = 0; i < 5; ++i) {
                let node = new Node();
                let sprite = node.addComponent(Sprite);
                sprite.spriteFrame = sf;
                this.container.addChild(node);
            }
        });
    }

    onRelease(evt?: Event, data?: string) {
        // this.skNode?.removeFromParent();
        // if (this.skeletonData) {
        //     this.skeletonData.destroy();
        //     this.skeletonData = null;
        // }
        this.container.destroyAllChildren();
        this.assets?.forEach((asset, index, arr) => {
            asset.decRef();
        });
        this.assets = [];
        // assetManager.releaseAll();
    }

    update(dt: number) {
        this.timer += dt;
        if (this.timer >= 1) {
            this.timer = 0;
            if (!this.dstrd) {
                this.dstrd = true;
                this.onAdd();
            } else {
                this.dstrd = false;
                this.onRelease();
            }
        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
