import { GC_MODES } from '../../const';
import { WebglRenderer } from '../WebglRenderer';
/**
 * TextureGarbageCollector. This class manages the GPU and ensures that it does not get clogged
 * up with textures that are no longer being used.
 *
 */
export default class TextureGarbageCollector {
    renderer: WebglRenderer;
    count: number;
    checkCount: number;
    maxIdle: number;
    checkCountMax: number;
    mode: number;
    /**
     * @param {WebGLRenderer} renderer - The renderer this manager works for.
     */
    constructor(renderer: WebglRenderer) {
        this.renderer = renderer;

        this.count = 0;
        this.checkCount = 0;
        this.maxIdle = 60 * 60;
        this.checkCountMax = 60 * 10;
        this.mode = GC_MODES.AUTO;
    }

    /**
     * Checks to see when the last time a texture was used
     * if the texture has not been used for a specified amount of time it will be removed from the GPU
     */
    update() {
        this.count++;

        if (this.mode === GC_MODES.MANUAL) {
            return;
        }

        this.checkCount++;

        if (this.checkCount > this.checkCountMax) {
            this.checkCount = 0;

            this.run();
        }
    }

    /**
     * Checks to see when the last time a texture was used
     * if the texture has not been used for a specified amount of time it will be removed from the GPU
     */
    run() {
        const tm = this.renderer.textureManager;
        const managedTextures = tm._managedTextures;
        let wasRemoved = false;

        for (let i = 0; i < managedTextures.length; i++) {
            const texture = managedTextures[i];

            // only supports non generated textures at the moment!
            //去掉长期不用的纹理
            if (!texture._glRenderTargets && this.count - texture._touchedId > this.maxIdle) {
                tm.destroyTexture(texture, true);
                managedTextures[i] = null;
                wasRemoved = true;
                
            }
        }

        if (wasRemoved) {
            let j = 0;

            for (let i = 0; i < managedTextures.length; i++) {
                if (managedTextures[i] !== null) {
                    managedTextures[j++] = managedTextures[i];
                }
            }

            managedTextures.length = j;
        }
    }

    /**
     * Removes all the textures within the specified displayObject and its children from the GPU
     *
     * @param {DisplayObject} displayObject - the displayObject to remove the textures from.
     */
    unload(displayObject) {
        const tm = this.renderer.textureManager;

        // only destroy non generated textures
        if (displayObject._texture && displayObject._texture._glRenderTargets) {
            tm.destroyTexture(displayObject._texture, true);
        }

        for (let i = displayObject.children.length - 1; i >= 0; i--) {
            this.unload(displayObject.children[i]);
        }
    }
}
