package dev.isxander.controlify.controller.joystick.render;

import dev.isxander.controlify.bindings.JoystickAxisBind;
import dev.isxander.controlify.controller.joystick.JoystickState;
import dev.isxander.controlify.gui.DrawSize;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;

public abstract class ThemedRenderer {
    protected final Minecraft minecraft = Minecraft.getInstance();
    protected final String themeId;

    public ThemedRenderer(String themeId) {
        this.themeId = themeId;
    }

    public static class Button extends ThemedRenderer implements JoystickRenderer.Button {
        private final ResourceLocation texture;

        public Button(String themeId, String identifier) {
            super(themeId);
            this.texture = new ResourceLocation("controlify", "textures/gui/joystick/" + themeId + "/" + identifier + ".png");
        }

        @Override
        public DrawSize render(GuiGraphics graphics, int x, int centerY, int size, boolean down) {
            graphics.pose().pushPose();
            graphics.pose().translate(x, centerY, 0);

            float scale = (float) size / 22f;
            graphics.pose().scale(scale, scale, 1);
            graphics.pose().translate(0f, -DEFAULT_SIZE / scale / 2f, 0);

            graphics.blit(texture, 0, 0, 0, 0, DEFAULT_SIZE, DEFAULT_SIZE, DEFAULT_SIZE, DEFAULT_SIZE);

            graphics.pose().popPose();

            return new DrawSize(size, size);
        }
    }

    public static class Axis extends ThemedRenderer implements JoystickRenderer.Axis {
        private final ResourceLocation texture;

        public Axis(String themeId, String identifier) {
            super(themeId);
            this.texture = new ResourceLocation("controlify", "textures/gui/joystick/" + themeId + "/" + identifier + ".png");
        }

        @Override
        public DrawSize render(GuiGraphics graphics, int x, int centerY, int size, JoystickAxisBind.AxisDirection direction) {
            graphics.pose().pushPose();
            graphics.pose().translate(x, centerY, 0);

            float scale = (float) size / 22f;
            graphics.pose().scale(scale, scale, 1);
            graphics.pose().translate(0f, -DEFAULT_SIZE / scale / 2f, 0);

            graphics.blit(
                    texture,
                    0, 0,
                    direction.ordinal() * DEFAULT_SIZE, 0,
                    DEFAULT_SIZE, DEFAULT_SIZE,
                    DEFAULT_SIZE * JoystickAxisBind.AxisDirection.values().length, DEFAULT_SIZE
            );

            graphics.pose().popPose();

            return new DrawSize(size, size);
        }
    }

    public static class Hat extends ThemedRenderer implements JoystickRenderer.Hat {
        private final ResourceLocation texture;

        public Hat(String themeId, String identifier) {
            super(themeId);
            this.texture = new ResourceLocation("controlify", "textures/gui/joystick/" + themeId + "/" + identifier + ".png");
        }

        @Override
        public DrawSize render(GuiGraphics graphics, int x, int centerY, int size, JoystickState.HatState hatState) {
            graphics.pose().pushPose();
            graphics.pose().translate(x, centerY, 0);

            float scale = (float) size / 22f;
            graphics.pose().scale(scale, scale, 1);
            graphics.pose().translate(0f, -DEFAULT_SIZE / scale / 2f, 0);

            graphics.blit(
                    texture,
                    0, 0,
                    hatState.ordinal() * DEFAULT_SIZE, 0,
                    DEFAULT_SIZE, DEFAULT_SIZE,
                    DEFAULT_SIZE * JoystickState.HatState.values().length, DEFAULT_SIZE
            );

            graphics.pose().popPose();

            return new DrawSize(size, size);
        }
    }
}
