package de.frajul.endlessroll.entities.guis;

import java.util.concurrent.atomic.AtomicBoolean;

import de.frajul.endlessroll.data.Color4f;
import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.textures.Texture;
import de.frajul.endlessroll.entities.textures.TexturePack;
import de.frajul.endlessroll.entities.tools.ToolSlot;
import de.frajul.endlessroll.entities.tools.ToolType;
import de.frajul.endlessroll.entities.tools.ToolUpgradeType;
import de.frajul.endlessroll.main.game.Game;

public class ToolButton {

    private final Color4f LOCKED = new Color4f(0.29f, 0.29f, 0.29f, 0.9f);
    private final Color4f ACTIVE_READY = new Color4f(0.25f, 0.89f, 0.17f, 0.9f);
    private final Color4f ACTIVE_NOT_READY = new Color4f(0.86f, 0.08f, 0.13f, 0.9f);
    private final Color4f INACTIVE_READY = new Color4f(1.0f, 0.95f, 0.0f, 0.9f);
    private final Color4f INACTIVE_NOT_READY = new Color4f(0.57f, 0.11f, 0.06f, 0.9f);

    private Gui border;
    private Gui background;
    private Progressbar progressbar;
    private ToolButtonAnimationLayer animationLayer;

    private Game game;
    private ToolType toolType;
    private boolean locked;
    private AtomicBoolean active;
    private Texture toolButtonLockedTexture;
    private Texture toolButtonEmptyTexture;

    public ToolButton(Vector position, float width, float height, TexturePack texturePack, Game game) {
        this.game = game;
        active = new AtomicBoolean(false);
        toolButtonLockedTexture = texturePack.toolButtonLocked;
        toolButtonEmptyTexture = texturePack.toolButtonEmpty;

        border = new Gui(texturePack.white, position, width, height);
        border.setRoundValue(0.3f);
        background = new Gui(null, position, height, height);
        progressbar = new Progressbar(texturePack.white, position, height, height, 0);
        progressbar.setColor(new Color4f(0.4f, 0.4f, 0.4f, 0.6f));
        progressbar.setRoundValue(1);
        progressbar.setProgress(0.6f);
        animationLayer = new ToolButtonAnimationLayer(toolButtonLockedTexture, position, height,
                height);
    }

    public void setToolSlot(ToolSlot toolSlot) {
        this.toolType = toolSlot.getToolType();
        this.locked = toolSlot.isLocked();

        if (locked) {
            background.setTexture(toolButtonLockedTexture);
        } else if (toolType == null) {
            background.setTexture(toolButtonEmptyTexture);
        } else {
            background.setTexture(toolType.getToolButtonTexture());
            animationLayer.setTexture(toolType.getToolButtonTexture());
        }
        if (toolType != null)
            progressbar.setMaxTime(toolType.getCurrentUpgradeValue(ToolUpgradeType.COOLDOWN));

        if (locked || toolType == null)
            border.setColor(LOCKED);
        else
            border.setColor(INACTIVE_NOT_READY);
    }

    public void setProgress(float progress) {
        progressbar.setProgress(progress);
        updateBorderColor();
    }

    public boolean hasFinishedLoading() {
        return progressbar.hasFinished();
    }

    public void update(float frameTime, boolean gameRunning) {
        if (gameRunning) {
            if (!progressbar.hasFinished()) {
                progressbar.update(frameTime);
                if (progressbar.hasFinished()) {
                    animationLayer.startAnimation();
                    if (active.get())
                        game.onToolButtonFinishedLoading(toolType);
                }
            }
            updateBorderColor();
        }

        animationLayer.update(frameTime);
    }

    public synchronized void updateBorderColor() {
        if (!locked && toolType != null) {
            if (active.get()) {
                if (progressbar.hasFinished())
                    border.setColor(ACTIVE_READY);
                else
                    border.setColor(ACTIVE_NOT_READY);
            } else {
                if (progressbar.hasFinished())
                    border.setColor(INACTIVE_READY);
                else
                    border.setColor(INACTIVE_NOT_READY);
            }
        }
    }

    public void setActive(boolean active) {
        this.active.set(active);
    }

    public AtomicBoolean getActive() {
        return active;
    }

    public Gui getBorder() {
        return border;
    }

    public Gui getBackground() {
        return background;
    }

    public Progressbar getProgressbar() {
        return progressbar;
    }

    public ToolButtonAnimationLayer getAnimationLayer() {
        return animationLayer;
    }

    public ToolType getToolType() {
        return toolType;
    }
}
