package com.ForgeEssentials.backup;

import java.io.File;
import java.io.PrintWriter;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.management.ServerConfigurationManager;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.event.world.WorldEvent;

import com.ForgeEssentials.api.ForgeEssentialsRegistrar.PermRegister;
import com.ForgeEssentials.api.modules.FEModule;
import com.ForgeEssentials.api.modules.FEModule.Config;
import com.ForgeEssentials.api.modules.FEModule.Init;
import com.ForgeEssentials.api.modules.FEModule.ModuleDir;
import com.ForgeEssentials.api.modules.FEModule.ServerInit;
import com.ForgeEssentials.api.modules.event.FEModuleInitEvent;
import com.ForgeEssentials.api.modules.event.FEModuleServerInitEvent;
import com.ForgeEssentials.api.permissions.IPermRegisterEvent;
import com.ForgeEssentials.api.permissions.PermissionsAPI;
import com.ForgeEssentials.api.permissions.RegGroup;
import com.ForgeEssentials.api.permissions.query.PermQueryPlayer;
import com.ForgeEssentials.core.ForgeEssentials;
import com.ForgeEssentials.util.FEChatFormatCodes;
import com.ForgeEssentials.util.OutputHandler;

import cpw.mods.fml.common.FMLCommonHandler;

@FEModule(name = "Backups", parentMod = ForgeEssentials.class, configClass = BackupConfig.class)
public class ModuleBackup
{
	@Config
	public static BackupConfig	config;

	@ModuleDir
	public static File			moduleDir;

	public static File			baseFolder;
	public static AutoBackup	autoBackup;
	public static AutoWorldSave	autoWorldSave;

	@Init
	public void load(FEModuleInitEvent e)
	{
		MinecraftForge.EVENT_BUS.register(this);
	}

	@ServerInit
	public void serverStarting(FEModuleServerInitEvent e)
	{
		e.registerServerCommand(new CommandBackup());
		autoBackup = new AutoBackup();
		autoWorldSave = new AutoWorldSave();
		makeReadme();
	}

	@PermRegister
	public void registerPermissions(IPermRegisterEvent event)
	{
		event.registerPermissionLevel("ForgeEssentials.backup.msg", RegGroup.GUESTS);
		event.registerPermissionLevel("ForgeEssentials.backup", RegGroup.OWNERS);
	}

	@ForgeSubscribe
	public void worldUnload(WorldEvent.Unload e)
	{
		if (FMLCommonHandler.instance().getEffectiveSide().isServer())
		{
			if (BackupConfig.backupIfUnloaded)
			{
				new Backup((WorldServer) e.world, false);
			}
		}
	}

	@ForgeSubscribe
	public void worldUnload(WorldEvent.Load e)
	{
		if (FMLCommonHandler.instance().getEffectiveSide().isServer())
		{
			if (BackupConfig.worldSaveing)
			{
				((WorldServer) e.world).canNotSave = !BackupConfig.worldSaveing;
			}
		}
	}

	public static void msg(String msg)
	{
		OutputHandler.info(msg);
		if (!BackupConfig.enableMsg)
			return;
		try
		{
			ServerConfigurationManager server = FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager();
			for (String username : server.getAllUsernames())
			{
				EntityPlayerMP player = server.getPlayerForUsername(username);
				if (PermissionsAPI.checkPermAllowed(new PermQueryPlayer(player, "ForgeEssentials.backup.msg")))
				{
					player.sendChatToPlayer(FEChatFormatCodes.AQUA + msg);
				}
			}
		}
		catch (Exception e)
		{
		}
	}

	private void makeReadme()
	{
		try
		{
			if (!baseFolder.exists())
			{
				baseFolder.mkdirs();
			}
			File file = new File(baseFolder, "README.txt");
			if (file.exists())
				return;
			PrintWriter pw = new PrintWriter(file);

			pw.println("############");
			pw.println("## WARNING ##");
			pw.println("############");
			pw.println("");
			pw.println("DON'T CHANGE ANYTHING IN THIS FOLDER.");
			pw.println("IF YOU DO, AUTOREMOVE WILL SCREW UP.");
			pw.println("");
			pw.println("If you have problems with this, report an issue and don't put:");
			pw.println("\"Yes, I read the readme\" in the issue or your message on github,");
			pw.println("YOU WILL BE IGNORED.");
			pw.println("- The FE Team");

			pw.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

@@ -1557,90 +1557,8 @@
 der;
-%0A%09public static AutoBackup%09autoBackup;%0A%09public static AutoWorldSave%09autoWorldSave;
 %0A%0A%09@
@@ -1776,57 +1776,111 @@
 ;%0A%09%09
-auto
+if (
 Backup
- = new AutoBackup();%0A%09%09autoWorldSave = 
+Config.autoInterval != 0)%0A%09%09%09new AutoBackup();%0A%09%09if (BackupConfig.worldSaveInterval != 0)%0A%09%09%09
 new 

Fix #511