package io.github.m0pt0pmatt.spongesurvivalgames.backups;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.bukkit.scheduler.BukkitRunnable;

import io.github.m0pt0pmatt.spongesurvivalgames.BukkitSurvivalGamesPlugin;
import io.github.m0pt0pmatt.spongesurvivalgames.SurvivalGame;

public class BackupTaker extends BukkitRunnable {

	private static final int ticksPerSecond = 20;
	
	private static final DateFormat format = new SimpleDateFormat("YYMMMMd-hh-mm-ss");
	
	@Override
	public void run() {
		BukkitSurvivalGamesPlugin.plugin.getLogger().info("Scheduling a backup a game...");
		File backups = new File(BukkitSurvivalGamesPlugin.plugin.getDataFolder(), "Backups");
		
		if (!backups.isDirectory()) {
			BukkitSurvivalGamesPlugin.plugin.getLogger().warning("Found file named 'Backup', but need name "
					+ "for backup folders!\nFile will be deleted!");
			backups.delete();
		}
		
		if (!backups.exists()) {
			backups.mkdirs();
		}
		
		File outFile = new File(backups, 
				"Backup-" + format.format(new Date()));
		
		Backup backup = new Backup(game);
		
		BackupDumper.dumpBackup(backup, outFile);
	}
	
	private SurvivalGame game;
	
	private int ticks;
	
	public BackupTaker(SurvivalGame game, int secondsTillBackup) {
		this.game = game;
		ticks = Math.round(secondsTillBackup * ticksPerSecond);
		
		this.runTaskTimer(BukkitSurvivalGamesPlugin.plugin, ticks, ticks);
	}
	
}

@@ -1055,16 +1055,39 @@
 %09%22Backup
+%5B%22 + game.getID() + %22%5D 
 -%22 + for

Put name in backup name