package com.mygdx.game;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import java.util.Random;

public class Citizen {
    float x, y;
    float targetX;
    float speed;
    Color clothesColor;
    float walkTimer;
    Building homeBuilding;
    float bobOffset;
    float bobSpeed;
    boolean isMoving;
    float size;
    float emotionTimer;
    boolean isHappy;
    Random random;
    float headSize;
    Planet planet;

    public Citizen(float x, float y, Color clothesColor, Building homeBuilding, Random random, Planet planet) {
        this.x = x;
        this.y = y;
        this.targetX = x + (random.nextFloat() - 0.5f) * 300;
        this.speed = 40 + random.nextFloat() * 60;
        this.clothesColor = clothesColor;
        this.walkTimer = random.nextFloat() * 100;
        this.homeBuilding = homeBuilding;
        this.bobOffset = random.nextFloat() * 100;
        this.bobSpeed = 1 + random.nextFloat() * 2;
        this.isMoving = random.nextBoolean();
        this.size = 0.9f + random.nextFloat() * 0.3f;
        this.headSize = 12 * size;
        this.emotionTimer = random.nextFloat() * 5;
        this.isHappy = random.nextBoolean();
        this.random = random;
        this.planet = planet;
    }

    public void update(float deltaTime) {
        walkTimer += deltaTime;
        bobOffset += deltaTime * bobSpeed;
        emotionTimer += deltaTime;

        if (emotionTimer > 3) {
            isHappy = !isHappy;
            emotionTimer = 0;
        }

        if (isMoving) {
            float direction = targetX > x ? 1 : -1;
            x += direction * speed * deltaTime;

            if (Math.abs(x - targetX) < 10) {
                targetX = homeBuilding.x + (random.nextFloat() - 0.5f) * homeBuilding.width;
                isMoving = random.nextBoolean();
            }
        }

        if (random.nextFloat() < 0.005f) {
            isMoving = !isMoving;
            if (isMoving) {
                targetX = homeBuilding.x + (random.nextFloat() - 0.5f) * homeBuilding.width;
            }
        }
    }

    public void draw(ShapeRenderer shapeRenderer, float time) {
        float currentY = y + (float)Math.sin(bobOffset) * 5;
        float scale = size;

        // Разный цвет кожи для разных планет
        Color skinColor;
        switch (planet) {
            case EARTH:
                skinColor = new Color(1.0f, 0.92f, 0.82f, 1);
                break;
            case MARS:
                skinColor = new Color(0.95f, 0.7f, 0.6f, 1); // Красноватый
                break;
            case VENUS:
                skinColor = new Color(1.0f, 0.85f, 0.7f, 1); // Золотистый
                break;
            default:
                skinColor = new Color(1.0f, 0.92f, 0.82f, 1);
        }

        // Голова
        shapeRenderer.setColor(skinColor);
        shapeRenderer.circle(x, currentY + 25 * scale, headSize);

        // Разные прически для разных планет
        shapeRenderer.setColor(getHairColor());
        switch (planet) {
            case EARTH:
                shapeRenderer.rect(x - headSize * 0.7f, currentY + 25 * scale + headSize * 0.5f,
                    headSize * 1.4f, headSize * 0.5f);
                break;
            case MARS:
                // Антенны вместо волос
                shapeRenderer.setColor(Color.GRAY);
                shapeRenderer.rectLine(x, currentY + 25 * scale + headSize,
                    x, currentY + 25 * scale + headSize + 15 * scale,
                    3 * scale);
                shapeRenderer.circle(x, currentY + 25 * scale + headSize + 15 * scale, 5 * scale);
                break;
            case VENUS:
                // Кристаллическая "прическа"
                shapeRenderer.setColor(new Color(1.0f, 0.9f, 0.3f, 1));
                for (int i = 0; i < 3; i++) {
                    float crystalX = x - headSize * 0.5f + i * headSize * 0.5f;
                    shapeRenderer.triangle(crystalX - 3 * scale, currentY + 25 * scale + headSize * 0.5f,
                        crystalX + 3 * scale, currentY + 25 * scale + headSize * 0.5f,
                        crystalX, currentY + 25 * scale + headSize * 0.5f + 10 * scale);
                }
                break;
        }

        // Тело
        shapeRenderer.setColor(clothesColor);
        shapeRenderer.rect(x - 10 * scale, currentY, 20 * scale, 25 * scale);

        // Глаза
        shapeRenderer.setColor(Color.WHITE);
        shapeRenderer.circle(x - headSize * 0.3f, currentY + 25 * scale + headSize * 0.2f, headSize * 0.25f);
        shapeRenderer.circle(x + headSize * 0.3f, currentY + 25 * scale + headSize * 0.2f, headSize * 0.25f);

        // Зрачки
        shapeRenderer.setColor(getEyeColor());
        shapeRenderer.circle(x - headSize * 0.3f, currentY + 25 * scale + headSize * 0.2f, headSize * 0.1f);
        shapeRenderer.circle(x + headSize * 0.3f, currentY + 25 * scale + headSize * 0.2f, headSize * 0.1f);

        // Блеск в глазах
        shapeRenderer.setColor(Color.WHITE);
        shapeRenderer.circle(x - headSize * 0.35f, currentY + 25 * scale + headSize * 0.25f, headSize * 0.05f);
        shapeRenderer.circle(x + headSize * 0.25f, currentY + 25 * scale + headSize * 0.25f, headSize * 0.05f);

        // Рот
        shapeRenderer.setColor(new Color(0.8f, 0.3f, 0.4f, 1));
        if (isHappy) {
            shapeRenderer.arc(x, currentY + 25 * scale, headSize * 0.4f, 180, 180, 20);
        } else {
            shapeRenderer.rectLine(x - headSize * 0.4f, currentY + 25 * scale,
                x + headSize * 0.4f, currentY + 25 * scale,
                headSize * 0.08f);
        }

        // Руки
        shapeRenderer.setColor(skinColor);
        float armOffset = isMoving ? (float)Math.sin(walkTimer * 10) * 10 * scale : 0;
        shapeRenderer.rectLine(x - 10 * scale, currentY + 15 * scale,
            x - 20 * scale, currentY + 5 * scale - armOffset,
            headSize * 0.2f);
        shapeRenderer.rectLine(x + 10 * scale, currentY + 15 * scale,
            x + 20 * scale, currentY + 5 * scale + armOffset,
            headSize * 0.2f);

        // Ноги
        shapeRenderer.setColor(new Color(0.3f, 0.2f, 0.1f, 1));
        float legOffset = isMoving ? (float)Math.sin(walkTimer * 10 + 3.14f) * 8 * scale : 0;
        shapeRenderer.rectLine(x - 5 * scale, currentY,
            x - 10 * scale, currentY - 15 * scale + legOffset,
            headSize * 0.25f);
        shapeRenderer.rectLine(x + 5 * scale, currentY,
            x + 10 * scale, currentY - 15 * scale - legOffset,
            headSize * 0.25f);

        // Обувь
        shapeRenderer.setColor(Color.BLACK);
        shapeRenderer.rect(x - 12 * scale, currentY - 18 * scale + legOffset,
            headSize * 0.4f, headSize * 0.2f);
        shapeRenderer.rect(x + 8 * scale, currentY - 18 * scale - legOffset,
            headSize * 0.4f, headSize * 0.2f);

        // Щёки
        if (isHappy) {
            shapeRenderer.setColor(new Color(1.0f, 0.7f, 0.7f, 0.5f));
            shapeRenderer.circle(x - headSize * 0.5f, currentY + 25 * scale, headSize * 0.15f);
            shapeRenderer.circle(x + headSize * 0.5f, currentY + 25 * scale, headSize * 0.15f);
        }
    }

    private Color getHairColor() {
        switch (planet) {
            case EARTH: return new Color(0.2f, 0.15f, 0.1f, 1);
            case MARS: return new Color(0.7f, 0.3f, 0.2f, 1);
            case VENUS: return new Color(1.0f, 0.8f, 0.3f, 1);
            default: return new Color(0.2f, 0.15f, 0.1f, 1);
        }
    }

    private Color getEyeColor() {
        switch (planet) {
            case EARTH: return new Color(0.1f, 0.1f, 0.3f, 1);
            case MARS: return new Color(0.3f, 0.1f, 0.1f, 1);
            case VENUS: return new Color(0.3f, 0.2f, 0.1f, 1);
            default: return new Color(0.1f, 0.1f, 0.3f, 1);
        }
    }
}
