package com.lubjam16.epiceggsmash.mainMenu;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.lubjam16.epiceggsmash.EpicEggSmash;
import com.lubjam16.epiceggsmash.game.Graphics;
import com.lubjam16.epiceggsmash.game.Sounds;
import com.lubjam16.epiceggsmash.global.Fonts;


/**
 * Created by stonek on 1/30/16.
 */
public class MainMenuScreen
        extends InputAdapter
        implements Screen {

    private GlyphLayout[] eggsGL;
    private GlyphLayout rulesGL;
    private GlyphLayout playGL;
    private GlyphLayout quitGL;
    private GlyphLayout creditsGL;

    private GlyphLayout[] eggsSelectedGL;
    private GlyphLayout rulesSelectedGL;
    private GlyphLayout playSelectedGL;
    private GlyphLayout quitSelectedGL;
    private GlyphLayout creditsSelectedGL;

    private Vector2[] eggsPosition;
    private Vector2 rulesPosition;
    private Vector2 playPosition;
    private Vector2 quitPosition;
    private Vector2 creditsPosition;

    private Vector2[] eggsSelectedPosition;
    private Vector2 rulesSelectedPosition;
    private Vector2 playSelectedPosition;
    private Vector2 quitSelectedPosition;
    private Vector2 creditsSelectedPosition;

    private int selectedEggs = 3;
    private int minEggs = 1;
    private int maxEggs = 5;

    private MainMenuOption selectedOption = MainMenuOption.START;

    private Viewport viewport;
    private SpriteBatch batch;

    EpicEggSmash game;

    public MainMenuScreen(EpicEggSmash epicEggSmash) {
        viewport = new FitViewport(MainMenu.WIDTH, MainMenu.HEIGHT);
        batch = new SpriteBatch();
        game = epicEggSmash;
        initMenuLayout();
        calculateMenuLocations();
    }

    private void initMenuLayout() {
        rulesGL = new GlyphLayout(Fonts.menuItem,"Rules");
        rulesSelectedGL = new GlyphLayout(Fonts.menuItemSelected, "[Rules]");
        playGL = new GlyphLayout(Fonts.menuItem,"Play");
        playSelectedGL = new GlyphLayout(Fonts.menuItemSelected,"[Play]");
        quitGL = new GlyphLayout(Fonts.menuItem,"Quit");
        quitSelectedGL = new GlyphLayout(Fonts.menuItemSelected,"[Quit]");
        creditsGL = new GlyphLayout(Fonts.menuItem,"Credits");
        creditsSelectedGL = new GlyphLayout(Fonts.menuItemSelected, "[Credits]");
        eggsGL = new GlyphLayout[5];
        eggsSelectedGL = new GlyphLayout[5];
        for (int i = 0; i < 5; i++){
            eggsGL[i] = new GlyphLayout(Fonts.menuItem, "Eggs: " + (i+1));
            eggsSelectedGL[i] = new GlyphLayout(Fonts.menuItemSelected, "[<] Eggs: " + (i+1) + " [>]");
        }
    }

    private void calculateMenuLocations() {
        eggsPosition = new Vector2[5];
        eggsSelectedPosition = new Vector2[5];
        for(int i = 0; i < 5; i++){
            eggsPosition[i] = new Vector2(400 - eggsGL[i].width/2,300);
            eggsSelectedPosition[i] = new Vector2(400 - eggsSelectedGL[i].width/2,300);
        }

        playPosition = new Vector2(400 - playGL.width/2,250);
        playSelectedPosition = new Vector2(400 - playSelectedGL.width/2,250);
        rulesPosition = new Vector2(400 - rulesGL.width/2,200);
        rulesSelectedPosition = new Vector2(400 - rulesSelectedGL.width/2,200);
        creditsPosition = new Vector2(400 - creditsGL.width/2,150);
        creditsSelectedPosition = new Vector2(400 - creditsSelectedGL.width/2,150);
        quitPosition = new Vector2(400 - quitGL.width/2,100);
        quitSelectedPosition = new Vector2(400 - quitSelectedGL.width/2,100);

    }

    @Override
    public void show() {
        Gdx.input.setInputProcessor(this);
    }

    @Override
    public void render(float delta) {
        viewport.apply();
        Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.setProjectionMatrix(viewport.getCamera().combined);

        batch.begin();
        batch.draw(Graphics.bg1, 0, 0);

        switch (selectedOption){
            case EGGS:
                Fonts.menuItemSelected.draw(batch,
                        eggsSelectedGL[selectedEggs - 1],
                        eggsSelectedPosition[selectedEggs - 1].x,
                        eggsSelectedPosition[selectedEggs - 1].y);
                Fonts.menuItem.draw(batch,
                        playGL,
                        playPosition.x,
                        playPosition.y);
                Fonts.menuItem.draw(batch,
                        rulesGL,
                        rulesPosition.x,
                        rulesPosition.y);
                Fonts.menuItem.draw(batch,
                        creditsGL,
                        creditsPosition.x,
                        creditsPosition.y);
                Fonts.menuItem.draw(batch,
                        quitGL,
                        quitPosition.x,
                        quitPosition.y);
                break;
            case START:
                Fonts.menuItemSelected.draw(batch,
                        playSelectedGL,
                        playSelectedPosition.x,
                        playSelectedPosition.y);
                Fonts.menuItem.draw(batch,
                        eggsGL[selectedEggs-1],
                        eggsPosition[selectedEggs-1].x,
                        eggsPosition[selectedEggs-1].y);
                Fonts.menuItem.draw(batch,
                        rulesGL,
                        rulesPosition.x,
                        rulesPosition.y);
                Fonts.menuItem.draw(batch,
                        creditsGL,
                        creditsPosition.x,
                        creditsPosition.y);
                Fonts.menuItem.draw(batch,
                        quitGL,
                        quitPosition.x,
                        quitPosition.y);
                break;
            case RULES:
                Fonts.menuItemSelected.draw(batch,
                        rulesSelectedGL,
                        rulesSelectedPosition.x,
                        rulesSelectedPosition.y);
                Fonts.menuItem.draw(batch,
                        eggsGL[selectedEggs-1],
                        eggsPosition[selectedEggs-1].x,
                        eggsPosition[selectedEggs-1].y);
                Fonts.menuItem.draw(batch,
                        playGL,
                        playPosition.x,
                        playPosition.y);
                Fonts.menuItem.draw(batch,
                        creditsGL,
                        creditsPosition.x,
                        creditsPosition.y);
                Fonts.menuItem.draw(batch,
                        quitGL,
                        quitPosition.x,
                        quitPosition.y);
                break;
            case CREDITS:
                Fonts.menuItemSelected.draw(batch,
                        creditsSelectedGL,
                        creditsSelectedPosition.x,
                        creditsSelectedPosition.y);
                Fonts.menuItem.draw(batch,
                        eggsGL[selectedEggs-1],
                        eggsPosition[selectedEggs-1].x,
                        eggsPosition[selectedEggs-1].y);
                Fonts.menuItem.draw(batch,
                        playGL,
                        playPosition.x,
                        playPosition.y);
                Fonts.menuItem.draw(batch,
                        rulesGL,
                        rulesPosition.x,
                        rulesPosition.y);
                Fonts.menuItem.draw(batch,
                        quitGL,
                        quitPosition.x,
                        quitPosition.y);
                break;
            default:
                Fonts.menuItemSelected.draw(batch,
                        quitSelectedGL,
                        quitSelectedPosition.x,
                        quitSelectedPosition.y);
                Fonts.menuItem.draw(batch,
                        eggsGL[selectedEggs-1],
                        eggsPosition[selectedEggs-1].x,
                        eggsPosition[selectedEggs-1].y);
                Fonts.menuItem.draw(batch,
                        playGL,
                        playPosition.x,
                        playPosition.y);
                Fonts.menuItem.draw(batch,
                        rulesGL,
                        rulesPosition.x,
                        rulesPosition.y);
                Fonts.menuItem.draw(batch,
                        creditsGL,
                        creditsPosition.x,
                        creditsPosition.y);
                break;
        }

        batch.draw(Graphics.title,400-Graphics.title.getWidth()/2,300);

        batch.end();
    }

    public void onRightArrowPress(){
        if(selectedOption.equals(MainMenuOption.EGGS)){
            if(selectedEggs < maxEggs){
                Sounds.menuSelected.play(0.55f);
                selectedEggs++;
            }
        }
    }

    public void onLeftArrowPress(){
        if(selectedOption.equals(MainMenuOption.EGGS)){
            if(selectedEggs > minEggs){
                Sounds.menuSelected.play(0.55f);
                selectedEggs--;
            }
        }
    }

    public void onUpArrowPress(){
        selectedOption = selectedOption.getPrevious();
        Sounds.menuChange.play(0.4f);
    }

    public void onDownArrowPress(){
        selectedOption = selectedOption.getNext();
        Sounds.menuChange.play(0.4f);
    }

    public void onEnterPress(){
        switch (selectedOption){
            case CREDITS:
                game.goToCredits();
                Sounds.menuSelected.play(0.55f);
                break;
            case START:
                game.goToGame(selectedEggs);
                Sounds.menuSelected.play(0.55f);
                break;
            case QUIT:
                game.quit();
                Sounds.menuSelected.play(0.55f);
                break;
            case RULES:
                game.goToRules();
                Sounds.menuSelected.play(0.55f);
                break;
            default:
                break;
        }
    }

    @Override
    public void resize(int width, int height) {
        viewport.update(width, height, true);
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        batch.dispose();
    }


    @Override
    public boolean keyDown(int keycode) {

        switch (keycode){
            case Input.Keys.LEFT:
                onLeftArrowPress();
                break;
            case Input.Keys.RIGHT:
                onRightArrowPress();
                break;
            case Input.Keys.UP:
                onUpArrowPress();
                break;
            case Input.Keys.DOWN:
                onDownArrowPress();
                break;
            case Input.Keys.ENTER:
                onEnterPress();
            default:
                break;
        }

        return true;
    }
}
