package huds;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.RunnableAction;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.ggj2k20.game.GameMain;

import helpers.GameInfo;
import helpers.GameManager;
import scenes.GamePlay;


public class MainMenuButtons
{
    private GameMain gameMain;

    private Stage stage; //similar to the screen

    private FitViewport gameViewport;

    private ImageButton playButton;

    private Label gameName;

    public MainMenuButtons(GameMain gameMain)
    {
        this.gameMain = gameMain;
        gameViewport = new FitViewport(GameInfo.W, GameInfo.H, new OrthographicCamera());

        stage = new Stage(gameViewport, gameMain.getBatch());

        // setting who is going to process the buttons touch. Stage is going to register the click
        Gdx.input.setInputProcessor(stage);

        createAndPositionButtons();

        createLabel();

        Table table = new Table();
        table.row();
        table.top();
        table.setFillParent(true);

        table.add(gameName).padTop(150f);
        stage.addActor(table);

        addListeners();
    }

    void createAndPositionButtons()
    {
        playButton = new ImageButton(new SpriteDrawable(new Sprite(new Texture("Buttons/Play.png"))));

        playButton.setPosition(GameInfo.W / 2f, GameInfo.H / 2f, Align.center);

        stage.addActor(playButton);
    }

    void addListeners()
    {
        playButton.addListener(new ChangeListener()
        {
            @Override
            public void changed(ChangeEvent event, Actor actor)
            {
                /*RunnableAction run = new RunnableAction();
                run.setRunnable(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        gameMain.setScreen(new GamePlay(gameMain));
                    }
                });

                SequenceAction sa = new SequenceAction(); // action to show up in order
                sa.addAction(Actions.fadeOut(0.09f));
                sa.addAction(run);

                stage.addAction(sa);*/

                gameMain.setScreen(new GamePlay(gameMain));
                GameManager.getInstance().isPaused = false;

            }
        });
    }

    void createLabel()
    {
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("Fonts/dirty-old-town.regular.ttf"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();

        parameter.size = 125;

        BitmapFont font = generator.generateFont(parameter);

        gameName = new Label("Damn, Dam!", new Label.LabelStyle(font, Color.BLACK));

    }

    public Stage getStage()
    {
        return stage;
    }
}
