package com.draga.manager.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
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.draga.Constants;
import com.draga.manager.FontManager;
import com.draga.manager.ScreenManager;

public class MenuScreen implements Screen
{
    private Stage stage;

    public MenuScreen()
    {
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        Table table = new Table();
        table.setFillParent(true);
        table.pad(((stage.getHeight() + stage.getWidth()) / 2f) / 50f);
        stage.addActor(table);

        Actor headerLabel = getHeaderLabel();
        Actor playButton = getPlayButton();


        table
            .add(headerLabel)
            .top();

        // Add a row with an expanded cell to fill the gap.
        table.row();
        table
            .add()
            .expand();

        table.row();
        ScrollPane levelsScrollPane = getLevelList();
        table.add(levelsScrollPane);

        // Add a row with an expanded cell to fill the gap.
        table.row();
        table
            .add()
            .expand();

        table.row();
        table
            .add(playButton)
            .bottom();

        stage.setDebugAll(Constants.IS_DEBUGGING);
    }

    private ScrollPane getLevelList()
    {
        TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.font = FontManager.getBigFont();
        textButtonStyle.checkedFontColor = Color.GREEN;

        TextButton[] textButtons = new TextButton[] {
            new TextButton("test1", textButtonStyle),
            new TextButton("test2", textButtonStyle),
            new TextButton("test3", textButtonStyle),
            new TextButton("test4", textButtonStyle),
            new TextButton("test5", textButtonStyle),
            new TextButton("test6", textButtonStyle),
            new TextButton("test7", textButtonStyle),
            new TextButton("test8", textButtonStyle),
            new TextButton("test9", textButtonStyle),
            new TextButton("test10", textButtonStyle),
            new TextButton("test11", textButtonStyle),
            new TextButton("test12", textButtonStyle),
            new TextButton("test13", textButtonStyle),
            new TextButton("test14", textButtonStyle),
            new TextButton("test15", textButtonStyle),
            new TextButton("test16", textButtonStyle),
            new TextButton("test17", textButtonStyle),
            new TextButton("test18", textButtonStyle),
            new TextButton("test19", textButtonStyle),
            new TextButton("test20", textButtonStyle),
        };
        ButtonGroup<TextButton> buttonGroup = new ButtonGroup<>();

        buttonGroup.setMaxCheckCount(1);
        buttonGroup.setMinCheckCount(1);
        buttonGroup.setUncheckLast(true);

        VerticalGroup verticalGroup = new VerticalGroup();

        for (TextButton textButton:textButtons){
            buttonGroup.add(textButton);
            verticalGroup.addActor(textButton);
        }

        ScrollPane scrollPane = new ScrollPane(verticalGroup);
        
        return scrollPane;
    }

    @Override
    public void show()
    {

    }

    @Override
    public void render(float deltaTime)
    {
        if (Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE))
        {
            Gdx.app.exit();
        }
        if (Gdx.input.isKeyJustPressed(Input.Keys.ENTER))
        {
            StartGameScreen();
        }

        stage.act(deltaTime);
        stage.draw();
    }

    @Override
    public void dispose()
    {
        Gdx.input.setInputProcessor(null);
        stage.dispose();
    }

    @Override
    public void pause()
    {

    }

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

    @Override
    public void resume()
    {

    }

    @Override
    public void hide()
    {

    }

    public TextButton getPlayButton()
    {
        TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.font = FontManager.getBigFont();

        TextButton playButton = new TextButton("Play", textButtonStyle);

        playButton.addListener(
            new ClickListener()
            {
                @Override
                public void clicked(InputEvent event, float x, float y)
                {
                    StartGameScreen();
                    super.clicked(event, x, y);
                }
            });
        return playButton;
    }

    private void StartGameScreen()
    {
        ScreenManager.setActiveScreen(new LoadingScreen("level/level1.json"));
    }

    public Actor getHeaderLabel()
    {
        Label.LabelStyle labelStyle = new Label.LabelStyle();
        BitmapFont bigFont = FontManager.getBigFont();
        labelStyle.font = bigFont;

        Label headerLabel = new Label("Space Travels 3", labelStyle);

        return headerLabel;
    }
}

@@ -1847,16 +1847,65 @@
 r.GREEN;
+%0A        textButtonStyle.fontColor = Color.WHITE;
 %0A%0A      
@@ -1984,16 +1984,17 @@
 on(%22test
+ 
 1%22, text
@@ -2039,16 +2039,17 @@
 on(%22test
+ 
 2%22, text
@@ -2094,16 +2094,17 @@
 on(%22test
+ 
 3%22, text
@@ -2149,16 +2149,17 @@
 on(%22test
+ 
 4%22, text
@@ -2204,16 +2204,17 @@
 on(%22test
+ 
 5%22, text
@@ -2259,16 +2259,17 @@
 on(%22test
+ 
 6%22, text
@@ -2314,16 +2314,17 @@
 on(%22test
+ 
 7%22, text
@@ -2369,16 +2369,17 @@
 on(%22test
+ 
 8%22, text
@@ -2424,16 +2424,17 @@
 on(%22test
+ 
 9%22, text
@@ -2479,16 +2479,17 @@
 on(%22test
+ 
 10%22, tex
@@ -2535,16 +2535,17 @@
 on(%22test
+ 
 11%22, tex
@@ -2591,16 +2591,17 @@
 on(%22test
+ 
 12%22, tex
@@ -2647,16 +2647,17 @@
 on(%22test
+ 
 13%22, tex
@@ -2703,16 +2703,17 @@
 on(%22test
+ 
 14%22, tex
@@ -2759,16 +2759,17 @@
 on(%22test
+ 
 15%22, tex
@@ -2815,16 +2815,17 @@
 on(%22test
+ 
 16%22, tex
@@ -2871,16 +2871,17 @@
 on(%22test
+ 
 17%22, tex
@@ -2927,16 +2927,17 @@
 on(%22test
+ 
 18%22, tex
@@ -2983,16 +2983,17 @@
 on(%22test
+ 
 19%22, tex
@@ -3039,16 +3039,17 @@
 on(%22test
+ 
 20%22, tex
@@ -3539,24 +3539,16 @@
 Group);%0A
-        
 %0A       

set font color for unchecked level