package com.mygdx.game;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;
import java.util.Random;

public class Building {
    // Ссылки на данные из GameConfig для обратной совместимости
    public static final String[][] BUILDING_TYPES_BY_PLANET = GameConfig.BUILDING_TYPES_BY_PLANET;
    public static final Color[][][] BUILDING_COLOR_THEMES_BY_PLANET = GameConfig.BUILDING_COLOR_THEMES_BY_PLANET;
    public static final Color[][][] ROOF_COLORS_BY_PLANET = GameConfig.ROOF_COLORS_BY_PLANET;
    public static final float[][] SIZE_SCALE_FACTORS_BY_PLANET = GameConfig.SIZE_SCALE_FACTORS_BY_PLANET;
    public static final Color[][] DETAIL_COLORS_BY_PLANET = GameConfig.DETAIL_COLORS_BY_PLANET;
    public static final Color[][] GLOWING_NAME_COLORS_BY_PLANET = GameConfig.GLOWING_NAME_COLORS_BY_PLANET;

    String type;
    float x;
    float y;
    float width;
    float height;
    Color[] colors;
    Color roofColor;
    Color detailColor;
    int constructionProgress;
    boolean isOverlapping;
    float[] cloudOffsets;
    int colorThemeIndex;
    boolean showName;
    float namePulse;
    Color nameColor;
    float nameYOffset;
    float wobbleOffset;
    float wobbleSpeed;
    float animationTimer;
    float animationSpeed;
    int buildingTypeIndex;
    float sizeScale;
    Random random;
    Planet planet;
    int planetIndex;

    public Building(String type, float x, float y, float width, float height, Random random, Planet planet) {
        this.type = type;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.random = random;
        this.planet = planet;
        this.planetIndex = planet.ordinal();

        this.buildingTypeIndex = findBuildingTypeIndex(type, planet);
        this.sizeScale = SIZE_SCALE_FACTORS_BY_PLANET[planetIndex][buildingTypeIndex];
        this.colorThemeIndex = random.nextInt(3);
        this.colors = BUILDING_COLOR_THEMES_BY_PLANET[planetIndex][buildingTypeIndex];
        this.roofColor = ROOF_COLORS_BY_PLANET[planetIndex][buildingTypeIndex][random.nextInt(2)];

        Color[] planetDetailColors = DETAIL_COLORS_BY_PLANET[planetIndex];
        this.detailColor = planetDetailColors[random.nextInt(planetDetailColors.length)];

        Color[] planetNameColors = GLOWING_NAME_COLORS_BY_PLANET[planetIndex];
        this.nameColor = planetNameColors[random.nextInt(planetNameColors.length)];

        this.constructionProgress = 0;
        this.isOverlapping = false;
        this.showName = false;
        this.namePulse = random.nextFloat() * 100;
        this.nameYOffset = 0;
        this.wobbleOffset = random.nextFloat() * 100;
        this.wobbleSpeed = 0.3f + random.nextFloat() * 0.7f;
        this.animationTimer = random.nextFloat() * 100;
        this.animationSpeed = 0.5f + random.nextFloat() * 1.0f;
        this.cloudOffsets = new float[3];
        for (int i = 0; i < 3; i++) {
            cloudOffsets[i] = random.nextFloat() * 100;
        }
    }

    private int findBuildingTypeIndex(String type, Planet planet) {
        String[] planetBuildings = BUILDING_TYPES_BY_PLANET[planet.ordinal()];
        for (int i = 0; i < planetBuildings.length; i++) {
            if (planetBuildings[i].equals(type)) {
                return i;
            }
        }
        return 0;
    }

    public void addBlock() {
        if (constructionProgress < 3) constructionProgress++;
        if (isComplete()) {
            showName = true;
        }
    }

    public void removeBlock() {
        if (constructionProgress > 0) constructionProgress--;
        if (!isComplete()) {
            showName = false;
        }
    }

    public boolean isComplete() { return constructionProgress >= 3; }
    public float getCurrentHeight() { return (height * constructionProgress) / 3.0f; }
    public Rectangle getBounds() { return new Rectangle(x, y, width, getCurrentHeight()); }
    public Planet getPlanet() { return planet; }

    public void update(float deltaTime) {
        animationTimer += deltaTime * animationSpeed;

        if (showName) {
            namePulse += deltaTime * 2;
            nameYOffset = (float)Math.sin(namePulse * 0.5f) * 3;
        }

        if (isComplete()) {
            wobbleOffset += deltaTime * wobbleSpeed;
        }
    }

    public float getWobbleX() {
        if (!isComplete()) return 0;
        return (float)Math.sin(wobbleOffset) * 2;
    }

    public float getWobbleY() {
        if (!isComplete()) return 0;
        return (float)Math.cos(wobbleOffset * 1.5f) * 1.5f;
    }

    public void createCitizensForBuilding(Array<Citizen> citizens, Random random) {
        int citizenCount = 2 + random.nextInt(3);

        for (int i = 0; i < citizenCount; i++) {
            float citizenX = x + random.nextFloat() * width;
            float citizenY = y - 40;
            Color clothesColor = DETAIL_COLORS_BY_PLANET[planetIndex][random.nextInt(DETAIL_COLORS_BY_PLANET[planetIndex].length)];

            Citizen citizen = new Citizen(citizenX, citizenY, clothesColor, this, random, planet);
            citizens.add(citizen);
        }
    }

    public float getSizeScale() {
        return sizeScale;
    }

    public Color getDetailColor() {
        return detailColor;
    }
}
