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;

// Всплывающее уведомление о разблокировке достижения.
 
public class AchievementPopup {
    
    private static final float POPUP_WIDTH = 400;
    private static final float POPUP_HEIGHT = 80;
    private static final float POPUP_DURATION = 3.0f;
    private static final Color BG_COLOR = new Color(0.1f, 0.1f, 0.2f, 0.95f);
    private static final Color BORDER_COLOR = new Color(1.0f, 0.8f, 0.2f, 1f);
    private static final Color TITLE_COLOR = new Color(1.0f, 0.9f, 0.3f, 1f);
    
    private Achievement currentAchievement;
    private float timer;
    private boolean active;
    
    public AchievementPopup() {
        this.currentAchievement = null;
        this.timer = 0;
        this.active = false;
    }
    
    //  Показать уведомление о достижении
    public void show(Achievement achievement) {
        this.currentAchievement = achievement;
        this.timer = POPUP_DURATION;
        this.active = true;
        Achievements.clearNewUnlock();
    }
    
    /**
     * Обновить таймер
     */
    public void update(float deltaTime) {
        if (active) {
            timer -= deltaTime;
            if (timer <= 0) {
                active = false;
                currentAchievement = null;
            }
        }
    }
    
    /**
     * Активен ли попап
     */
    public boolean isActive() {
        return active && currentAchievement != null;
    }
    
    /**
     * Отрисовать попап достижения
     */
    public void render(SpriteBatch batch, BitmapFont font, GlyphLayout layout, 
                       Texture whitePixel, int screenWidth, int screenHeight) {
        if (!isActive()) {
            return;
        }
        
        float progress = timer / POPUP_DURATION;
        
        // Анимация появления/исчезновения
        float alpha = 1.0f;
        float offsetY = 0;
        
        if (progress > 0.8f) {
            // Появление (первые 20% времени)
            float appearProgress = (1.0f - progress) / 0.2f;
            alpha = appearProgress;
            offsetY = (1.0f - appearProgress) * 50;
        } else if (progress < 0.2f) {
            // Исчезновение (последние 20% времени)
            alpha = progress / 0.2f;
            offsetY = (1.0f - alpha) * 50;
        }
        
        float x = screenWidth / 2f - POPUP_WIDTH / 2f;
        float y = screenHeight - POPUP_HEIGHT - 20 - offsetY;
        
        batch.begin();
        
        // Фон
        batch.setColor(BG_COLOR.r, BG_COLOR.g, BG_COLOR.b, BG_COLOR.a * alpha);
        batch.draw(whitePixel, x, y, POPUP_WIDTH, POPUP_HEIGHT);
        
        // Рамка
        batch.setColor(BORDER_COLOR.r, BORDER_COLOR.g, BORDER_COLOR.b, BORDER_COLOR.a * alpha);
        batch.draw(whitePixel, x, y, POPUP_WIDTH, 3); // низ
        batch.draw(whitePixel, x, y + POPUP_HEIGHT - 3, POPUP_WIDTH, 3); // верх
        batch.draw(whitePixel, x, y, 3, POPUP_HEIGHT); // лево
        batch.draw(whitePixel, x + POPUP_WIDTH - 3, y, 3, POPUP_HEIGHT); // право
        
        // Заголовок "Достижение разблокировано!"
        font.setColor(TITLE_COLOR.r, TITLE_COLOR.g, TITLE_COLOR.b, alpha);
        font.getData().setScale(0.9f);
        String header = Localization.achievementUnlocked();
        layout.setText(font, header);
        font.draw(batch, header, x + POPUP_WIDTH / 2f - layout.width / 2f, y + POPUP_HEIGHT - 15);
        
        // Название достижения
        font.setColor(1, 1, 1, alpha);
        font.getData().setScale(1.1f);
        String title = "🏆 " + currentAchievement.getTitle();
        layout.setText(font, title);
        font.draw(batch, title, x + POPUP_WIDTH / 2f - layout.width / 2f, y + POPUP_HEIGHT - 40);
        
        // Описание
        font.setColor(0.8f, 0.8f, 0.8f, alpha);
        font.getData().setScale(0.8f);
        String desc = currentAchievement.getDescription();
        layout.setText(font, desc);
        font.draw(batch, desc, x + POPUP_WIDTH / 2f - layout.width / 2f, y + 20);
        
        font.getData().setScale(1.0f);
        batch.setColor(Color.WHITE);
        
        batch.end();
    }
}
