package cz.jzitnik.game.blocks;

import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.RightClickLogic;
import cz.jzitnik.game.entities.items.InventoryItem;
import cz.jzitnik.game.handlers.rightclick.RightClickHandler;
import cz.jzitnik.game.ui.InventoryClickHandler;
import cz.jzitnik.game.ui.Window;
import cz.jzitnik.tui.ScreenRenderer;
import cz.jzitnik.tui.SpriteList;
import org.jline.terminal.MouseEvent;
import org.jline.terminal.Terminal;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

@RightClickLogic
public class Chest implements RightClickHandler, Serializable {
    private static final int ROW_AMOUNT = 4;
    private static final int COLUMN_AMOUNT = 6;
    private static final int CELL_WIDTH = 50;
    private static final int CELL_HEIGHT = 26;
    private static final int BORDER_SIZE = 2;

    private final InventoryItem[] items = new InventoryItem[ROW_AMOUNT * COLUMN_AMOUNT];
    private int size;

    public void render(Game game, StringBuilder buffer, Terminal terminal, SpriteList spriteList) {
        int widthPixels = COLUMN_AMOUNT * (CELL_WIDTH + BORDER_SIZE) + BORDER_SIZE;
        var inventory = game.getInventory();

        int moveLeft = Math.max(0, (terminal.getWidth() / 2) - (widthPixels / 2));

        List<String> sprites = game.getInventory().getSprites(items, spriteList, inventory.getSelectedItemInv() - 50, game);

        for (int i = 0; i < ROW_AMOUNT; i++) {
            for (int j = 0; j < CELL_HEIGHT; j++) {
                buffer.append("\033[0m").append(" ".repeat(moveLeft));
                for (int k = 0; k < COLUMN_AMOUNT; k++) {
                    buffer.append("\033[38;5;231;48;5;231m▓".repeat(BORDER_SIZE));
                    if (j == 0 || j == CELL_HEIGHT - 1) {
                        buffer.append("\033[38;5;231;48;5;231m▓".repeat(CELL_WIDTH));
                    } else {
                        String sprite = sprites.get(i * COLUMN_AMOUNT + k);
                        if (sprite == null || sprite.isEmpty()) {
                            buffer.append("\033[0m ".repeat(CELL_WIDTH));
                        } else {
                            String[] spriteLines = sprite.split("\n");
                            buffer.append(spriteLines[j - 1]);
                        }
                    }
                    buffer.append("\033[38;5;231;48;5;231m▓".repeat(BORDER_SIZE));
                }

                buffer.append("\n");
            }
        }

        buffer.append("\n".repeat(20));

        size = buffer.toString().split("\n").length;

        game.getInventory().renderFull(buffer, terminal, spriteList, false, Optional.of(size), game);
    }

    public void click(Game game, MouseEvent mouseEvent, Terminal terminal, ScreenRenderer screenRenderer) {
        int x = mouseEvent.getX();
        int y = mouseEvent.getY();
        int widthPixels = COLUMN_AMOUNT * (CELL_WIDTH + BORDER_SIZE) + BORDER_SIZE + 5;
        int heightPixels = ROW_AMOUNT * (CELL_HEIGHT) - 10;
        int moveLeft = Math.max(0, (terminal.getWidth() / 2) - (widthPixels / 2));

        if (x > moveLeft && x <= moveLeft + widthPixels && y > 0 && y <= heightPixels
                && mouseEvent.getType() == MouseEvent.Type.Pressed) {
            if (mouseEvent.getType() != MouseEvent.Type.Pressed)
                return;

            int blockX = (x - moveLeft) / 52;
            int blockY = y / 26;

            InventoryClickHandler.handleItemClick(mouseEvent, game.getInventory(), items,
                    blockY * COLUMN_AMOUNT + blockX, 50, Optional.of(items));

            screenRenderer.render(game);

            return;
        }

        // TODO: Why I need to add 20 here. Like wtf
        InventoryClickHandler.click(mouseEvent, terminal, screenRenderer, game, Optional.of(size + 20),
                Optional.of(items));
    }

    public void breakBlock(Game game) {
        for (var i = 0; i < items.length; i++) {
            if (items[i] == null) {
                continue;
            }
            game.getInventory().addItem(items[i]);
            items[i] = null;
        }
    }

    @Override
    public void onBlockRightClick(int ignored, int ignored2, Game game, ScreenRenderer ignored4) {
        game.setWindow(Window.CHEST);
    }
}
