package io.github.thatsmusic99.headsplus.sql;

import io.github.thatsmusic99.headsplus.HeadsPlus;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class PinnedChallengeManager extends SQLManager {

    private static PinnedChallengeManager instance;

    public PinnedChallengeManager() {
        instance = this;
        createTable();
    }

    public static PinnedChallengeManager get() {
        return instance;
    }

    @Override
    public void createTable() {
        CompletableFuture.runAsync(() -> {
            try (Connection connection = implementConnection()) {
                PreparedStatement statement = connection.prepareStatement(
                        "CREATE TABLE IF NOT EXISTS headsplus_pinned_challenges " +
                                "(user_id INT NOT NULL," +
                                "challenge VARCHAR(256) NOT NULL," +
                                "FOREIGN KEY (user_id) REFERENCES headsplus_players(id))"
                );

                statement.executeUpdate();
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
        }, HeadsPlus.async);
    }

    @Override
    public void transferOldData() {

    }

    public CompletableFuture<List<String>> getPinnedChallenges(UUID uuid) {
        return CompletableFuture.supplyAsync(() -> {
            try (Connection connection = implementConnection()) {
                PreparedStatement statement = connection.prepareStatement(
                        "SELECT challenge FROM headsplus_pinned_challenges WHERE headsplus_players.uuid = ? " +
                                "AND headsplus_players.id = user_id");
                statement.setString(1, uuid.toString());
                ResultSet set = statement.executeQuery();
                List<String> challenges = new ArrayList<>();
                while (set.next()) {
                    challenges.add(set.getString("challenge"));
                }
                return challenges;
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
            return new ArrayList<>();
        }, HeadsPlus.async);
    }

    public CompletableFuture<Void> addChallenge(UUID uuid, String challenge) {
        return CompletableFuture.runAsync(() -> {
            try (Connection connection = implementConnection()) {
                PreparedStatement statement = connection.prepareStatement(
                        "INSERT INTO headsplus_pinned_challenges (user_id, challenge) VALUES (?, ?)");
                statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
                statement.setString(2, challenge);

                statement.executeUpdate();
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
        }, HeadsPlus.async);
    }

    public CompletableFuture<Void> removeChallenge(UUID uuid, String challenge) {
        return CompletableFuture.runAsync(() -> {
            try (Connection connection = implementConnection()) {
                PreparedStatement statement = connection.prepareStatement(
                        "DELETE FROM headsplus_pinned_challenges " +
                                "WHERE headsplus_players.id = ? AND challenge = ?");
                statement.setInt(1, PlayerSQLManager.get().getUserID(uuid));
                statement.setString(2, challenge);

                statement.executeUpdate();
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
        }, HeadsPlus.async);
    }
}

@@ -91,16 +91,288 @@
 dsPlus;%0A
+import org.json.simple.JSONArray;%0Aimport org.json.simple.JSONObject;%0Aimport org.json.simple.parser.JSONParser;%0Aimport org.json.simple.parser.ParseException;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;
 %0Aimport 
@@ -1678,16 +1678,1021 @@
 ata() %7B%0A
+        // Checks to see if the storage folder exists%0A        File storageFolder = new File(HeadsPlus.get().getDataFolder(), %22storage%22);%0A        if (!storageFolder.exists() %7C%7C !storageFolder.isDirectory()) return;%0A        // Checks to see if the actual file exists%0A        File pinnedChallenges = new File(storageFolder, %22pinned-challenges.json%22);%0A        if (!pinnedChallenges.exists()) return;%0A        try (FileInputStream file = new FileInputStream(pinnedChallenges)) %7B%0A            // Parse the JSON%0A            JSONObject json = (JSONObject) new JSONParser().parse(new InputStreamReader(file));%0A            for (Object obj : json.keySet()) %7B%0A                String uuid = (String) obj;%0A                JSONArray challenges = (JSONArray) json.get(uuid);%0A                challenges.forEach(challenge -%3E addChallenge(UUID.fromString(uuid), (String) challenge));%0A            %7D%0A        %7D catch (IOException %7C ParseException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        pinnedChallenges.delete();
 %0A    %7D%0A%0A

Allow transfer from old data