package eu.midnightdust.yaytris.ui;

import eu.midnightdust.yaytris.util.CatppuccinColor;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

import static eu.midnightdust.yaytris.ui.TetrisUI.scale;

public class ScoreMenu extends AbstractMenu {
    final TetrisUI ui;
    final PreviewCanvas previewCanvas;
    final JLabel gameOverLabel;
    final JLabel currentScoreLabel;
    final JLabel currentLevelLabel;
    final JLabel currentTimeLabel;

    ScoreMenu(int x, int y, int width, int height, TetrisUI ui) {
        this.ui = ui;
        this.setBounds(x, y, width, height);
        this.setLayout(null);

        this.previewCanvas = new PreviewCanvas(ui);
        this.previewCanvas.setBounds(scale(92), scale(4), scale(40), scale(40));
        this.previewCanvas.setBackground(Color.BLACK);
        this.previewCanvas.setBorder(new LineBorder(CatppuccinColor.SURFACE0.getColor()));
        this.add(previewCanvas, 0);

        this.gameOverLabel = new JLabel();
        this.add(gameOverLabel);

        this.currentScoreLabel = new JLabel("Score: 0");
        this.add(currentScoreLabel);

        this.currentLevelLabel = new JLabel("Level: 0");
        this.add(currentLevelLabel);

        this.currentTimeLabel = new JLabel("Zeit: 00:00");
        this.add(currentTimeLabel);

        JButton backButton = new JButton("Zurück");
        backButton.addActionListener(ui::openMainMenu);
        this.add(backButton);
    }

    public void updateScore(int score) {
        this.currentScoreLabel.setText(String.format("Score: %s", score));
        this.repaint();
    }

    public void updateTime(LocalTime startingTime) {
        LocalTime timeElapsed = LocalTime.ofNanoOfDay(LocalTime.now().toNanoOfDay() - startingTime.toNanoOfDay());

        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(timeElapsed.getHour() > 0 ? "HH:mm:ss" : "mm:ss");
        this.currentTimeLabel.setText(String.format("Zeit: %s", timeFormatter.format(timeElapsed)));
        this.repaint();
    }

    public void gameOver() {
        this.gameOverLabel.setText("Game over :(");
    }

    public void updateLevel(int level) {
        this.currentLevelLabel.setText(String.format("Level: %s", level));
        this.repaint();
    }

    @Override
    public int getSpacing() {
        return 17;
    }
}
