package com.gmail.nossr50.commands.mc;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;

import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.commands.CommandHelper;
import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.spout.SpoutStuff;
import com.gmail.nossr50.util.Users;

public class McremoveCommand implements CommandExecutor {
    private final String location;
    private final mcMMO plugin;

    public McremoveCommand (mcMMO plugin) {
        this.plugin = plugin;
        this.location = mcMMO.usersFile;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        String playerName;
        String tablePrefix = Config.getInstance().getMySQLTablePrefix();
        String databaseName = Config.getInstance().getMySQLDatabaseName();
        String usage = ChatColor.RED + "Proper usage is /mcremove <player>"; //TODO: Needs more locale.
        String success;

        if (CommandHelper.noCommandPermissions(sender, "mcmmo.tools.mcremove")) {
            return true;
        }

        switch (args.length) {
        case 1:
            playerName = args[0];
            success = ChatColor.GREEN + playerName + " was successfully removed from the database!"; //TODO: Locale
            break;

        default:
            sender.sendMessage(usage);
            return true;
        }

        /* MySQL */
        if (Config.getInstance().getUseMySQL()) {
            int userId = 0;
            userId = mcMMO.database.getInt("SELECT id FROM " + tablePrefix + "users WHERE user = '" + playerName + "'");

            if (userId > 0) {
                mcMMO.database.write("DELETE FROM "
                        + databaseName + "."
                        + tablePrefix + "users WHERE "
                        + tablePrefix + "users.id=" + userId);

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

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

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

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

                sender.sendMessage(success);

            }
            else {
                sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist"));
            }
        }
        else {
            if (removeFlatFileUser(playerName)) {
                sender.sendMessage(success);
            }
            else {
                sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist"));
            }
        }

        //Force PlayerProfile stuff to update
        OfflinePlayer player = plugin.getServer().getOfflinePlayer(playerName);
        PlayerProfile playerProfile = Users.getProfile(player);

        if (playerProfile != null) {
            playerProfile.getSpoutHud().removeWidgets();
            Users.remove(player.getName());

            if (player.isOnline()) {
                Users.addUser((Player) player);
                SpoutStuff.reloadSpoutPlayer((Player) player);
            }
        }

        return true;
    }

    private boolean removeFlatFileUser(String playerName) {
        boolean worked = false;

        try {
            FileReader file = new FileReader(location);
            BufferedReader in = new BufferedReader(file);
            StringBuilder writer = new StringBuilder();
            String line = "";

            while ((line = in.readLine()) != null) {

                /* Write out the same file but when we get to the player we want to remove, we skip his line. */
                if (!line.split(":")[0].equalsIgnoreCase(playerName)) {
                    writer.append(line).append("\r\n");
                }
                else {
                    System.out.println("User found, removing...");
                    worked = true;
                    continue; //Skip the player
                }
            }

            in.close();
            FileWriter out = new FileWriter(location); //Write out the new file
            out.write(writer.toString());
            out.close();

            return worked;
        }
        catch (Exception e) {
            plugin.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString());
            return worked;
        }
    }
}

@@ -117,16 +117,44 @@
 eWriter;
+%0Aimport java.io.IOException;
 %0A%0Aimport
@@ -4185,16 +4185,82 @@
 false;%0A%0A
+        BufferedReader in = null;%0A        FileWriter out = null;%0A%0A
         
@@ -4329,39 +4329,24 @@
             
-BufferedReader 
 in = new Buf
@@ -4986,43 +4986,8 @@
     
-in.close();%0A            FileWriter 
 out 
@@ -5085,61 +5085,8 @@
 ));%0A
-            out.close();%0A%0A            return worked;%0A
     
@@ -5262,32 +5262,504 @@
 ing());%0A        
+%7D%0A        finally %7B%0A            if (in != null) %7B%0A                try %7B%0A                    in.close();%0A                %7D%0A                catch (IOException ex) %7B%0A                    ex.printStackTrace();%0A                %7D%0A            %7D%0A%0A            if (out != null) %7B%0A                try %7B%0A                    out.close();%0A                %7D%0A                catch (IOException ex) %7B%0A                    ex.printStackTrace();%0A                %7D%0A            %7D%0A        %7D%0A%0A    
     return worke
@@ -5760,26 +5760,16 @@
  worked;
-%0A        %7D
 %0A    %7D%0A%7D

Improve file handling.