/*
 * Copyright 2015 John "LuaMilkshake" Marion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.pktr.smartbackup.creator;

import net.pktr.smartbackup.Messenger;
import net.pktr.smartbackup.SmartBackup;

import net.minecraft.command.ICommandSender;
import net.minecraft.world.MinecraftException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Handles creation of snapshots.
 *
 * <p>A snapshot is a folder of hardlinks to the world data files. Snapshots provide a
 * restorable state of the world without copying all of the world data every time you take a
 * backup.</p>
 */
public class SnapshotCreator extends BackupCreator {
  /**
   * Sets up a snapshot creation thread.
   *
   * @param sender The ICommandSender that requested this snapshot. Used for status messages.
   */
  public SnapshotCreator(ICommandSender sender) {
    super(sender);
    this.setName("Snapshot Thread");
  }

  @Override
  public void run() {
    Messenger messenger = SmartBackup.getMessenger();

    status = BackupStatus.INPROGRESS;

    messenger.info(requester, "Starting snapshot");

    boolean savingWasEnabled = this.getWorldSaving();
    if (savingWasEnabled) {
      this.changeWorldSaving(false);
    }

    this.savePlayerData();
    try {
      this.saveWorldData();
    } catch (MinecraftException exception) {
      this.error = exception;
      this.status = BackupStatus.FAILED;

      messenger.error(
          requester,
          "Unable to save world data for a snapshot. No data has been backed up.",
          exception
      );

      return;
    }

    // TODO: Create snapshot
    try {
      // Fake work
      sleep(10000);
    } catch (InterruptedException e) {
      endTime = new Date();
      status = BackupStatus.INTERRUPTED;

      messenger.info(
          requester,
          "The snapshot was interrupted. Any temporary backup files will be deleted."
      );

      return;
    }

    if (savingWasEnabled) {
      this.changeWorldSaving(true);
    }

    endTime = new Date();
    status = BackupStatus.COMPLETED;

    SimpleDateFormat rfc8601Formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    rfc8601Formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

    messenger.info(requester, "Snapshot completed at " + rfc8601Formatter.format(endTime));
  }
}

@@ -643,16 +643,65 @@
 eator;%0A%0A
+import net.pktr.smartbackup.BackupConfiguration;%0A
 import n
@@ -727,24 +727,24 @@
 .Messenger;%0A
-
 import net.p
@@ -1595,16 +1595,81 @@
 enger();
+%0A    BackupConfiguration config = SmartBackup.getConfiguration();
 %0A%0A    st
@@ -2236,24 +2236,484 @@
 urn;%0A    %7D%0A%0A
+    String%5B%5D backupIncludes = config.getBackupIncludes();%0A    String%5B%5D backupExcludes = config.getBackupExcludes();%0A%0A    if (backupIncludes.length == 0) %7B%0A      // I might want to use a better error class here%0A      this.error = new Throwable(%22There are no configured targets for backups in the config file!%22);%0A      this.status = BackupStatus.FAILED;%0A      endTime = new Date();%0A      messenger.error(requester, this.error.getMessage());%0A      return;%0A    %7D%0A%0A
     // TODO:

Add loading snapshots targets and check for empty