package com.elmakers.mine.bukkit.data;

import com.elmakers.mine.bukkit.api.data.MageData;
import com.elmakers.mine.bukkit.api.data.MageDataCallback;
import com.elmakers.mine.bukkit.api.magic.MageController;
import com.elmakers.mine.bukkit.utilities.YamlDataFile;
import org.apache.commons.multiverse.io.FilenameUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class YamlMageDataStore extends ConfigurationMageDataStore {
    private File playerDataFolder;
    private File migratedDataFolder;

    @Override
    public void initialize(MageController controller, ConfigurationSection configuration) {
        super.initialize(controller, configuration);
        Plugin plugin = controller.getPlugin();
        String playerFolder = configuration.getString("folder", "players");
        String migrateFolder = configuration.getString("migration_folder", "migrated");
        playerDataFolder = new File(plugin.getDataFolder(), playerFolder);
        playerDataFolder.mkdirs();
        migratedDataFolder = new File(plugin.getDataFolder(), migrateFolder);
    }

    @Override
    public void save(MageData mage, MageDataCallback callback) {
        File playerData = new File(playerDataFolder, mage.getId() + ".dat");
        YamlDataFile saveFile = new YamlDataFile(controller.getLogger(), playerData);
        save(mage, saveFile);
        saveFile.save();
        if (callback != null) {
            callback.run(mage);
        }
    }

    @Override
    public void load(String id, MageDataCallback callback) {
        final File playerFile = new File(playerDataFolder, id + ".dat");
        if (!playerFile.exists()) {
            callback.run(null);
        }
        YamlConfiguration saveFile = YamlConfiguration.loadConfiguration(playerFile);
        MageData data = load(id, saveFile);
        if (callback != null) {
            callback.run(data);
        }
    }

    @Override
    public void delete(String id) {
        File playerData = new File(playerDataFolder, id + ".dat");
        if (playerData.exists()) {
            playerData.delete();
        }
    }

    @Override
    public Collection<String> getAllIds() {
        List<String> ids = new ArrayList<String>();
        File[] files = playerDataFolder.listFiles();
        for (File file : files) {
            ids.add(FilenameUtils.removeExtension(file.getName()));
        }
        return ids;
    }

    @Override
    public void migrate(String id) {
        File playerData = new File(playerDataFolder, id + ".dat");
        if (playerData.exists()) {
            migratedDataFolder.mkdir();
            File migratedData = new File(migratedDataFolder, id + ".dat.migrated");
            playerData.renameTo(migratedData);
        }
    }
}

@@ -261,63 +261,8 @@
 le;%0A
-import org.apache.commons.multiverse.io.FilenameUtils;%0A
 impo
@@ -2440,61 +2440,260 @@
     
-ids.add(FilenameUtils.removeExtension(file.getName())
+String filename = file.getName();%0A            int extensionIndex = filename.lastIndexOf('.');%0A            if (extensionIndex %3E 0) %7B%0A                filename = filename.substring(0, filename.lastIndexOf('.'));%0A            %7D%0A%0A            ids.add(filename
 );%0A 

Remove random reference to apache I/O from multiverse