package com.tiedup.remake.client.gui.screens;

import com.tiedup.remake.dialogue.conversation.PetRequest;
import com.tiedup.remake.network.ModNetwork;
import com.tiedup.remake.network.master.PacketPetRequest;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.network.chat.Component;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * Screen for pet players to make requests to their Master.
 *
 * Displays 7 request options:
 * - Ask for food
 * - Ask to rest
 * - Request walk (you lead)
 * - Request walk (Master leads)
 * - Ask to be tied
 * - Ask to be untied
 * - End conversation
 *
 */
@OnlyIn(Dist.CLIENT)
public class PetRequestScreen extends BaseInteractionScreen {

    private final int entityId;
    private final String masterName;

    // Layout constants
    private static final int PANEL_WIDTH = 200;
    private static final int PANEL_HEIGHT = 220;
    private static final int MARGIN = 10;
    private static final int BUTTON_HEIGHT = 20;
    private static final int BUTTON_SPACING = 4;

    // Color constants
    private static final int COLOR_WHITE = 0xFFFFFF;
    private static final int COLOR_TITLE = 0x8B008B; // Dark purple (Master color)

    public PetRequestScreen(int entityId, String masterName) {
        super(
            Component.translatable("gui.tiedup.pet_request.title", masterName),
            PANEL_WIDTH,
            PANEL_HEIGHT
        );
        this.entityId = entityId;
        this.masterName = masterName;
    }

    @Override
    protected void init() {
        super.init(); // Center the panel (sets leftPos and topPos)

        int contentX = getContentX(MARGIN);
        int contentWidth = getContentWidth(MARGIN);
        int btnY = topPos + 35;

        // === Request Buttons ===

        // Row 1: Ask for food
        addRequestButton(
            contentX,
            btnY,
            contentWidth,
            PetRequest.REQUEST_FOOD,
            "gui.tiedup.pet_request.food",
            "gui.tiedup.pet_request.food.tooltip"
        );
        btnY += BUTTON_HEIGHT + BUTTON_SPACING;

        // Row 2: Ask to rest
        addRequestButton(
            contentX,
            btnY,
            contentWidth,
            PetRequest.REQUEST_SLEEP,
            "gui.tiedup.pet_request.sleep",
            "gui.tiedup.pet_request.sleep.tooltip"
        );
        btnY += BUTTON_HEIGHT + BUTTON_SPACING;

        // Row 3: Walk options (side by side)
        int halfWidth = (contentWidth - BUTTON_SPACING) / 2;

        // Walk (you lead)
        Button walkPassiveBtn = Button.builder(
            Component.translatable("gui.tiedup.pet_request.walk_passive"),
            b -> sendRequest(PetRequest.REQUEST_WALK_PASSIVE)
        )
            .bounds(contentX, btnY, halfWidth, BUTTON_HEIGHT)
            .tooltip(
                Tooltip.create(
                    Component.translatable(
                        "gui.tiedup.pet_request.walk_passive.tooltip"
                    )
                )
            )
            .build();
        this.addRenderableWidget(walkPassiveBtn);

        // Walk (Master leads)
        Button walkActiveBtn = Button.builder(
            Component.translatable("gui.tiedup.pet_request.walk_active"),
            b -> sendRequest(PetRequest.REQUEST_WALK_ACTIVE)
        )
            .bounds(
                contentX + halfWidth + BUTTON_SPACING,
                btnY,
                halfWidth,
                BUTTON_HEIGHT
            )
            .tooltip(
                Tooltip.create(
                    Component.translatable(
                        "gui.tiedup.pet_request.walk_active.tooltip"
                    )
                )
            )
            .build();
        this.addRenderableWidget(walkActiveBtn);
        btnY += BUTTON_HEIGHT + BUTTON_SPACING;

        // Row 4: Tie/Untie options (side by side)
        Button tieBtn = Button.builder(
            Component.translatable("gui.tiedup.pet_request.tie"),
            b -> sendRequest(PetRequest.REQUEST_TIE)
        )
            .bounds(contentX, btnY, halfWidth, BUTTON_HEIGHT)
            .tooltip(
                Tooltip.create(
                    Component.translatable("gui.tiedup.pet_request.tie.tooltip")
                )
            )
            .build();
        this.addRenderableWidget(tieBtn);

        Button untieBtn = Button.builder(
            Component.translatable("gui.tiedup.pet_request.untie"),
            b -> sendRequest(PetRequest.REQUEST_UNTIE)
        )
            .bounds(
                contentX + halfWidth + BUTTON_SPACING,
                btnY,
                halfWidth,
                BUTTON_HEIGHT
            )
            .tooltip(
                Tooltip.create(
                    Component.translatable(
                        "gui.tiedup.pet_request.untie.tooltip"
                    )
                )
            )
            .build();
        this.addRenderableWidget(untieBtn);
        btnY += BUTTON_HEIGHT + BUTTON_SPACING + 10;

        // Row 5: End conversation
        addRequestButton(
            contentX,
            btnY,
            contentWidth,
            PetRequest.END_CONVERSATION,
            "gui.tiedup.pet_request.end",
            "gui.tiedup.pet_request.end.tooltip"
        );
        btnY += BUTTON_HEIGHT + BUTTON_SPACING + 10;

        // Row 6: Cancel button
        Button cancelBtn = Button.builder(
            Component.translatable("gui.tiedup.pet_request.cancel"),
            b -> onClose()
        )
            .bounds(contentX, btnY, contentWidth, BUTTON_HEIGHT)
            .build();
        this.addRenderableWidget(cancelBtn);
    }

    /**
     * Add a request button to the screen.
     */
    private void addRequestButton(
        int x,
        int y,
        int width,
        PetRequest request,
        String translationKey,
        String tooltipKey
    ) {
        Button btn = Button.builder(Component.translatable(translationKey), b ->
            sendRequest(request)
        )
            .bounds(x, y, width, BUTTON_HEIGHT)
            .tooltip(Tooltip.create(Component.translatable(tooltipKey)))
            .build();
        this.addRenderableWidget(btn);
    }

    @Override
    protected void renderContent(
        GuiGraphics graphics,
        int mouseX,
        int mouseY,
        float partialTick
    ) {
        // Title
        renderTitle(graphics, this.title, topPos + 10, COLOR_TITLE);

        // Subtitle
        graphics.drawCenteredString(
            this.font,
            Component.translatable("gui.tiedup.pet_request.subtitle"),
            leftPos + panelWidth / 2,
            topPos + 22,
            COLOR_WHITE
        );
    }

    /**
     * Send a request to the server.
     */
    private void sendRequest(PetRequest request) {
        ModNetwork.sendToServer(new PacketPetRequest(entityId, request));

        // Close screen after sending request
        onClose();
    }
}
