package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;

public class InputHandler {
    public static void handleInput(AbsurdCityBuilder1 game) {
        // Проверяем, что мы в игровом режиме
        if (game.getGameState() != GameState.PLAYING) {
            return;
        }

        // Смена типа здания
        if (Gdx.input.isKeyJustPressed(Input.Keys.LEFT)) {
            if (!game.isTyping() && !game.isSwitchingPlanets()) {
                int buildingCount = Building.BUILDING_TYPES_BY_PLANET[game.getCurrentPlanet().ordinal()].length;
                int newType = (game.getSelectedBuildingType() - 1 + buildingCount) % buildingCount;
                game.setSelectedBuildingType(newType);
                game.updateUI();

                game.setMessageText("Выбран: " +
                    Building.BUILDING_TYPES_BY_PLANET[game.getCurrentPlanet().ordinal()][newType]);
                game.setMessageColor(com.badlogic.gdx.graphics.Color.CYAN);
            }
        }

        if (Gdx.input.isKeyJustPressed(Input.Keys.RIGHT)) {
            if (!game.isTyping() && !game.isSwitchingPlanets()) {
                int buildingCount = Building.BUILDING_TYPES_BY_PLANET[game.getCurrentPlanet().ordinal()].length;
                int newType = (game.getSelectedBuildingType() + 1) % buildingCount;
                game.setSelectedBuildingType(newType);
                game.updateUI();

                game.setMessageText("Выбран: " +
                    Building.BUILDING_TYPES_BY_PLANET[game.getCurrentPlanet().ordinal()][newType]);
                game.setMessageColor(com.badlogic.gdx.graphics.Color.CYAN);
            }
        }

        // Смена планеты
        if (!game.isTyping() && !game.isSwitchingPlanets()) {
            if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) {
                Planet nextPlanet = game.getCurrentPlanet().next();
                game.startPlanetSwitch(nextPlanet);
            }

            if (Gdx.input.isKeyJustPressed(Input.Keys.DOWN)) {
                Planet prevPlanet = game.getCurrentPlanet().previous();
                game.startPlanetSwitch(prevPlanet);
            }

            // Быстрые клавиши для планет
            if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_1)) {
                game.startPlanetSwitch(Planet.EARTH);
            }
            if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_2)) {
                game.startPlanetSwitch(Planet.MARS);
            }
            if (Gdx.input.isKeyJustPressed(Input.Keys.NUM_3)) {
                game.startPlanetSwitch(Planet.VENUS);
            }
        }

        if (Gdx.input.isKeyJustPressed(Input.Keys.FORWARD_DEL) ||
            Gdx.input.isKeyJustPressed(Input.Keys.DEL) ||
            (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT) && Gdx.input.isKeyJustPressed(Input.Keys.Z)) ||
            (Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT) && Gdx.input.isKeyJustPressed(Input.Keys.Z))) {

            if (!game.isTyping() && !game.isSwitchingPlanets()) {
                if (game.getCurrentBuilding() != null) {
                    game.setCurrentBuilding(null);
                    game.setCurrentProblem(null);
                    game.setTyping(false);
                    game.getInputText().setLength(0);
                    game.setMessageText("Строительство отменено. Выберите новое место.");
                    game.setMessageColor(com.badlogic.gdx.graphics.Color.YELLOW);
                    game.updateUI();
                } else if (game.getBuildings().size > 0) {
                    game.getBuildings().removeIndex(game.getBuildings().size - 1);
                    game.setMessageText("Последнее здание удалено.");
                    game.setMessageColor(com.badlogic.gdx.graphics.Color.ORANGE);
                } else {
                    game.setMessageText("Нет зданий для удаления.");
                    game.setMessageColor(com.badlogic.gdx.graphics.Color.YELLOW);
                }
            }
        }

        if (Gdx.input.justTouched()) {
            if (!game.isSwitchingPlanets()) {
                float touchX = Gdx.input.getX();
                float touchY = Gdx.input.getY();

                float worldX = touchX * (game.getWIDTH() / (float)Gdx.graphics.getWidth());
                float worldY = game.getHEIGHT() - touchY * (game.getHEIGHT() / (float)Gdx.graphics.getHeight());

                if (worldY > game.getUI_INPUT_HEIGHT()) {
                    BuildingPlacer.handleBuildingClick(game, worldX, worldY);
                }
            }
        }

        if (game.isTyping() && game.getCurrentBuilding() != null && !game.isSwitchingPlanets()) {
            for (int key = Input.Keys.NUM_0; key <= Input.Keys.NUM_9; key++) {
                if (Gdx.input.isKeyJustPressed(key)) {
                    game.getInputText().append(Input.Keys.toString(key).charAt(Input.Keys.toString(key).length() - 1));
                }
            }

            for (int key = Input.Keys.NUMPAD_0; key <= Input.Keys.NUMPAD_9; key++) {
                if (Gdx.input.isKeyJustPressed(key)) {
                    char numChar = (char)('0' + (key - Input.Keys.NUMPAD_0));
                    game.getInputText().append(numChar);
                }
            }

            if (Gdx.input.isKeyJustPressed(Input.Keys.MINUS) || Gdx.input.isKeyJustPressed(Input.Keys.NUMPAD_SUBTRACT)) {
                if (game.getInputText().length() == 0) {
                    game.getInputText().append('-');
                }
            }

            if (Gdx.input.isKeyJustPressed(Input.Keys.BACKSPACE)) {
                if (!Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT) &&
                    !Gdx.input.isKeyPressed(Input.Keys.CONTROL_RIGHT)) {

                    if (game.isTyping() && game.getInputText().length() > 0) {
                        game.getInputText().deleteCharAt(game.getInputText().length() - 1);
                    } else if (!game.isTyping() && game.getCurrentBuilding() != null && game.getCurrentBuilding().constructionProgress > 0) {
                        game.getCurrentBuilding().removeBlock();
                        game.setMessageText("Блок удален! Текущий прогресс: " + game.getCurrentBuilding().constructionProgress + "/3");
                        game.setMessageColor(com.badlogic.gdx.graphics.Color.YELLOW);
                        game.updateUI();
                    }
                }
            }

            if (Gdx.input.isKeyJustPressed(Input.Keys.ENTER)) {
                game.checkAnswer();
            }

            if (Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)) {
                if (game.isTyping()) {
                    game.setTyping(false);
                    game.getInputText().setLength(0);
                    game.setMessageText("Ввод отменен. Можно продолжить строительство.");
                    game.setMessageColor(com.badlogic.gdx.graphics.Color.YELLOW);
                }
            }
        }
    }
}
