package jesse.keeblarcraft.ChatStuff;

import java.util.ArrayList;
import java.util.List;

import jesse.keeblarcraft.ChatStuff.ChatFormatting.COLOR_CODE;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

// Create a "chat menu" which is just a menu in chat with pages that a user can click to display
// the next page.
public class ChatMenu {
    private List<Text> msgList = new ArrayList<>();
    private Text header;
    private Text leftArrow;
    private Text rightArrow;
    private int pageLimit = 5; // Messages per page in this menu
    private int pageCount = 1; // Calculated at runtime
    private ChatMsg formatter = new ChatMsg();

    public ChatMenu() {
        // Initialize default header and arrows
        header = Text.of("[----- HELP MENU -----]");
        header = formatter.ColorMsg(header, COLOR_CODE.GOLD);

        leftArrow = Text.of("<<");
        leftArrow = formatter.ColorMsg(leftArrow, COLOR_CODE.GOLD);

        rightArrow = Text.of(">>");
        rightArrow = formatter.ColorMsg(rightArrow, COLOR_CODE.GOLD);
    }

    public void SetHeader(Text header) {
        this.header = header;
    }

    public void SetHeader(ChatMsg msg) {
        this.header = msg.regularText;
    }

    public void SetLeftArrow(Text leftArrow) {
        this.leftArrow = leftArrow;
    }

    public void SetRightArrow(Text rightArrow) {
        this.rightArrow = rightArrow;
    }

    public void AddMsg(Text newMsg) {
        msgList.add(newMsg);
    }

    public void AddMsg(String newMsg) {
        AddMsg(Text.of(newMsg));
    }

    public void AddMsg(ChatMsg newMsg) {
        AddMsg(newMsg.regularText);
    }

    public void ClearList() {
        msgList.clear();
        pageCount = 1;
    }

    public void SetPageLimit(int limit) {
        if (limit >= 1) {
            pageLimit = limit;
        }
    }

    public void SendMsg(ServerPlayerEntity target) {
        // Calculate number of pages
        pageCount = (int) Math.ceil(msgList.size() / (double) pageLimit);

        // Send the header
        target.sendMessage(header);
        target.sendMessage(Text.of("")); // Spacer

        // Send the body
        int msgIndex = 0;
        for (int page = 0; page < pageCount; page++) {
            for (int i = 0; i < pageLimit; i++) {
                target.sendMessage(msgList.get(msgIndex++));
            }
        }

        // Send the footer
        // target.sendMessage(footer);
    }
}
