package de.craften.plugins.minigamemanager.plugin.persistence;

import de.craften.plugins.minigamemanager.plugin.data.Score;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * A minigame persistence that uses yaml files.
 */
public class YamlMinigamePersistence implements MinigamePersistence {
    private final File file;
    private final YamlConfiguration configuration;
    private final Object lock = new Object();

    public YamlMinigamePersistence(File file) {
        this.file = file;
        configuration = YamlConfiguration.loadConfiguration(file);
    }

    @Override
    public void putScore(String gameId, Player player, String levelId, int score) {
        synchronized (lock) {
            if (!configuration.isConfigurationSection(gameId + "." + levelId)) {
                configuration.createSection(gameId + "." + levelId);
            }

            List<Map<?, ?>> scores = configuration.getMapList(gameId + "." + levelId + ".scores");
            if (scores == null) {
                scores = new ArrayList<>();
            }

            Map<String, String> scoreSection = new HashMap<>();
            scoreSection.put("player", player.getUniqueId().toString());
            scoreSection.put("score", String.valueOf(score));
            scores.add(scoreSection);

            configuration.set(gameId + ".scores", scores);

            try {
                configuration.save(file);
            } catch (IOException e) {
                Bukkit.getLogger().severe("[MinigameManager] Could not save score");
            }
        }
    }

    @Override
    public List<Score> getTopScores(String gameId, String levelId, int count) {
        synchronized (lock) {
            if (!configuration.isList(gameId + "." + levelId + ".scores")) {
                return Collections.emptyList();
            }
            List<Map<?, ?>> scores = configuration.getMapList(gameId + "." + levelId + ".scores");
            List<Score> topScores = new ArrayList<>(scores.size());
            for (Map<?, ?> score : scores) {
                topScores.add(new Score(
                        gameId,
                        Bukkit.getOfflinePlayer(UUID.fromString(score.get("player").toString())),
                        Integer.parseInt(score.get("score").toString())
                ));
            }
            Collections.sort(topScores, new Comparator<Score>() {
                @Override
                public int compare(Score a, Score b) {
                    return b.getScore() - a.getScore();
                }
            });
            return topScores.subList(0, Math.min(count, topScores.size()));
        }
    }
}

@@ -799,24 +799,126 @@
 nt score) %7B%0A
+        final String key = gameId + %22.%22 + levelId;%0A        final String scoresKey = key + %22.scores%22;%0A%0A
         sync
@@ -989,38 +989,19 @@
 Section(
-gameId + %22.%22 + levelId
+key
 )) %7B%0A   
@@ -1041,38 +1041,19 @@
 Section(
-gameId + %22.%22 + levelId
+key
 );%0A     
@@ -1054,32 +1054,33 @@
 %0A            %7D%0A%0A
+%0A
             List
@@ -1129,42 +1129,17 @@
 ist(
-gameId + %22.%22 + levelId + %22.
 scores
-%22
+Key
 );%0A 
@@ -1502,26 +1502,17 @@
 set(
-gameId + %22.
 scores
-%22
+Key
 , sc
@@ -1823,24 +1823,126 @@
 nt count) %7B%0A
+        final String key = gameId + %22.%22 + levelId;%0A        final String scoresKey = key + %22.scores%22;%0A%0A
         sync
@@ -2001,42 +2001,17 @@
 ist(
-gameId + %22.%22 + levelId + %22.
 scores
-%22
+Key
 )) %7B
@@ -2139,42 +2139,17 @@
 ist(
-gameId + %22.%22 + levelId + %22.
 scores
-%22
+Key
 );%0A 

Fix scores not saved correctly.