package commons.cmd;
import java.io.File;
import java.util.List;
import basics.filesystem.file.FileUtl;
import basics.filesystem.scanner.FolderEventListener;
import basics.filesystem.scanner.FileEventListener;
import basics.filesystem.scanner.FileSystemScanner;
import basics.unexpected.Problem;
import basics.utl.Empty;
import basics.utl.SUtl;
import basics.xml.TPath;
import commons.application.core.Command;
import commons.utl.CtxUtl;
public class del extends Command implements FileEventListener, FolderEventListener
{

private static final String MUSTHAVE = "folder,pattern";
private static final String ARGS     = "ARGS: folder:store|xml|inbox|html p:<pattern>. ";
TPath                       path     = new TPath("/");
List<String>                profiles = null;
List<String>                scripts  = null;
List<String>                others   = null;
String                      what     = "*";

public void process () throws Problem
{
   String subfolder = ctx.consume("folder", "").trim();
   String pattern = ctx.consume("pattern", "").trim();
   String folder = null;
   if (subfolder.equals("inbox"))
   {
      folder = CtxUtl.inboxSpace(ctx).getAbsolutePath();
   }
   else if (subfolder.equals("store"))
   {
      folder = CtxUtl.storageHome(ctx).getAbsolutePath();
   }
   else if (subfolder.equals("xml"))
   {
      folder = CtxUtl.xmlSpace(ctx).getAbsolutePath();
   }
   else if (subfolder.equals("html"))
   {
      folder = CtxUtl.htmlSpace(ctx).getAbsolutePath();
   }
   else
   {
      throw new Problem("Parameter f must have one of these values: inbox, store, xml");
   }
   if (Empty.is(pattern)) throw new Problem("Parameter p must not be empty, enter a pattern.");
   FileSystemScanner fs = new FileSystemScanner(this, this);
   fs.setPattern(SUtl.split(pattern, ","));
   fs.startScan(folder, false);
}

public void signContract ()
{
   demand(MUSTHAVE, null);
}

public String description ()
{
   return implName() + " lists the home/work folder's contents. " + ARGS;
}

public boolean acceptFile (File file)
{
   return true;
}

public void nextFile (File file)
{
   FileUtl.delete(file);
}

public void nextFolder (File file)
{
// Manager.ui().put(path.toString() + file.getName());
}

public void changeDown (File file)
{
   path.append(FileUtl.getNameOnly(file.getAbsolutePath()));
}

public void changeUp (File file)
{
   path.removeLast();
}
}
