package com.gmail.nossr50.runnables;

import java.util.ArrayList;
import java.util.HashMap;

import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.util.Database;

public class UserPurgeTask implements Runnable {
    private Plugin plugin;
    private Database database = mcMMO.getPlayerDatabase();
    private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
    private String databaseName = Config.getInstance().getMySQLDatabaseName();

    public UserPurgeTask(Plugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public void run() {
        if (Config.getInstance().getUseMySQL()) {
            purgePowerlessSQL();
            purgeOldSQL();
        }
        else {
            //TODO: Make this work for Flatfile data.
        }
    }

    private void purgePowerlessSQL() {
        String query = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing";
        HashMap<Integer, ArrayList<String>> userslist = database.read("SELECT " + query + ", user_id FROM " + tablePrefix + "skills WHERE " + query + " > 0 ORDER BY " + query + " DESC ");

        int purgedUsers = 0;

        for (int i = 1; i <= userslist.size(); i++) {
            int userId = Integer.valueOf(userslist.get(i).get(1));
            HashMap<Integer, ArrayList<String>> username = database.read("SELECT user FROM " + tablePrefix + "users WHERE id = '" + userId + "'");

            if (username != null && Bukkit.getOfflinePlayer(username.get(1).get(0)).isOnline()) {
                continue;
            }

            deleteFromSQL(userId);
            purgedUsers++;
        }

        plugin.getLogger().info("Purged " + purgedUsers + "users from the database.");
    }

    private void purgeOldSQL() {
        String query = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing";
        HashMap<Integer, ArrayList<String>> userslist = database.read("SELECT " + query + ", user_id FROM " + tablePrefix + "skills WHERE " + query + " > 0 ORDER BY " + query + " DESC ");

        int purgedUsers = 0;

        for (int i = 1; i <= userslist.size(); i++) {
            int userId = Integer.valueOf(userslist.get(i).get(1));
            long lastLoginTime = database.getInt("SELECT lastlogin FROM " + tablePrefix + "users WHERE id = '" + userId + "'") * 1000L;
            long loginDifference = System.currentTimeMillis() - lastLoginTime;

            if (loginDifference > 2630000000L) {
                deleteFromSQL(userId);
                purgedUsers++;
            }
        }

        plugin.getLogger().info("Purged " + purgedUsers + "users from the database.");
    }

    private void deleteFromSQL(int userId) {
        database.write("DELETE FROM "
                + databaseName + "."
                + tablePrefix + "users WHERE "
                + tablePrefix + "users.id=" + userId);
    
        database.write("DELETE FROM "
                + databaseName + "."
                + tablePrefix + "cooldowns WHERE "
                + tablePrefix + "cooldowns.user_id=" + userId);

        database.write("DELETE FROM "
                + databaseName + "."
                + tablePrefix + "huds WHERE "
                + tablePrefix + "huds.user_id=" + userId);

        database.write("DELETE FROM "
                + databaseName + "."
                + tablePrefix + "skills WHERE "
                + tablePrefix + "skills.user_id=" + userId);

        database.write("DELETE FROM "
                + databaseName + "."
                + tablePrefix + "experience WHERE "
                + tablePrefix + "experience.user_id=" + userId);
    }
}

@@ -939,32 +939,91 @@
 werlessSQL() %7B%0D%0A
+        System.out.println(%22Purging powerless users...%22);%0D%0A
         String q
@@ -1126,32 +1126,32 @@
 tics+fishing%22;%0D%0A
-
         HashMap%3C
@@ -1404,32 +1404,112 @@
 size(); i++) %7B%0D%0A
+            System.out.println(%22Checking user %22 + i + %22/%22 + userslist.size());%0D%0A
             int 
@@ -2053,32 +2053,141 @@
 urgeOldSQL() %7B%0D%0A
+        System.out.println(%22Purging old users...%22);%0D%0A        long currentTime = System.currentTimeMillis();%0D%0A
         String q
@@ -2568,32 +2568,112 @@
 size(); i++) %7B%0D%0A
+            System.out.println(%22Checking user %22 + i + %22/%22 + userslist.size());%0D%0A
             int 
@@ -2892,31 +2892,24 @@
 ifference = 
-System.
 currentTimeM
@@ -2907,24 +2907,16 @@
 rentTime
-Millis()
  - lastL
@@ -3217,24 +3217,80 @@
  userId) %7B%0D%0A
+        System.out.println(%22Deleting user %22 + userId);%0D%0A
         data
@@ -4158,32 +4158,32 @@
 rience WHERE %22%0D%0A
-
                 
@@ -4227,23 +4227,103 @@
  + userId);%0D
+%0A%0D%0A        System.out.println(%22User %22 + userId + %22 was successfully removed!%22);%0D
 %0A    %7D%0D%0A%7D%0D%0A

Add some debug so we can find the problem...