package com.voxelgameslib.voxelgameslib.persistence;

import com.voxelgameslib.voxelgameslib.VoxelGamesLib;
import com.voxelgameslib.voxelgameslib.user.GamePlayer;
import com.voxelgameslib.voxelgameslib.user.User;
import com.voxelgameslib.voxelgameslib.utils.db.DB;
import com.voxelgameslib.voxelgameslib.utils.db.DbRow;

import java.sql.SQLException;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nonnull;
import javax.inject.Inject;

import lombok.extern.java.Log;

@Log
public class MysqlPersistenceProvider implements PersistenceProvider {
    @Inject
    private VoxelGamesLib voxelGamesLib;

    @Override
    public void start() {
        DB.initialize(voxelGamesLib);

        initialise();
    }

    @Override
    public void stop() {
        DB.close();
    }

    @Override
    public void saveUser(@Nonnull User user) {
        DB.executeUpdateAsync("UPDATE vgl_user SET username = ? WHERE uuid = ?", user.getRawDisplayName(), user.getUuid());
    }

    @Override
    @Nonnull
    public Optional<User> loadUser(@Nonnull UUID id) {
        User user = new GamePlayer();
        DbRow userData;

        try {
            userData = DB.getFirstRow("SELECT * FROM vgl_user WHERE uuid = ?", id);
        } catch (SQLException e) {
            log.severe("Error loading player data for UUID: " + id);
            e.printStackTrace();
            return Optional.empty();
        }

        user.setUuid(userData.get("uuid"));
        user.setDisplayName(userData.get("username"));
        // todo: set more variables

        return Optional.ofNullable(user);
    }

    private void initialise() {
        // todo: create tables if they don't exist
    }
}

@@ -101,16 +101,76 @@
 mesLib;%0A
+import com.voxelgameslib.voxelgameslib.config.GlobalConfig;%0A
 import c
@@ -677,16 +677,61 @@
 amesLib;
+%0A    @Inject%0A    private GlobalConfig config;
 %0A%0A    @O
@@ -772,16 +772,79 @@
         
+// commented out since VGL autoloads this regardless%0A        //
 DB.initi
@@ -862,16 +862,24 @@
 GamesLib
+, config
 );%0A%0A    
@@ -950,16 +950,18 @@
         
+//
 DB.close

update this class so it compiles