package pathgame.rendering;

import java.util.LinkedList;
import me.hammerle.snuviengine.api.Renderer;
import me.hammerle.snuviengine.api.Texture;
import pathgame.gameplay.MinusStepsValues;
import pathgame.gameplay.Player;

/**
 * A container for holding everything about the renderer for the Head-Up-Display
 *
 * @author julia
 */
public class HUDRenderer
{

    public static final float OFFSET_Y = 40;
    private static final Texture ENERGYBAR = new Texture("resources/energyBars.png");

    /**
     * Recalculates the rendering positions and settings of the HUD-elements on
     * the screen every rendertick based on the gamelogic in the gametick
     *
     * @param r the renderer
     * @param p the current player
     * @param lag the current lag
     */
    public void renderTick(Renderer r, Player p, float lag)//TileMap map, TileMapRenderer map, float lag, float offX, float offY)
    {
        r.translateTo(0.0f, 0.0f);
        r.scale(2.0f, 2.0f);
        r.updateMatrix();

        renderHUDBackgound(r);
        renderObjectiveTracker(r, p);
        renderEnergyBar(r, p);
        renderEnergyText(r, p);
        renderMinusEnergy(r, p, lag);
    }

    private void renderHUDBackgound(Renderer r)
    {
        r.setMixColorEnabled(true);
        r.setColorEnabled(true);
        r.setTextureEnabled(false);
        r.setBlendingEnabled(true);

        r.getColorRenderer().drawRectangle(0, 0, r.getViewWidth(), 20, 0xFF_00_00_00);//ABGR
    }

    private void renderObjectiveTracker(Renderer r, Player p)
    {
        r.setMixColorEnabled(false);
        r.setColorEnabled(true);
        r.setTextureEnabled(true);

        String objectiveTracker = String.valueOf(p.getObjectivesVisited()) + "/" + String.valueOf(p.getObjectivesAmount()) + " Towns";
        r.getFontRenderer().drawString(2, 6, objectiveTracker);
    }

    private void renderEnergyText(Renderer r, Player p)
    {
        r.setMixColorEnabled(false);
        r.setColorEnabled(true);
        r.setTextureEnabled(true);

        String energy = String.valueOf(p.getEnergyLeft()) + "/" + String.valueOf(p.getEnergySupply());

        r.getFontRenderer().drawString(
                r.getViewWidth() * 0.5f
                - r.getFontRenderer().getSize(energy).getWidth() - 15,
                6, energy);
    }

    private void renderEnergyBar(Renderer r, Player p)
    {
        r.setMixColorEnabled(true);
        r.setColorEnabled(false);
        r.setTextureEnabled(true);
        r.setBlendingEnabled(true);

        float energyPercent = 100 / (float) p.getEnergySupply() * (float) p.getEnergyLeft() / 100;
        if(energyPercent < 0)
        {
            energyPercent = 0;
        }
        ENERGYBAR.bind();
        r.getTextureRenderer().drawRectangle(100, 5, (r.getViewWidth() / 2 - 5), 17, 0, 0.0625f, 1, 0.0625f * 2);
        r.getTextureRenderer().drawRectangle(100, 5, (((r.getViewWidth() / 2 - 5) - 100) * (energyPercent)) + 100, 17, 0, 0, 1 * energyPercent, 0.0625f);

    }

    private void renderMinusEnergy(Renderer r, Player p, float lag)
    {
        r.setMixColorEnabled(false);
        r.setColorEnabled(true);
        r.setTextureEnabled(true);

        LinkedList<MinusStepsValues> steps = p.getLastSteps();
        for(MinusStepsValues step : steps)
        {
            String minusEnergy = String.format("&4-%d", step.getValue());
            r.getFontRenderer().drawString(r.getViewWidth() * 0.5f - 63, 9 + step.getLifeTime() + lag, minusEnergy);
        }
    }
}
