package de.samdev.colorrunner.screens.menu;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

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

public abstract class BaseMenu implements Screen {

    private final int VIEWPORT_WIDTH = 100;
    private final int VIEWPORT_HEIGHT = 100;

    private List<BaseMenuElement> elements = new ArrayList<BaseMenuElement>();
    private OrthographicCamera cam;
    private SpriteBatch sbatch = new SpriteBatch();

    public BaseMenu() {
        super();

        cam = new OrthographicCamera();
        cam.position.x += VIEWPORT_WIDTH/2;
        cam.position.y += VIEWPORT_HEIGHT/2;
        cam.viewportWidth += VIEWPORT_WIDTH;
        cam.viewportHeight += VIEWPORT_HEIGHT;
        cam.update();

        sbatch.setProjectionMatrix(cam.combined);

        init();
    }

    protected abstract void init();

    protected void add(BaseMenuElement e) {
        elements.add(e);
    }

    @Override
    public void show() {
        // NOP
    }

    @Override
    public void render(float delta) {

        // ####### UPDATE #########

        for (BaseMenuElement e : elements) {
            e.update(delta);
        }

        // ####### RENDER #########

        Gdx.gl.glClearColor(0.22f, 0.22f, 0.22f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        sbatch.begin();
        for (BaseMenuElement e : elements) {
            e.render(sbatch);
        }
        sbatch.end();
    }

    @Override
    public void resize(int width, int height) {
        // NOP
    }

    @Override
    public void pause() {
        // NOP
    }

    @Override
    public void resume() {
        // NOP
    }

    @Override
    public void hide() {
        // NOP
    }

    @Override
    public void dispose() {

    }
}
