import MyParticleSimulator from "./MyParticleSimulator";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ParticleUtils extends cc.Component {

    @property(cc.Integer)
    _frameNums: number = 1;


    @property({
        type: cc.Integer,
        displayName: '纹理帧数'
    })


    set frameNums(val) {
        this._frameNums = val;
        this.updateSysInfo();
    }

    get frameNums(): number {
        return this._frameNums;
    }



    @property(cc.Boolean)
    _aufoRate: boolean = false;

    @property({
        type: cc.Boolean,
        displayName: '自动化帧数'
    })

    set aufoRate(val) {
        this._aufoRate = val;
        this.updateSysInfo();
    }

    get aufoRate(): boolean {
        return this._aufoRate;
    }

    @property(cc.Integer)
    _animRate: number = 60;

    @property({
        type: cc.Integer,
        displayName: '动画帧数'
    })


    set animRate(val) {
        this._animRate = val;
        this.updateSysInfo();
    }

    get animRate(): number {
        return this._animRate;
    }


    _particleUtils: cc.ParticleSystem = null;
    updateSysInfo() {
        if (this._particleUtils == null) {
            this._particleUtils = this.node.getComponent(cc.ParticleSystem);
        }
        if (this._aufoRate) {
            this._particleUtils['setParticleFrameInfo'](this._frameNums, -1);
        } else {
            this._particleUtils['setParticleFrameInfo'](this._frameNums, this._animRate);
        }
    }

    onLoad() {

        if (this._particleUtils == null) {
            this._particleUtils = this.node.getComponent(cc.ParticleSystem);
        }
        if (CC_EDITOR) {
            cc.log("当前是在编辑器内!");
            if (this._aufoRate) {
                this._particleUtils['setParticleFrameInfo'](this._frameNums, -1);
            } else {
                this._particleUtils['setParticleFrameInfo'](this._frameNums, this._animRate);
            }
        } else {
            cc.log("不是编辑器,将本身原来的删除,然后换成新的内容");
            // let particleSystem = this.node.addComponent(MyParticleSystem);

            this._particleUtils['_simulator']._uvFilled = 0;
            delete this._particleUtils['_simulator'];
            this._particleUtils['_simulator'] = new MyParticleSimulator(this._particleUtils, this._frameNums, this._animRate);
            this._particleUtils['_simulator'].active = true;



            // this._simulator
            // // reset uv data so next time simulator will refill buffer uv info when exit edit mode from prefab.
            // this._simulator._uvFilled = 0;


            // this.node.removeComponent(cc.ParticleSystem);

        }


    }


}