package com.thebrokenrail.gestus.emote;

import net.minecraft.util.Identifier;
import net.minecraft.util.math.EulerAngle;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class EmoteLayer {
    private int frame = 0;
    private Identifier current;
    private final boolean loop;

    private boolean mirrored = false;

    public EmoteLayer(Identifier start, boolean loop) {
        current = start;
        this.loop = loop;
    }

    public Identifier getCurrent() {
        return current;
    }

    public void play(Identifier current, boolean mirrored) {
        if (!Objects.equals(this.current, current) || this.mirrored != mirrored) {
            this.current = current;
            this.mirrored = mirrored;
            frame = 0;
        }
    }

    public boolean isPlaying() {
        return current != null;
    }

    public boolean isMirrored() {
        return mirrored;
    }

    public Map<EmotePart, EulerAngle> next() {
        if (current != null) {
            Emote emote = EmoteRegistry.get(current);
            if (emote != null) {
                EmoteFrame result = emote.frames[frame];
                frame++;
                if (frame >= emote.frames.length) {
                    if (loop) {
                        frame = frame % emote.frames.length;
                    } else {
                        frame = 0;
                        current = null;
                    }
                }
                return mirrored ? result.mirrored : result.normal;
            }
        }
        frame = 0;
        return new HashMap<>();
    }
}
