package pathgame.rendering;

import me.hammerle.snuviengine.api.Renderer;
import pathgame.gameplay.Level;

/**
 * A container for holding everything about the renderer for the score menu
 *
 * @author julia
 */
public class ScoreMenuRenderer
{
    /**
     * Recalculates the rendering positions and settings of the menu-elements on
     * the screen every rendertick based on the gamelogic in the gametick
     *
     * @param r the renderer
     * @param lag the current lag
     * @param level the current level containing the player and the map
     */
    public void renderTick(Renderer r, float lag, Level level)
    {
        float windowWidth = r.getViewWidth();
        float windowHeight = r.getViewHeight();
        float paddingX = 30;
        float paddingY = 50;
        r.setMixColorEnabled(false);
        r.setColorEnabled(true);
        r.setTextureEnabled(false);
        r.setBlendingEnabled(true);
        r.getColorRenderer().drawRectangle(paddingX, paddingY, windowWidth - paddingX, windowHeight - paddingY, 0x90000000);

        r.setTextureEnabled(true);
        r.translateTo(0.0f, 0.0f);
        float scale = scale(r, 1);

        String message = String.format("&2%d&f of &2%d&f Energy used",
                level.getPlayer().getEnergyUsed(), level.getPlayer().getEnergySupply());

        r.getFontRenderer().drawString((windowWidth * scale - getWidth(r, message)) / 2, (windowHeight * scale - getHeight(r, message)) / 2 - windowHeight * scale * 0.5f * 0.15f, message);

        if((float) level.getPlayer().getEnergyUsed() / level.getPlayer().getEnergySupply() < 0.5f)
        {
            message = "You were better than the algorithm!";
        }
        else if((float) level.getPlayer().getEnergyUsed() / level.getPlayer().getEnergySupply() < 0.6f)
        {
            message = "Only 20% more energy use than the algorithm!";
        }
        else if((float) level.getPlayer().getEnergyUsed() / level.getPlayer().getEnergySupply() < 0.75f)
        {
            message = "Only 50% more energy use than the algorithm!";
        }
        else if((float) level.getPlayer().getEnergyUsed() / level.getPlayer().getEnergySupply() < 1)
        {
            message = "Only twice the energy use of the algorithm!";
        }
        else if((float) level.getPlayer().getEnergyUsed() / level.getPlayer().getEnergySupply() == 1)
        {
            message = "As good as the algorithm!";
        }
        else if((float) level.getPlayer().getEnergyUsed() / level.getPlayer().getEnergySupply() > 1)
        {
            message = "More than twice the energy use of the algorithm!";
        }

        r.getFontRenderer().drawString((windowWidth * scale - getWidth(r, message)) / 2, (windowHeight * scale - getHeight(r, message)) / 2 - windowHeight * scale * 0.5f * (-0.15f), message);

        scale = scale(r, 2);
        if((float) level.getPlayer().getEnergyUsed() / level.getPlayer().getEnergySupply() <= 1)
        {

            message = "Congratulations!";
        }
        else
        {
            message = "What a pity! Out of energy!";
        }
        r.getFontRenderer().drawString((windowWidth * scale - getWidth(r, message)) / 2, (windowHeight * scale - getHeight(r, message)) / 2, message);

        message = "[&7PRESS ENTER&f]";
        r.getFontRenderer().drawString((windowWidth * scale - getWidth(r, message)) / 2, (windowHeight * scale - paddingY * scale - getHeight(r, message)) - 10, message);

    }

    private float getWidth(Renderer r, String s)
    {
        return r.getFontRenderer().getSize(s).getWidth();
    }

    private float getHeight(Renderer r, String s)
    {
        return r.getFontRenderer().getSize(s).getHeight();
    }

    private float scale(Renderer r, int scale)
    {
        r.scale(scale, scale);
        r.updateMatrix();
        return 1.0f / scale;
    }
}
