/*=======================================================
 * InterpCommand.java 
 *
 * "A Tcl like language implementation in Java named APWTCL
 * ((Java) Arnulf's Preferred Web Tcl)"
 *
 * APWTCL InterpCommand class
 *
 * Released under same BSD license as Tcl.
 * (Tcl BSD license found at <http://www.tcl.tk/software/tcltk/license.html>)
 *
 * Copyright 2012 Arnulf P. Wiedemann
 *
 */

package org.apwtcl.lang.cmd;

import java.util.ArrayList;
import org.apwtcl.lang.Interp;
import org.apwtcl.lang.ApwtclObj;
import org.apwtcl.lang.InterpAlias;
import org.apwtcl.lang.Token;
import org.apwtcl.lang.Variable;

public class InterpCommand extends Token {

  /* ==================== InterpCommand ================================== */
  public InterpCommand() {
  }

  /* ==================== initSubCmds ================================== */
  public int initSubCmds(Interp interp) {
//print("interp initSubCmds called"+interp+"!");
    interp.command_obj_type.registerNativeSubCommand("::interp", "alias", "InterpCommand");
    return OK;
  }

  /* ==================== interpCmd ================================== */
  public int interpCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("interp called!"+args+"!");
    return OK;
  }

  /* ==================== interpSubcmdsCmd ================================== */
  public int interpSubcmdsCmd(Interp interp, ArrayList<ApwtclObj> args) {
print("interp subcmds called");
    ApwtclObj obj_ptr = interp.string_obj_type.newStringObj("interp", -1, "INTERP_COMMAND_1");
    ApwtclObj result_ptr;
    obj_ptr.incrRefCount("INTERP_COMMAND_2");
    result_ptr = interp.command_obj_type.getSubCommands(obj_ptr);
    obj_ptr.decrRefCount("INTERP_COMMAND_3");
    if (result_ptr == null) {
      return ERROR;
    }
    interp.setResult(result_ptr);
    print("sub cmds for !interp!: "+result_ptr+"!");
    return OK;
  }

  /* ==================== interpAliasCmd ================================== */
  public int interpAliasCmd(Interp interp, ArrayList<ApwtclObj> args) {
//print("interp alias called"+args+"!");
    if (args.size() < 4) {
      interp.wrongNumArgs(1, args, "slavePath slaveCmd ?masterPath masterCmd? ?arg ...?");
      return ERROR;
    }
    ApwtclObj src_path = args.get(1);
    ApwtclObj src_cmd = args.get(2);
//print("alias1!"+src_path.getString()+"!"+src_cmd.getString()+"!"+args.size()+"!");
    if (args.size() == 4) {
      /* return the target_cmd */
      if (!src_path.getString().equals("")) {
        interp.setResultString("interp alias interp other current interp not yet implemented, src_path must be empty!");
        return ERROR;
      }
      if (src_path.getString().equals("")) {
//        src_path = interp;
      }
      String result_str = "";
      ArrayList<InterpAlias> my_aliases = interp.aliases.get(interp);
      for (int i = 0; i < interp.aliases.size(); i++) {
        InterpAlias my_alias = my_aliases.get(i);
        if (my_alias.src_path.equals(src_path)) {
          if (my_alias.src_cmd.equals(src_cmd)) {
            result_str = my_alias.target_cmd.getString();
            break;
          }
        }
      }
      interp.setResultString(result_str);
      return OK;
    }
    if (args.size() == 5) {
      /* seems to be the unset, so the argument has to be empty */
      ApwtclObj arg3 = args.get(3);
      if (!src_path.getString().equals("")) {
        interp.setResultString("interp alias interp other current interp not yet implemented, src_path must be empty!");
        return ERROR;
      }
      if (src_path.getString().equals("")) {
//        src_path = interp;
      }
      boolean found = false;
      ArrayList<InterpAlias> my_aliases = interp.aliases.get(interp);
      for (int i = 0; i < my_aliases.size(); i++) {
        InterpAlias my_alias = my_aliases.get(i);
        if ((!my_alias.src_path.getString().equals(src_path.getString())) || (!my_alias.src_cmd.getString().equals(src_cmd))) {
          my_aliases.add(my_alias);
        } else {
          found = true;
        }
      }
      if (found) {
        interp.aliases.put(interp,my_aliases);
      } else {
//        if (src_path == interp) {
//          src_path = "{}";
//        } else {
//          interp.setResultString("multiple interps not yet implemented!");
//          return ERROR;
//        }
        interp.setResultString("could not unset alias: \""+src_path+" "+src_cmd+"\" does not exist");
        return ERROR;
      }
      return OK;
    }
    if (args.size() < 5) {
      interp.wrongNumArgs(1, args, "slavePath slaveCmd ?masterPath masterCmd? ?arg ...?");
    }
    ApwtclObj target_path = args.get(3);
    ApwtclObj target_cmd = args.get(4);
//print("alias2!"+target_path.getString()+"!"+target_cmd.getString()+"!"+args.size()+"!");
    if ((!src_path.getString().equals("")) || (!target_path.getString().equals(""))) {
      interp.setResultString("interp alias interp other current interp not yet implemented, src_path and target_path must be empty!");
      return ERROR;
    }
    if (src_path.getString().equals("")) {
//      src_path = interp;
    }
    if (target_path.getString().equals("")) {
//      target_path = interp;
    }
//print("IA!"+src_path+"!"+src_cmd+"!"+target_path+"!"+target_cmd+"!"+args.slice(5)+"!");
    ArrayList<ApwtclObj> my_args = new ArrayList<ApwtclObj>();
    for (int i = 5; i < args.size(); i++) {
      my_args.add(args.get(i));
    }
//print("my_args!"+my_args+"!");
    ArrayList<InterpAlias> my_aliases = interp.aliases.get(interp);
//print("my_aliases!"+my_aliases+"!");
    if (my_aliases == null) {
      my_aliases = new ArrayList<InterpAlias>();
    }
    my_aliases.add(new InterpAlias(interp, src_path, src_cmd, target_path, target_cmd, my_args));
    interp.aliases.put(interp, my_aliases);
    return OK;
  }

}
