package com.mygdx.game;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import java.util.Random;

public class JokeBubble {
    String text;
    float x, y;
    float timer;
    Color bubbleColor;
    float floatOffset;
    float scale;
    Random random;

    public JokeBubble(String text, float x, float y, Random random) {
        this.text = text;
        this.x = x;
        this.y = y;
        this.timer = 0;
        this.random = random;
        this.bubbleColor = new Color(
            random.nextFloat() * 0.3f + 0.7f,
            random.nextFloat() * 0.3f + 0.7f,
            random.nextFloat() * 0.3f + 0.7f,
            0.9f
        );
        this.floatOffset = random.nextFloat() * 100;
        this.scale = 0.9f + random.nextFloat() * 0.2f;
    }

    public void update(float deltaTime) {
        timer += deltaTime;
        floatOffset += deltaTime * 2;
    }

    public boolean isExpired() {
        return timer > 3.0f;
    }

    public void draw(SpriteBatch batch, ShapeRenderer shapeRenderer, BitmapFont font,
                     GlyphLayout glyphLayout, Texture whitePixel) {
        batch.begin();

        float currentY = y + (float)Math.sin(floatOffset) * 8;

        // Пузырь
        font.getData().setScale(0.8f * scale);
        glyphLayout.setText(font, text);
        float bubbleWidth = glyphLayout.width + 25;
        float bubbleHeight = glyphLayout.height + 20;

        // Рисуем пузырь с закругленными краями
        batch.setColor(bubbleColor);

        // Основная часть пузыря
        batch.draw(whitePixel, x - bubbleWidth/2, currentY, bubbleWidth, bubbleHeight);

        // Закругленные углы (эмулируем кругами)
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(bubbleColor);
        shapeRenderer.circle(x - bubbleWidth/2, currentY + bubbleHeight/2, bubbleHeight/2);
        shapeRenderer.circle(x + bubbleWidth/2, currentY + bubbleHeight/2, bubbleHeight/2);
        shapeRenderer.end();

        // Текст
        font.setColor(Color.BLACK);
        font.draw(batch, text, x - glyphLayout.width/2, currentY + bubbleHeight/2 + glyphLayout.height/2);

        // Хвостик пузыря
        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
        shapeRenderer.setColor(bubbleColor);
        shapeRenderer.triangle(
            x - 8, currentY,
            x + 8, currentY,
            x, currentY - 15
        );
        shapeRenderer.end();

        font.getData().setScale(1.0f);
        batch.end();
    }
}
