package com.mygdx.seabattle.views;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.mygdx.seabattle.SeaBattle;

/**
 * Created by Torstein on 16.03.2015.
 */
public class MainMenuScreen implements Screen{
    private SpriteBatch batch;
    private Stage stage;
    private TextField nickNameField;
    private TextButton playButton, exitButton, howToPlayButton;
    private Label nickNameLabel;
    private SeaBattle seaBattleGame;
    private Skin skin;
    private int width, height;

    public MainMenuScreen(SeaBattle seaBattleGame) {
        this.seaBattleGame = seaBattleGame;
        width = seaBattleGame.getWidth();
        height = seaBattleGame.getHeight();
        skin = seaBattleGame.getSkin();
    }

    @Override
    public void show() {
        batch = new SpriteBatch();
        stage = new Stage();
        Gdx.input.setInputProcessor(stage); // So that the stage can receive input-events like button-clicks

        /** Create menu elements **/
        nickNameLabel = new Label("Enter nickname:", skin);
        nickNameField = new TextField("", skin);
        playButton = new TextButton("Find match", skin);
        exitButton = new TextButton("Exit", skin);
        howToPlayButton = new TextButton("How to play", skin);

        /** Calculate and set element sizes **/
        double nickNameFieldWidth = nickNameLabel.getWidth(), nickNameFieldHeight = height/38.4;
        nickNameField.setSize((float) nickNameFieldWidth, (float) nickNameFieldHeight);

        double bW = width/2.4, bH = (height - (7* seaBattleGame.getBorder()) - seaBattleGame.getTitleImg().getHeight() - seaBattleGame.getShipImg().getHeight() - nickNameLabel.getHeight())/3;
        float btnSizeW = (float) bW;
        float btnSizeH = (float) bH;
        playButton.setSize(btnSizeW,btnSizeH);
        exitButton.setSize(btnSizeW, btnSizeH);
        howToPlayButton.setSize(btnSizeW, btnSizeH);

        /** Calculate and set element positions **/
        float nickNameLabelPosX = (width/2) - nickNameLabel.getWidth() - (seaBattleGame.getBorder()/2);
        float nickNameLabelPosY = seaBattleGame.getShipImg().getY() - seaBattleGame.getBorder() - nickNameLabel.getHeight();
        nickNameLabel.setPosition(nickNameLabelPosX, nickNameLabelPosY);

        float nickNameFieldPosX = (width/2) + (seaBattleGame.getBorder()/2);
        float nickNameFieldPosY = nickNameLabelPosY + 5;
        nickNameField.setPosition(nickNameFieldPosX, nickNameFieldPosY);

        float playButtonPosX = (width/2) - (btnSizeW/2);
        float playButtonPosY = nickNameLabelPosY - (seaBattleGame.getBorder()) - btnSizeH;
        playButton.setPosition(playButtonPosX, playButtonPosY);

        float howToPlayButtonPosX = playButtonPosX;
        float howToPlayButtonPosY = playButtonPosY - seaBattleGame.getBorder() - btnSizeH;
        howToPlayButton.setPosition(howToPlayButtonPosX, howToPlayButtonPosY);

        float exitButtonPosX = playButtonPosX;
        float exitButtonPosY = howToPlayButtonPosY - seaBattleGame.getBorder() - btnSizeH;
        exitButton.setPosition(exitButtonPosX, exitButtonPosY);

        /** Add groups and elements to the stage **/
        stage.addActor(seaBattleGame.getBackground());
        seaBattleGame.getBackground().setFillParent(true);
        stage.addActor(seaBattleGame.getTitleImg());
        stage.addActor(seaBattleGame.getShipImg());
        stage.addActor(nickNameLabel);
        stage.addActor(nickNameField);
        nickNameField.setMessageText(randomNickname());
        stage.addActor(playButton);
        stage.addActor(howToPlayButton);
        stage.addActor(exitButton);

        /** Add listeners **/
        playButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                String nickName = nickNameField.getText();
                if (nickName.length() == 0) {
                    nickName = "DefaultPlayer";
                }
                seaBattleGame.findMatch(nickName);
            }
        });
        howToPlayButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                seaBattleGame.setTutorialScreen();
            }
        });
        exitButton.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                seaBattleGame.exit();
            }
        });
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.enableBlending();
        batch.begin();
        stage.draw();
        batch.end();
    }

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

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

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void hide() {
        dispose();
    }

    private String randomNickname() {
        String[] adjectives = {"Awesome", "Mighty", "Dark", "Mean", "Fearsome", "Big", "Cowardly", "Smart", "Hot", "Evil", "Angry"};
        String[] nouns = {"Beaver", "Captain", "Anchovy", "Pirate", "Gangster", "Boy", "Tim", "Knight", "Sirloin", "Mama", "Viking"};
        return adjectives[(int) (Math.random() * adjectives.length)] + nouns[(int) (Math.random() * nouns.length)] + (int) (Math.random() * 100);
    }

    public static void main(String[] args) {
        MainMenuScreen mms = new MainMenuScreen(new SeaBattle());
        System.out.println(mms.randomNickname());
    }
}

@@ -793,16 +793,71 @@
  height;
+%0A    private String defaultNickname = randomNickname();
 %0A%0A    pu
@@ -3849,30 +3849,31 @@
 ageText(
-random
+default
 Nickname
 ());%0A   
@@ -3860,26 +3860,24 @@
 aultNickname
-()
 );%0A        s
@@ -4296,23 +4296,23 @@
 e = 
-%22D
+d
 efault
-Player%22
+Nickname
 ;%0A  
@@ -5591,16 +5591,25 @@
  %22Angry%22
+, %22Sweet%22
 %7D;%0A     
@@ -5734,16 +5734,28 @@
 %22Viking%22
+, %22Champion%22
 %7D;%0A     
@@ -5904,175 +5904,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        MainMenuScreen mms = new MainMenuScreen(new SeaBattle());%0A        System.out.println(mms.randomNickname());%0A    %7D
 %0A%7D%0A

Set nickname to the default generated nickname