package commons.cmd;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
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.xml.TPath;
import commons.application.GLOBAL;
import commons.application.Application;
import commons.application.core.Command;
import commons.utl.CtxUtl;
public class ls extends Command implements FileEventListener, FolderEventListener
{

private static final String ARGS     = "ARGS: [p(rofiles)|s(cripts)|o(thers)]. ";
TPath                       path     = new TPath("/");
List<String>                profiles = null;
List<String>                scripts  = null;
List<String>                others   = null;
String                      what     = "*";

public void process () throws Problem
{
   what = ctx.consume(GLOBAL.CTX_ENVKEY_ARG0, "*");
   path.setPrefix("/");
   path.setSuffix("/");
   profiles = new ArrayList<String>();
   scripts = new ArrayList<String>();
   others = new ArrayList<String>();
   FileSystemScanner fs = new FileSystemScanner(this, this);
   fs.startScan(CtxUtl.scriptSpace(ctx).getAbsolutePath(), true);
   boolean ok = false;
   if (what.equals("*") || what.startsWith("p"))
   {
      Application.ui().print("Profiles: ");
      Collections.sort(profiles);
      for (String m : profiles)
         Application.ui().print(m);
      ok = true;
   }
   if (what.equals("*") || what.startsWith("s"))
   {
      Application.ui().print("Scripts: ");
      Collections.sort(scripts);
      for (String m : scripts)
         Application.ui().print(m);
      ok = true;
   }
   if (what.equals("*") || what.startsWith("o"))
   {
      Application.ui().print("Others: ");
      Collections.sort(others);
      for (String m : others)
         Application.ui().print(m);
      ok = true;
   }
   if (!ok) { throw new Problem("Unknown argument, use '*|p|s|o'"); }
}

public void signContract ()
{}

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)
{
   String m = path.toString() + file.getName() + " (" + file.length() + " byte)";
   String ext = FileUtl.getExtension(file.getAbsolutePath().toLowerCase());
   if (ext.endsWith("profile"))
   {
      if (what.equals("*") || what.startsWith("p")) profiles.add(m);
   }
   else if (ext.endsWith("script"))
   {
      if (what.equals("*") || what.startsWith("s")) scripts.add(m);
   }
   else if (what.equals("*") || what.startsWith("o")) others.add(m);
}

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();
}
}
