/*
 *  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.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class PreviewToolbar extends AbstractWidget {
    private final List<PreviewControlButton> buttons = new ArrayList<>();
    private Consumer<PreviewControlButton.ControlType> onButtonClick;

    private boolean gridVisible = true;
    private boolean axesVisible = true;
    private boolean planesVisible = false;

    public PreviewToolbar(int x, int y, int spacing) {
        super(x, y, 0, 16, net.minecraft.network.chat.Component.empty());

        // 创建按钮数组
        PreviewControlButton.ControlType[] buttonTypes = {
                PreviewControlButton.ControlType.ROTATE_LEFT,
                PreviewControlButton.ControlType.ROTATE_RIGHT,
                PreviewControlButton.ControlType.ROTATE_UP,
                PreviewControlButton.ControlType.ROTATE_DOWN,
                PreviewControlButton.ControlType.RESET_VIEW,
                PreviewControlButton.ControlType.ZOOM_IN,
                PreviewControlButton.ControlType.ZOOM_OUT,
                PreviewControlButton.ControlType.TOGGLE_GRID,
                PreviewControlButton.ControlType.TOGGLE_AXES,
                PreviewControlButton.ControlType.TOGGLE_PLANES,
                PreviewControlButton.ControlType.PERSPECTIVE_TOP,
                PreviewControlButton.ControlType.PERSPECTIVE_FRONT,
                PreviewControlButton.ControlType.PERSPECTIVE_SIDE,
                PreviewControlButton.ControlType.PERSPECTIVE_ISO
        };

        // 创建按钮
        int currentX = x;
        for (PreviewControlButton.ControlType type : buttonTypes) {
            PreviewControlButton button = new PreviewControlButton(
                    currentX, y, type, btn -> {
                if (onButtonClick != null) {
                    onButtonClick.accept(type);
                }
                updateButtonStates();
            }
            );

            // 设置特定按钮的初始状态
            if (type == PreviewControlButton.ControlType.TOGGLE_GRID) {
                button.setEnable(gridVisible);
            } else if (type == PreviewControlButton.ControlType.TOGGLE_AXES) {
                button.setEnable(axesVisible);
            } else if (type == PreviewControlButton.ControlType.TOGGLE_PLANES) {
                button.setEnable(planesVisible);
            }

            buttons.add(button);
            currentX += button.getWidth() + spacing;
        }

        this.width = currentX - x - spacing;
    }

    public void setOnButtonClick(Consumer<PreviewControlButton.ControlType> callback) {
        this.onButtonClick = callback;
    }

    private void updateButtonStates() {
        for (PreviewControlButton button : buttons) {
            if (button.type == PreviewControlButton.ControlType.TOGGLE_GRID) {
                button.setEnable(gridVisible);
            } else if (button.type == PreviewControlButton.ControlType.TOGGLE_AXES) {
                button.setEnable(axesVisible);
            } else if (button.type == PreviewControlButton.ControlType.TOGGLE_PLANES) {
                button.setEnable(planesVisible);
            }
        }
    }

    public void setGridVisible(boolean visible) {
        this.gridVisible = visible;
        updateButtonStates();
    }

    public void setAxesVisible(boolean visible) {
        this.axesVisible = visible;
        updateButtonStates();
    }

    public void setPlanesVisible(boolean visible) {
        this.planesVisible = visible;
        updateButtonStates();
    }

    public boolean isGridVisible() { return gridVisible; }
    public boolean isAxesVisible() { return axesVisible; }
    public boolean isPlanesVisible() { return planesVisible; }

    @Override
    public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
        // 绘制工具栏背景
        guiGraphics.fill(this.getX() - 2, this.getY() - 2,
                this.getX() + this.width + 2, this.getY() + this.height + 2,
                0x80000000);
        guiGraphics.renderOutline(this.getX() - 2, this.getY() - 2,
                this.width + 4, this.height + 4,
                0xFF555555);

        // 渲染所有按钮
        for (PreviewControlButton button : buttons) {
            button.render(guiGraphics, mouseX, mouseY, partialTick);
        }
    }

    @Override
    public boolean mouseClicked(double mouseX, double mouseY, int button) {
        for (PreviewControlButton btn : buttons) {
            if (btn.isMouseOver(mouseX, mouseY)) {
                return btn.mouseClicked(mouseX, mouseY, button);
            }
        }
        return false;
    }

    @Override
    public boolean mouseReleased(double mouseX, double mouseY, int button) {
        for (PreviewControlButton btn : buttons) {
            if (btn.isMouseOver(mouseX, mouseY)) {
                return btn.mouseReleased(mouseX, mouseY, button);
            }
        }
        return false;
    }

    @Override
    public void mouseMoved(double mouseX, double mouseY) {
        for (PreviewControlButton btn : buttons) {
            btn.mouseMoved(mouseX, mouseY);
        }
    }

    @Override
    public boolean mouseDragged(double mouseX, double mouseY, int button, double dragX, double dragY) {
        for (PreviewControlButton btn : buttons) {
            if (btn.isMouseOver(mouseX, mouseY)) {
                return btn.mouseDragged(mouseX, mouseY, button, dragX, dragY);
            }
        }
        return false;
    }

    @Override
    public void updateWidgetNarration(@NotNull NarrationElementOutput narrationElementOutput) {
        // 无障碍功能支持
    }
}
