package xaero.rotatenjump.gui;

import java.util.ArrayList;
import java.util.List;

import xaero.rotatenjump.game.graphics.Graphics;
import xaero.rotatenjump.gui.elements.Button;

/**
 * Created by xaero on 10/07/2017.
 */

public abstract class Gui {

    public static final float TOUCH_SLIMNESS = 50;
    private final static float[] bgColour = {0.39f, 0.39f, 0.39f, 0.86f};

    protected List<Button> buttons;

    private Button buttonTouched;
    protected float touchedX = -1;
    protected float touchedY = -1;
    private boolean willActivate;

    Gui(){
        buttons = new ArrayList<>();
    }

    public abstract void init();

    public void preRender(float touchX, float touchY){
        if(touchedX == -1 && touchedY == -1 && touchX != -1 && touchY != -1){
            touchedX = touchX;
            touchedY = touchY;
        }
    }

    public void render(float touchX, float touchY){
        for(int i = 0; i < buttons.size(); i++)
            buttons.get(i).render(0, 0);
    }

    public abstract void onClose();

    public void onTouch(float x, float y){
        for(int i = 0; i < buttons.size(); i++){
            Button b = buttons.get(i);
            if(x >= b.getX() && x < b.getX() + b.getW() && y >= b.getY() && y < b.getY() + b.getH()) {
                b.onTouch();
                buttonTouched = b;
                willActivate = true;
                break;
            }
        }
    }

    public void drawBackground(){
        Graphics.color = bgColour;
        Graphics.SQUARE.draw(Graphics.getWidth() / 2, Graphics.getHeight() / 2, 0, 0, 0, Graphics.getWidth(), Graphics.getHeight(), 1);
        //Graphics.fontRenderer.getFontSheetModel().draw(Graphics.getWidth() / 2, Graphics.getHeight() / 2, 0, 900, 900);
        Graphics.color = Graphics.RED;
        //Graphics.fontRenderer.drawCenteredGUIString("MEGA SUPER very amazing test 123! õäöü", Graphics.getWidth() / 2, Graphics.getHeight() / 2, 1);
       // Graphics.fontRenderer.drawGUIString("M", Graphics.getWidth() / 2, Graphics.getHeight() / 2, 1);
       // GameView.textureTest.draw(Graphics.getWidth() / 2, Graphics.getHeight() / 2, 0, 512, 512);
    }

    public void onRelease(float x, float y){
        if(buttonTouched != null)
            buttonTouched.onRelease(willActivate);
        buttonTouched = null;
        touchedY = touchedX = -1;
    }

    public void onMove(float x, float y){
        if(Math.abs(y - touchedY) > Graphics.getHeight() / TOUCH_SLIMNESS)
            willActivate = false;
    }

}
