package com.ygames.ysoccer.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.ygames.ysoccer.framework.Assets;
import com.ygames.ysoccer.framework.GLGame;
import com.ygames.ysoccer.framework.GLScreen;
import com.ygames.ysoccer.framework.Settings;
import com.ygames.ysoccer.gui.Button;
import com.ygames.ysoccer.gui.Label;
import com.ygames.ysoccer.gui.MenuTheme;
import com.ygames.ysoccer.gui.Widget;
import com.ygames.ysoccer.match.Team;

import static com.ygames.ysoccer.competitions.Competition.Category.DIY_COMPETITION;
import static com.ygames.ysoccer.competitions.Competition.Category.PRESET_COMPETITION;
import static com.ygames.ysoccer.framework.Assets.font10;
import static com.ygames.ysoccer.framework.Assets.font14;
import static com.ygames.ysoccer.framework.Assets.gettext;
import static com.ygames.ysoccer.framework.Font.Align.CENTER;
import static com.ygames.ysoccer.framework.Font.Align.LEFT;
import static com.ygames.ysoccer.framework.GLGame.State.COMPETITION;
import static com.ygames.ysoccer.framework.GLGame.State.NONE;
import static com.ygames.ysoccer.framework.GLGame.State.TRAINING;

public class Main extends GLScreen {

    private static final int MENU_BUTTON_WIDTH = 175;
    private static final int MENU_BUTTON_HEIGHT = 18;
    private static final int MENU_COLUMN_GAP = 45;
    private static final int MENU_ROW_GAP = 23;
    private static final int MENU_TOP = 360;

    public Main(GLGame game) {
        super(game);
        background = new Texture(GLGame.BACKGROUND_IMAGE);

        game.teamList.clear();
        game.setState(NONE, null);
        navigation.folder = Assets.teamsRootFolder;
        navigation.league = null;
        navigation.team = null;

        Widget w;

        w = new GameOptionsButton();
        widgets.add(w);
        setSelectedWidget(w);

        w = new MatchOptionsButton();
        widgets.add(w);

        w = new ControlsButton();
        widgets.add(w);

        w = new EditTacticsButton();
        widgets.add(w);

        w = new EditTeamsButton();
        widgets.add(w);

        w = new FriendlyButton();
        widgets.add(w);

        w = new DiyCompetitionButton();
        widgets.add(w);

        w = new PresetCompetitionButton();
        widgets.add(w);

        w = new TrainingButton();
        widgets.add(w);

        w = new ExitGameButton();
        widgets.add(w);

        w = new OnlineButton();
        widgets.add(w);

        int y = 510;
        if (game.hasCompetition()) {
            w = new ReplayContinueCompetitionButton(y);
            widgets.add(w);

            w = new SaveCompetitionButton(y);
            widgets.add(w);

            y += 80;
        }

        w = new LoadOldCompetitionButton(y);
        widgets.add(w);

        // release date
        w = new Label();
        w.setText("", LEFT, font10);
        w.setPosition(20, game.gui.HEIGHT - 29);
        widgets.add(w);

        if (Settings.development) {
            w = new DeveloperToolsButton();
            widgets.add(w);
        }
    }

    private class GameOptionsButton extends Button {

        GameOptionsButton() {
            setGeometry(game.gui.WIDTH / 2 - MENU_COLUMN_GAP - MENU_BUTTON_WIDTH, MENU_TOP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setColors(MenuTheme.ACTION.brighter());
            setText(gettext("GAME OPTIONS"), CENTER, font10);
        }

        @Override
        public void onFire1Down() {
            game.setScreen(new GameOptions(game));
        }
    }

    private class MatchOptionsButton extends Button {

        MatchOptionsButton() {
            setColors(MenuTheme.ACTION.brighter());
            setGeometry(game.gui.WIDTH / 2 - MENU_COLUMN_GAP - MENU_BUTTON_WIDTH, MENU_TOP + MENU_ROW_GAP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setText(gettext("MATCH OPTIONS"), CENTER, font10);
        }

        @Override
        public void onFire1Down() {
            game.setScreen(new MatchOptions(game));
        }
    }

    private class ControlsButton extends Button {

        ControlsButton() {
            setColors(MenuTheme.ACTION.brighter());
            setGeometry(game.gui.WIDTH / 2 - MENU_COLUMN_GAP - MENU_BUTTON_WIDTH, MENU_TOP + 2 * MENU_ROW_GAP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setText(gettext("CONTROLS"), CENTER, font10);
        }

        @Override
        public void onFire1Down() {
            game.setScreen(new SetupControls(game));
        }
    }

    private class EditTacticsButton extends Button {

        EditTacticsButton() {
            setColors(MenuTheme.ACTION);
            setGeometry(game.gui.WIDTH / 2 - MENU_COLUMN_GAP - MENU_BUTTON_WIDTH, MENU_TOP + 3 * MENU_ROW_GAP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setText(gettext("EDIT TACTICS"), CENTER, font10);
        }

        @Override
        public void onFire1Down() {
            FileHandle teamFileHandle = Assets.teamsRootFolder.child("CUSTOM/team.electronics.json");
            game.tacticsTeam = Assets.json.fromJson(Team.class, teamFileHandle.readString("UTF-8"));
            game.setScreen(new SelectTactics(game));
        }
    }

    private class EditTeamsButton extends Button {

        EditTeamsButton() {
            setColors(MenuTheme.ACTION);
            setGeometry(game.gui.WIDTH / 2 - MENU_COLUMN_GAP - MENU_BUTTON_WIDTH, MENU_TOP + 4 * MENU_ROW_GAP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setText(gettext("EDIT TEAMS"), CENTER, font10);
        }

        @Override
        public void onFire1Down() {
            game.setState(GLGame.State.EDIT, null);
            navigation.competition = null;
            game.setScreen(new SelectFolder(game));
        }
    }

    private class FriendlyButton extends Button {

        FriendlyButton() {
            setGeometry(game.gui.WIDTH / 2 + MENU_COLUMN_GAP, MENU_TOP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setColors(MenuTheme.ACTION);
            setText(gettext("FRIENDLY"), CENTER, font10);
        }

        @Override
        public void onFire1Down() {
            game.setScreen(new DesignFriendly(game));
        }
    }

    private class DiyCompetitionButton extends Button {

        DiyCompetitionButton() {
            setColors(MenuTheme.ACTION);
            setGeometry(game.gui.WIDTH / 2 + MENU_COLUMN_GAP, MENU_TOP + MENU_ROW_GAP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setText(gettext("DIY COMPETITION"), CENTER, font10);
        }

        @Override
        public void onFire1Down() {
            if (game.hasCompetition()) {
                game.setScreen(new CreateCompetitionWarning(game, DIY_COMPETITION));
            } else {
                game.setScreen(new DiyCompetition(game));
            }
        }
    }

    private class PresetCompetitionButton extends Button {

        PresetCompetitionButton() {
            setColors(MenuTheme.ACTION);
            setGeometry(game.gui.WIDTH / 2 + MENU_COLUMN_GAP, MENU_TOP + 2 * MENU_ROW_GAP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setText("MANAGER MODE", CENTER, font10);
        }

        @Override
        public void onFire1Down() {
            if (game.hasCompetition()) {
                game.setScreen(new CreateCompetitionWarning(game, PRESET_COMPETITION));
            } else {
                game.setState(COMPETITION, PRESET_COMPETITION);
                FileHandle managerRoot = Assets.teamsRootFolder.child("2024-25/CLUB_TEAMS");
                navigation.folder = managerRoot.exists() ? managerRoot : Assets.teamsRootFolder;
                navigation.league = null;
                navigation.team = null;
                navigation.competition = null;
                game.setScreen(new ManagerSelectTeam(game));
            }
        }
    }

    private class TrainingButton extends Button {

        TrainingButton() {
            setColors(MenuTheme.ACTION);
            setGeometry(game.gui.WIDTH / 2 + MENU_COLUMN_GAP, MENU_TOP + 3 * MENU_ROW_GAP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setText(gettext("TRAINING"), CENTER, font10);
        }

        @Override
        protected void onFire1Down() {
            game.setState(TRAINING, null);
            navigation.competition = null;
            game.setScreen(new SelectFolder(game));
        }
    }

    private class OnlineButton extends Button {

        OnlineButton() {
            setColors(MenuTheme.ACTION);
            setGeometry(game.gui.WIDTH / 2 + MENU_COLUMN_GAP, MENU_TOP + 4 * MENU_ROW_GAP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setText("ONLINE", CENTER, font10);
        }

        @Override
        protected void onFire1Down() {
            game.setScreen(new OnlineMenu(game));
        }
    }

    private class ExitGameButton extends Button {

        ExitGameButton() {
            setColors(MenuTheme.DANGER);
            setGeometry((game.gui.WIDTH - MENU_BUTTON_WIDTH) / 2, MENU_TOP + 6 * MENU_ROW_GAP, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT);
            setText("EXIT GAME", CENTER, font10);
        }

        @Override
        protected void onFire1Down() {
            Gdx.app.exit();
        }
    }

    private class ReplayContinueCompetitionButton extends Button {

        ReplayContinueCompetitionButton(int y) {
            setColors(MenuTheme.ACTION);
            setGeometry((game.gui.WIDTH - 300) / 2, y, 300, 18);
            String s = gettext(game.competition.isEnded() ? "REPLAY %s" : "CONTINUE %s");
            setText(s.replace("%s", game.competition.name), CENTER, font10);
        }

        @Override
        public void onFire1Up() {
            if (game.competition.isEnded()) {
                game.competition.restart();
            }

            switch (game.competition.category) {
                case DIY_COMPETITION:
                case PRESET_COMPETITION:
                    game.setState(COMPETITION, game.competition.category);
                    switch (game.competition.type) {
                        case LEAGUE:
                            game.setScreen(new PlayLeague(game));
                            break;

                        case CUP:
                            game.setScreen(new PlayCup(game));
                            break;

                        case TOURNAMENT:
                            game.setScreen(new PlayTournament(game));
                            break;
                    }
                    break;
            }
        }
    }

    private class SaveCompetitionButton extends Button {

        SaveCompetitionButton(int y) {
            setColors(MenuTheme.ACTION);
            setGeometry((game.gui.WIDTH - 300) / 2, y + 23, 300, 18);
            String s = gettext("SAVE %s");
            setText(s.replace("%s", game.competition.name), CENTER, font10);
        }

        @Override
        public void onFire1Up() {
            game.setScreen(new SaveCompetition(game));
        }
    }

    private class LoadOldCompetitionButton extends Button {

        LoadOldCompetitionButton(int y) {
            setColors(MenuTheme.VALUE);
            setGeometry((game.gui.WIDTH - 300) / 2, y, 300, 18);
            setText(gettext("LOAD OLD COMPETITION"), CENTER, font10);
        }

        @Override
        public void onFire1Up() {
            if (game.hasCompetition()) {
                game.setScreen(new LoadCompetitionWarning(game));
            } else {
                game.setScreen(new LoadCompetition(game));
            }
        }
    }

    private class DeveloperToolsButton extends Button {

        DeveloperToolsButton() {
            setColors(MenuTheme.VALUE);
            setGeometry((game.gui.WIDTH - 150) / 2, 675, 150, 18);
            setText("DEVELOPER TOOLS", CENTER, font10);
        }

        @Override
        public void onFire1Down() {
            game.setScreen(new DeveloperTools(game));
        }
    }
}
