/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.client.gui.components;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.EroticDungeon;

public class PreviewControlButton extends ImageButton {
    private static final ResourceLocation TEXTURE = EroticDungeon.rl("textures/gui/3d_controls.png");

    public enum ControlType {
        ROTATE_LEFT(0, 0, "gui.eroticdungeon.preview.rotate_left"),
        ROTATE_RIGHT(16, 0, "gui.eroticdungeon.preview.rotate_right"),
        ROTATE_UP(32, 0, "gui.eroticdungeon.preview.rotate_up"),
        ROTATE_DOWN(48, 0, "gui.eroticdungeon.preview.rotate_down"),
        RESET_VIEW(0, 16, "gui.eroticdungeon.preview.reset_view"),
        ZOOM_IN(16, 16, "gui.eroticdungeon.preview.zoom_in"),
        ZOOM_OUT(32, 16, "gui.eroticdungeon.preview.zoom_out"),
        TOGGLE_GRID(48, 16, "gui.eroticdungeon.preview.toggle_grid"),
        TOGGLE_AXES(0, 32, "gui.eroticdungeon.preview.toggle_axes"),
        TOGGLE_PLANES(16, 32, "gui.eroticdungeon.preview.toggle_planes"),
        PERSPECTIVE_TOP(32, 32, "gui.eroticdungeon.preview.perspective_top"),
        PERSPECTIVE_FRONT(48, 32, "gui.eroticdungeon.preview.perspective_front"),
        PERSPECTIVE_SIDE(0, 48, "gui.eroticdungeon.preview.perspective_side"),
        PERSPECTIVE_ISO(16, 48, "gui.eroticdungeon.preview.perspective_iso");

        public final int texX;
        public final int texY;
        public final String translationKey;

        ControlType(int texX, int texY, String translationKey) {
            this.texX = texX;
            this.texY = texY;
            this.translationKey = translationKey;
        }
    }

    final ControlType type;
    private boolean enable = true;

    public PreviewControlButton(int x, int y, @NotNull ControlType type, Button.OnPress onPress) {
        super(x, y, 16, 16, type.texX, type.texY, 0,
                TEXTURE, 64, 64, onPress,
                Component.translatable(type.translationKey));
        this.type = type;
    }

    public PreviewControlButton(int x, int y, int width, int height, @NotNull ControlType type, Button.OnPress onPress) {
        super(x, y, width, height, type.texX, type.texY, 0,
                 TEXTURE, 64, 64, onPress,
                Component.translatable(type.translationKey));
        this.type = type;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public boolean isEnable() {
        return enable;
    }

    @Override
    public void renderWidget(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
        if (!enable) {
            int disabledY = this.yTexStart + this.yDiffTex * 2; // 使用禁用状态的纹理
            this.renderTexture(guiGraphics, this.resourceLocation,
                    this.getX(), this.getY(),
                    this.xTexStart, disabledY,
                    this.yDiffTex, this.width, this.height,
                    this.textureWidth, this.textureHeight);
        } else if (this.isHovered()) {
            int hoverY = this.yTexStart + this.yDiffTex; // 使用悬停状态的纹理
            this.renderTexture(guiGraphics, this.resourceLocation,
                    this.getX(), this.getY(),
                    this.xTexStart, hoverY,
                    this.yDiffTex, this.width, this.height,
                    this.textureWidth, this.textureHeight);
        } else {
            super.renderWidget(guiGraphics, mouseX, mouseY, partialTick);
        }

        // 渲染工具提示
        if (this.isHovered()) {
            guiGraphics.renderTooltip(Minecraft.getInstance().font, this.getMessage(), mouseX, mouseY);
        }
    }
}
