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;

public class HUDRenderer
{
    private static final Texture ENERGYBAR = new Texture("res/energyBar.png");
    private static final Texture ENERGYBARGREY = new Texture("res/energyBarGrey.png");

    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);

    }

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

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

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

        //render ObjectiveTracker
        String objectiveTracker = String.valueOf(p.getObjectivesVisited()) + "/" + String.valueOf(p.getObjectivesAmount()) + " Towns";

        r.getFontRenderer().drawString(2, 1, objectiveTracker);
    }

    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,
                1, energy);
    }

    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;
        ENERGYBARGREY.bind();
        r.getTextureRenderer().drawRectangle(100, 0, (r.getViewWidth() / 2 - 5), 10, 0, 0, 1, 1);

        ENERGYBAR.bind();
        r.getTextureRenderer().drawRectangle(100, 0, (((r.getViewWidth() / 2 - 5) - 100) * (energyPercent)) + 100, 10, 0, 0, 1 * energyPercent, 1);

    }

    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, 3 + step.getLifeTime() + lag, minusEnergy);
        }
    }
}