package com.pqbyte.coherence;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
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.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class MenuScreen extends ScreenAdapter {

  private Stage stage;
  private Coherence game;
  Texture spaceShipImage;

  public MenuScreen(Coherence game) {
    this.game = game;
    Skin skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
    Button button = createButton(skin);
    Label titleLabel = createTitleLabel(skin);

    stage = new Stage();
    stage.addActor(button);
    stage.addActor(titleLabel);

    spaceShipImage = new Texture(Gdx.files.internal("Spaceship.jpg"));

    Gdx.input.setInputProcessor(stage);
  }

  @Override
  public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    SpriteBatch batch = game.batch;
    batch.begin();
    batch.draw(spaceShipImage,500,300);
    batch.end();
    stage.draw();
  }

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

  private Label createTitleLabel(Skin skin) {
    Label label = new Label("COHERENCE", skin);

    // Double font size
    label.setSize(label.getWidth() * 2, label.getHeight());
    label.setFontScale(2);

    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();

    label.setPosition(
        (width - label.getWidth()) / 2,
        (height - label.getHeight()) / 2 + height / 4
    );

    return label;
  }

  private Button createButton(Skin skin) {
    TextButton button = new TextButton("START GAME", skin, "default");
    int buttonWidth = 300;
    int buttonHeight = 60;
    button.setWidth(buttonWidth);
    button.setHeight(buttonHeight);

    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();

    button.setPosition(
        (width - buttonWidth) / 2,
        (height - buttonHeight) / 2 - height / 4
    );

    button.addListener(new ClickListener() {
      @Override
      public void clicked(InputEvent event, float xPos, float yPos) {
        game.setScreen(new GameScreen(game));
        dispose();
      }
    });

    return button;
  }
}

@@ -57,71 +57,8 @@
 dx;%0A
-import com.badlogic.gdx.Input;%0Aimport com.badlogic.gdx.Screen;%0A
 impo
@@ -168,161 +168,15 @@
 ics.
-OrthographicCamera;%0Aimport com.badlogic.gdx.graphics.Texture;%0Aimport com.badlogic.gdx.graphics.g2d.Batch;%0Aimport com.badlogic.gdx.graphics.g2d.BitmapFont
+Texture
 ;%0Aim
@@ -362,32 +362,81 @@
 ne2d.ui.Button;%0A
+import com.badlogic.gdx.scenes.scene2d.ui.Image;%0A
 import com.badlo
@@ -516,16 +516,65 @@
 i.Skin;%0A
+import com.badlogic.gdx.scenes.scene2d.ui.Table;%0A
 import c
@@ -782,34 +782,88 @@
 me;%0A
-  Texture spaceShipImage;%0A
+%0A  /**%0A   * The main menu screen.%0A   *%0A   * @param game The game instance.%0A   */
 %0A  p
@@ -993,44 +993,119 @@
     
-Button button = createButton(skin
+Table table = new Table();%0A    table.setFillParent(true);%0A    stage = new Stage();%0A    stage.addActor(table
 );%0A
+%0A
     
@@ -1151,116 +1151,173 @@
 n);%0A
-%0A
     
-stage = new Stage
+table.add(titleLabel).spaceBottom(20);%0A    table.row
 ();%0A
+%0A
     
-stage.addActor(button);%0A    stage.addActor(titleLabel
+int imageHeight = 300;%0A    int imageWidth = (int) (imageHeight * 1.34
 );%0A
-%0A
     
-spaceShipImage = 
+Image image = new Image(
 new 
@@ -1360,18 +1360,240 @@
 p.jpg%22))
+)
 ;%0A
+    table.add(image).width(imageWidth).height(imageHeight).spaceBottom(100);%0A    table.row();%0A%0A    Button button = createButton(skin);%0A    table.add(button).width(300).height(60);%0A%0A    table.setDebug(Constants.isDebug());
 %0A    Gdx
@@ -1765,120 +1765,8 @@
 T);%0A
-    SpriteBatch batch = game.batch;%0A    batch.begin();%0A    batch.draw(spaceShipImage,500,300);%0A    batch.end();%0A
     
@@ -2059,218 +2059,8 @@
 );%0A%0A
-    int width = Gdx.graphics.getWidth();%0A    int height = Gdx.graphics.getHeight();%0A%0A    label.setPosition(%0A        (width - label.getWidth()) / 2,%0A        (height - label.getHeight()) / 2 + height / 4%0A    );%0A%0A
     
@@ -2182,19 +2182,8 @@
 skin
-, %22default%22
 );%0A 
@@ -2310,209 +2310,8 @@
 );%0A%0A
-    int width = Gdx.graphics.getWidth();%0A    int height = Gdx.graphics.getHeight();%0A%0A    button.setPosition(%0A        (width - buttonWidth) / 2,%0A        (height - buttonHeight) / 2 - height / 4%0A    );%0A%0A
     

Change MenuScreen to use tables