/* (non-Javadoc)
   * @see com.buildml.main.ICliCommand#invoke(com.buildml.model.BuildStore, java.lang.String[])
   */
  @Override
  public void invoke(IBuildStore buildStore, String buildStorePath, String[] args) {

    CliUtils.validateArgs(
        getName(),
        args,
        2,
        2,
        "You must specify a package name and a colon-separated list of action-specs.");

    IPackageMemberMgr pkgMemberMgr = buildStore.getPackageMemberMgr();
    IActionMgr actionMgr = buildStore.getActionMgr();

    /*
     * The package can be of the form: "pkg". There is no scope value allowed
     * for actions.
     */
    String pkgName = args[0];
    int pkgAndScopeIds[] = CliUtils.parsePackageAndScope(buildStore, pkgName, false);
    int pkgId = pkgAndScopeIds[0];

    /* compute the ActionSet from the user-supplied list of action-specs */
    String actionSpecs = args[1];
    ActionSet actionsToSet = CliUtils.getCmdLineActionSet(actionMgr, actionSpecs);

    /* now visit each action in the ActionSet and set its package */
    boolean prevState = buildStore.setFastAccessMode(true);
    for (int actionId : actionsToSet) {
      pkgMemberMgr.setPackageOfMember(IPackageMemberMgr.TYPE_ACTION, actionId, pkgId);
    }
    buildStore.setFastAccessMode(prevState);
  }
  /**
   * Create a new {@link MovePackageRefactorer} object.
   *
   * @param destPkgId ID of the package to move members into.
   * @param multiOp MultiUndoOp to add change operations to.
   * @param buildStore The IBuildStore that stores the packages/members.
   */
  public MovePackageRefactorer(int destPkgId, MultiUndoOp multiOp, IBuildStore buildStore) {
    this.destPkgId = destPkgId;
    this.multiOp = multiOp;
    this.buildStore = buildStore;

    pkgMgr = buildStore.getPackageMgr();
    actionMgr = buildStore.getActionMgr();
    fileMgr = buildStore.getFileMgr();
    fileGroupMgr = buildStore.getFileGroupMgr();
    pkgMemberMgr = buildStore.getPackageMemberMgr();
    pkgRootMgr = buildStore.getPackageRootMgr();
  }
  /**
   * Validate the list of members that is input into moveMembersToPackage(). There are many possible
   * errors, including invalid member types, or undefined action, file, or fileGroup ID numbers.
   * Errors are report via exceptions.
   *
   * @param members The list of MemberDesc to be validated.
   * @throws CanNotRefactorException The reason why the members list is invalid.
   */
  private void validateMembersList(List<MemberDesc> members) throws CanNotRefactorException {

    IFileMgr fileMgr = buildStore.getFileMgr();
    IFileGroupMgr fileGroupMgr = buildStore.getFileGroupMgr();
    IActionMgr actionMgr = buildStore.getActionMgr();

    /* can't be null! */
    if (members == null) {
      throw new CanNotRefactorException(Cause.INVALID_MEMBER, -1);
    }

    /* keep a lists of invalid "things" - we can provide the whole list in the exception */
    List<Integer> invalidFiles = new ArrayList<Integer>();
    List<Integer> invalidFileGroups = new ArrayList<Integer>();
    List<Integer> invalidActions = new ArrayList<Integer>();

    /*
     * Scan the list of members, validating their type, and whether each ID is valid.
     * Invalid entries are logged, and will be reported as exceptions once we've seen
     * all the members.
     */
    for (MemberDesc member : members) {
      int id = member.memberId;
      switch (member.memberType) {
        case IPackageMemberMgr.TYPE_FILE:
          PathType pathType = fileMgr.getPathType(id);
          if (pathType == PathType.TYPE_INVALID) {
            invalidFiles.add(id);
          }
          break;

        case IPackageMemberMgr.TYPE_FILE_GROUP:
          if (fileGroupMgr.getGroupType(id) == ErrorCode.NOT_FOUND) {
            invalidFileGroups.add(id);
          }
          break;

        case IPackageMemberMgr.TYPE_ACTION:
          if (!actionMgr.isActionValid(id)) {
            invalidActions.add(id);
          }
          break;

        default:
          throw new CanNotRefactorException(Cause.INVALID_MEMBER, member.memberType);
      }
    }

    /* Thrown exceptions if we found anything invalid */
    if (!invalidFiles.isEmpty()) {
      throw new CanNotRefactorException(Cause.INVALID_PATH, invalidFiles.toArray(new Integer[0]));
    }
    if (!invalidActions.isEmpty()) {
      throw new CanNotRefactorException(
          Cause.INVALID_ACTION, invalidActions.toArray(new Integer[0]));
    }
    if (!invalidFileGroups.isEmpty()) {
      throw new CanNotRefactorException(
          Cause.INVALID_FILE_GROUP, invalidFileGroups.toArray(new Integer[0]));
    }
  }