package de.frajul.endlessroll.entities.guis;

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

import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.textures.TexturePack;
import de.frajul.endlessroll.entities.tools.ToolType;
import de.frajul.endlessroll.main.game.Game;
import de.frajul.endlessroll.user.ToolSlotSettings;

public class ToolButtonBar {

    private final int BUTTON_COUNT = 4;
    private final float BUTTON_HEIGHT = 0.34f;
    private final float BUTTON_WIDTH = BUTTON_HEIGHT * 1.17f;
    private final float BUTTON_MARGIN = 0.012f;

    private List<ToolButton> toolButtons = new ArrayList<>();

    public ToolButtonBar(TexturePack texturePack, Game game, ToolSlotSettings toolSlotSettings) {
        float buttonWidth = BUTTON_WIDTH + 2 * BUTTON_MARGIN;
        float totalWidth = BUTTON_COUNT * buttonWidth;

        for (int i = 0; i < BUTTON_COUNT; i++) {
            Vector position = new Vector(-totalWidth / 2 + (i + 0.5f) * buttonWidth,
                    1 - (0.5f * BUTTON_HEIGHT + BUTTON_MARGIN * 2));
            ToolButton toolButton = new ToolButton(position, BUTTON_WIDTH, BUTTON_HEIGHT,
                    texturePack, game);
            toolButton.setToolSlot(toolSlotSettings.get(i));
            toolButtons.add(toolButton);
        }
    }

    public void update(float frameTime, boolean gameRunning) {
        for (ToolButton button : toolButtons)
            button.update(frameTime, gameRunning);
    }

    public void changeToolButtonTypes(ToolSlotSettings toolSlotSettings) {
        for (int i = 0; i < 4; i++) {
            toolButtons.get(i).setToolSlot(toolSlotSettings.get(i));
        }
    }

    public void reset(ToolSlotSettings toolSlotSettings) {
        changeToolButtonTypes(toolSlotSettings);

        boolean activeButtonAlreadySet = false;
        for (ToolButton toolButton : toolButtons) {
            if (!activeButtonAlreadySet && toolButton.getToolType() != null) {
                toolButton.setActive(true);
                activeButtonAlreadySet = true;
            } else
                toolButton.setActive(false);
            toolButton.updateBorderColor();
            toolButton.setProgress(1);
            toolButton.getAnimationLayer().reset();
        }
    }

    public void setActive(ToolType activeType) {
        for (ToolButton button : toolButtons)
            button.setActive(button.getToolType() == activeType);
    }

    public ToolButton getActiveButton() {
        for (ToolButton button : toolButtons)
            if (button.getActive().get())
                return button;
        return null;
    }

    public List<Gui> toGuiList() {
        List<Gui> list = new ArrayList<>();
        for (ToolButton button : toolButtons) {
            list.add(button.getBorder());
            list.add(button.getBackground());
            list.add(button.getProgressbar());
            list.add(button.getAnimationLayer());
        }
        return list;
    }

    public List<ToolButton> getToolButtons() {
        return toolButtons;
    }
}
