package fucksocks.utils;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * The class <code>Arguments</code> is an argument tool class.
 *
 * @author Youchao Feng
 * @version 1.0
 * @date Sep 24, 2015 10:20 AM
 */
public class Arguments {

  private final String[] args;
  private final Map<String, Integer> argPositionMap;

  public Arguments(final String[] args) {
    this.args = args;
    argPositionMap = new HashMap<>();
    for (int i = 0; i < args.length; i++) {
      argPositionMap.put(args[i], i);
    }
  }

  public static <T> T valueOf(String arg, Class<T> type) {
    checkNotNull(arg, "Argument [arg] may not be null");
    checkNotNull(type, "Argument [type] may not be null");
    Object value = arg.trim();
    if (arg.contains("=")) {
      value = arg.split("=")[1].trim();
    }
    if (type.equals(String.class)) {

    } else if (type.equals(Integer.class)) {
      value = Integer.parseInt((String) value);
    } else if (type.equals(Long.class)) {
      value = Long.parseLong((String) value);
    } else {
      throw new IllegalArgumentException("Not support" + type.getName());
    }
    return (T) value;
  }

  public static String valueOf(String arg) {
    return valueOf(arg, String.class);
  }

  public static int intValueOf(String arg) {
    return valueOf(arg, Integer.class);
  }

  public static long longValueOf(String arg) {
    return valueOf(arg, Long.class);
  }

  public static boolean isHelpArg(String arg) {
    arg = arg.trim();
    return arg.equals("-h") || arg.equals("--help");
  }

  public boolean hasArg(String arg) {
    return argPositionMap.get(arg) != null;
  }

  public boolean hasOneOfArgs(String... args) {
    for (String arg : args) {
      if (argPositionMap.get(arg) != null) {
        return true;
      }
    }
    return false;
  }

  public boolean hasArgsIn(String... args) {
    for (String arg : args) {
      if (argPositionMap.get(arg) != null) {
        return true;
      }
    }
    return false;
  }

  public String getValue(String arg, @Nullable String defaultValue) {
    Integer index = argPositionMap.get(checkNotNull(arg));
    if (index == null) {
      return defaultValue;
    } else if (index + 1 < args.length) {
      return args[index + 1];
    }
    return defaultValue;
  }

  public long getLongValue(String arg, long defaultValue) {
    String value = getValue(arg, null);
    if (value == null) {
      return defaultValue;
    }
    return Long.parseLong(value);
  }

  public int getIntValue(String arg, int defaultValue) {
    String value = getValue(arg, null);
    if (value == null) {
      return defaultValue;
    }
    return Integer.parseInt(value);
  }

  public boolean getBooleanValue(String arg, boolean defaultValue) {
    String value = getValue(arg, null);
    if (value == null) {
      return defaultValue;
    }
    return Boolean.parseBoolean(value);
  }

  public String getValue(List<String> args, @Nullable String defaultValue) {
    checkNotNull(args);
    String value = defaultValue;
    for (String arg : args) {
      value = getValue(arg, value);
    }
    return value;
  }

  public int getIntValue(List<String> args, int defaultValue) {
    int value = defaultValue;
    for (String arg : args) {
      value = getIntValue(arg, value);
    }
    return value;
  }

  public long getLongValue(List<String> args, long defaultValue) {
    long value = defaultValue;
    for (String arg : args) {
      value = getLongValue(arg, value);
    }
    return value;
  }

  public boolean getBooleanValue(List<String> args, boolean defaultValue) {
    boolean value = defaultValue;
    for (String arg : args) {
      value = getBooleanValue(arg, value);
    }
    return value;
  }

  public String getValueFromArg(String prefix, String splitRegex, @Nullable String defaultValue) {
    checkNotNull(prefix);
    checkNotNull(splitRegex);
    for (String arg : args) {
      if (arg.startsWith(prefix)) {
        String[] nameValue = arg.split(splitRegex);
        if (nameValue.length >= 2) {
          return nameValue[1];
        } else {
          return defaultValue;
        }
      }
    }
    return defaultValue;
  }

  public int getIntValueFromArg(String prefix, String splitRegex, int defaultValue) {
    String value = getValueFromArg(prefix, splitRegex, null);
    if (value == null) {
      return defaultValue;
    }
    return Integer.parseInt(value);
  }

  public long getLongValueFromArg(String prefix, String splitRegex, long defaultValue) {
    String value = getValueFromArg(prefix, splitRegex, null);
    if (value == null) {
      return defaultValue;
    }
    return Long.parseLong(value);
  }

  public boolean getBooleanValueFromArg(String prefix, String splitRegex, boolean defaultValue) {
    String value = getValueFromArg(prefix, splitRegex, null);
    if (value == null) {
      return defaultValue;
    }
    return Boolean.parseBoolean(value);
  }

  public String[] getArgs() {
    return args;
  }

}

@@ -1,8 +1,619 @@
+/*%0A * Copyright 2015-2025 the original author or authors.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22); you may not use this file except%0A * in compliance with the License. You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software distributed under the License%0A * is distributed on an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express%0A * or implied. See the License for the specific language governing permissions and limitations under%0A * the License.%0A */%0A%0A
 package 
@@ -954,16 +954,22 @@
 %0Apublic 
+final 
 class Ar
@@ -1085,24 +1085,34 @@
 ments(final 
+@Nullable 
 String%5B%5D arg
@@ -1172,24 +1172,49 @@
 ashMap%3C%3E();%0A
+    if(args != null) %7B%0A  
     for (int
@@ -1241,24 +1241,26 @@
 gth; i++) %7B%0A
+  
       argPos
@@ -1285,16 +1285,24 @@
 i%5D, i);%0A
+      %7D%0A
     %7D%0A  

check null, add license, set final class