package eu.midnightdust.yaytris.ui;

import eu.midnightdust.yaytris.HighScores;
import eu.midnightdust.yaytris.util.CatppuccinColor;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

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

public class HighScoreMenu extends JPanel {
    final TetrisUI ui;

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

        this.add(new JLabel(t("highscores.title")));

        List<String> highscores = new ArrayList<>();
        for (String key : HighScores.scores.keySet()) {
            highscores.add(String.format("%s – %s", HighScores.scores.get(key), key));
        }
        highscores.sort((s1, s2) -> Integer.compare(Integer.parseInt(s2.split("–")[0].replace(" ", "")), Integer.parseInt(s1.split("–")[0].replace(" ", ""))));
        JList<String> highscoreList = new JList<>(highscores.toArray(String[]::new));
        highscoreList.setBackground(CatppuccinColor.BASE.getColor());
        highscoreList.setSelectionForeground(CatppuccinColor.CRUST.getColor());
        JScrollPane highscoreScrollPane = new JScrollPane(highscoreList);
        highscoreScrollPane.setBorder(new LineBorder(CatppuccinColor.SURFACE0.getColor(), 3, true));
        this.add(highscoreScrollPane);
        highscoreScrollPane.setBounds(scale(60), scale(43), scale(100), scale(80));

        JButton backButton = new JButton(t("ui.back"));
        backButton.addActionListener(ui::openMainMenu);
        backButton.setBounds(scale(60), scale(140), scale(100), scale(20));
        this.add(backButton);
    }

    @Override
    public Component add(Component comp) {
        if (comp instanceof JLabel) {
            comp.setBounds(scale(60), scale(30), scale(100), scale(7));
        }
        return super.add(comp);
    }
}
