package pathgame.rendering;

import me.hammerle.snuviengine.api.KeyBinding;
import me.hammerle.snuviengine.api.Renderer;
import pathgame.gameplay.Keys;
import pathgame.gameplay.menu.Menu;
import pathgame.gameplay.menu.MenuButton;

/**
 * A container for holding everything about the renderer for the menus
 *
 * @author julia
 */
public class MenuRenderer
{
    private final float keyWidths[] = new float[Keys.KEYS.length];

        /**
     * 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 menu the current menu
     */
    public void renderTick(Renderer r, float lag, Menu menu)
    {
        r.translateTo(0.0f, 0.0f);
        float scale = 2.0f;
        r.scale(scale, scale);
        r.updateMatrix();
        scale = 1.0f / scale;

        MenuButton[] options = menu.getOptions();

        float lastGap = 10.0f;
        float baseBoxHeight = 300.0f * scale;
        float textBoxPaddingY = (100.0f - (20.0f / 3.0f) * options.length) * scale;
        float windowHeight = r.getViewHeight() * scale;
        float windowWidth = r.getViewWidth() * scale;

        float y = (windowHeight - baseBoxHeight) * 0.5f + textBoxPaddingY;
        float textBoxHeight = baseBoxHeight - textBoxPaddingY * 2;
        float step = (textBoxHeight - r.getFontRenderer().getHeight()) / (options.length - 1) - lastGap / (options.length - 2);

        r.setMixColorEnabled(false);
        r.setColorEnabled(true);
        r.setTextureEnabled(false);
        r.setBlendingEnabled(true);

        r.getColorRenderer().drawRectangle(windowWidth * 0.10f, y - textBoxPaddingY * 0.5f, windowWidth * 0.90f, y + textBoxHeight + textBoxPaddingY * 0.5f, 0x50000000);

        r.setTextureEnabled(true);

        if(!menu.isOptionMenu())
        {
            for(int i = 0; i < options.length - 1; i++)
            {
                renderText(options[i].getName(), menu.getActiveIndex() == i, r, windowWidth, y, false);
                y += step;
            }
            y += lastGap;
            renderText(options[options.length - 1].getName(), menu.getActiveIndex() == options.length - 1, r, windowWidth, y, false);
        }
        else
        {
            float max = Float.MIN_VALUE;
            for(int i = 0; i < Keys.KEYS.length; i++)
            {
                keyWidths[i] = r.getFontRenderer().getSize(getKeyName(Keys.KEYS[i])).getWidth();
                if(keyWidths[i] > max)
                {
                    max = keyWidths[i];
                }
            }

            for(int i = 0; i < options.length - 1; i++)
            {
                boolean active = menu.getActiveIndex() == i;
                renderText(options[i].getName(), active, r, windowWidth, y, true);
                r.getFontRenderer().drawString(windowWidth * 0.85f - max * 0.5f - keyWidths[i] * 0.5f, y, addColor(getKeyName(Keys.KEYS[i]), active));
                y += step;
            }
            y += lastGap;
            renderText(options[options.length - 1].getName(), menu.getActiveIndex() == options.length - 1, r, windowWidth, y, true);
        }
    }

    private void renderText(String s, boolean active, Renderer r, float wWidth, float y, boolean left)
    {
        if(left)
        {
            r.getFontRenderer().drawString(wWidth * 0.15f, y, addColor(s, active));
        }
        else
        {
            r.getFontRenderer().drawString(wWidth * 0.5f - (r.getFontRenderer().getSize(s).getWidth() * 0.5f), y, addColor(s, active));
        }
    }
    
    private String addColor(String s, boolean active)
    {
        return (active ? "&f" : "&7") + s;
    }
    
    private String getKeyName(KeyBinding key)
    {
        if(key.isRebinding())
        {
            return "[...]";
        }
        else
        {
            return key.getName();
        }
    }
}