class DisplayManager {
    constructor() {
        this.looks = [];
        this.activeLook = 0;
        this.makeupEngine = null;
    }

    async push(look) {
        const currentLook = [];

        for (let i in look) {
            const effect = look[i];
            let id = await this.makeupEngine.CreateMakeup(effect.feature, effect.finish, [0.0, 0.0, 0.5, 1.0]);
            this.makeupEngine.UpdateMakeup(id);

            currentLook.push({ effect: effect, ID: id });
        }

        this.looks.push(currentLook);
    }

    async updateMakeup(lookCallback) {
        for (const look of this.looks) {
            lookCallback(look);
            look.forEach((effect) => {
                this.makeupEngine.UpdateMakeup(effect.ID);
            });
        }
    }

    async displayActive() {
        let look = this.looks[this.activeLook];
        look.forEach((effect) => {
            this.makeupEngine.DisplayMakeup(effect.ID);
        });
    }

    async next() {
        await this.go(1);
    }

    async previous() {
        await this.go(-1);
    }

    async go(step) {
        if (this.activeLook == -1) return;

        this.removeOldLook();
        let newIndex = (this.activeLook + step + this.looks.length) % this.looks.length;
        this.activeLook = -1;
        this.displayNewLook(newIndex);
        this.activeLook = newIndex;
    }

    async updateActive(index) {
        this.removeOldLook();
        this.displayNewLook(index);
        this.activeLook = index;
    }

    displayNewLook(index) {
        let newLook = this.looks[index];
        newLook.forEach((effect) => {
            this.makeupEngine.DisplayMakeup(effect.ID);
        });
    }

    removeOldLook() {
        let oldLook = this.looks[this.activeLook];
        oldLook.forEach((effect) => {
            this.makeupEngine.HideMakeup(effect.ID);
        });
    }
}
