// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.commandInterface.commandLine.psi.impl;

import com.intellij.commandInterface.command.Argument;
import com.intellij.commandInterface.command.Help;
import com.intellij.commandInterface.command.Option;
import com.intellij.commandInterface.commandLine.CommandLinePart;
import com.intellij.commandInterface.commandLine.ValidationResult;
import com.intellij.commandInterface.commandLine.psi.CommandLineArgument;
import com.intellij.commandInterface.commandLine.psi.CommandLineFile;
import com.intellij.commandInterface.commandLine.psi.CommandLineOption;
import com.intellij.psi.PsiElement;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Class to be used by autogenerated PSI elements to delegate logic
 *
 * @author Ilya.Kazakevich
 */
@SuppressWarnings("StaticMethodOnlyUsedInOneClass") // This class was created to store functions for grammar kit
final class CommandLinePsiImplUtils {


  private CommandLinePsiImplUtils() {
  }

  /**
   * Checks if option is long (--long-option vs -s)
   *
   * @param o option
   * @return true if long
   */
  static boolean isLong(final @NotNull CommandLineOption o) {
    return o.getLongOptionNameToken() != null;
  }

  /**
   * For options with eq finds argument next to it.
   * For options with out of eq just use next psi
   * @return null if option does not have eq
   */
  static @Nullable CommandLineArgument findArgument(final @NotNull CommandLineOption option) {
    if (option.getText().endsWith("=")) {
      return ObjectUtils.tryCast(option.getNextSibling(), CommandLineArgument.class);
    }
    return null;
  }
  /**
   * Finds real option based on psi opton
   *
   * @param option psi option
   * @return real option (if any)
   */
  static @Nullable Option findRealOption(final @NotNull CommandLineOption option) {
    final ValidationResult validationResult = getValidationResult(option);
    if (validationResult == null) {
      return null;
    }
    return validationResult.getOption(option);
  }

  /**
   * @return for arg in quotes returns bare value, or simply value otherwise
   */
  static @NotNull String getValueNoQuotes(final @NotNull CommandLineArgument argument) {
    final char[] chars = argument.getText().toCharArray();
    if (chars.length == 0) {
      return "";
    }
    final char firstChar = chars[0];
    if (firstChar == chars[chars.length - 1] && firstChar == '"' || firstChar == '\'') {
      return argument.getText().substring(1, argument.getTextLength() - 1);
    }
    return argument.getText();
  }
  /**
   * Tries to find appropriate help for argument. It can be argument help for positional argument or option help
   * for option argument.
   *
   * @param argument argument to search help for
   * @return help for argument or null if not found
   */
  static @Nullable Help findBestHelp(final @NotNull CommandLineArgument argument) {
    final Option option = argument.findOptionForOptionArgument();
    if (option != null) {
      return option.getHelp();
    }
    final Argument realArgument = argument.findRealArgument();
    return (realArgument != null ? realArgument.getHelp() : null);
  }


  /**
   * Finds real argument based on psi argument
   *
   * @param argument psi argument
   * @return real argument (if any)
   */
  static @Nullable Argument findRealArgument(final @NotNull CommandLineArgument argument) {
    final ValidationResult validationResult = getValidationResult(argument);
    if (validationResult == null) {
      return null;
    }
    return validationResult.getArgument(argument);
  }

  /**
   * Finds option if argument is option argument
   *
   * @param argument argument to check
   * @return option (if option argument) or null if not
   */
  static @Nullable Option findOptionForOptionArgument(final @NotNull CommandLineArgument argument) {
    final ValidationResult validationResult = getValidationResult(argument);
    if (validationResult == null) {
      return null;
    }
    return validationResult.getOptionForOptionArgument(argument);
  }

  /**
   * Searches for validation result for command line
   *
   * @param commandLinePart command line part
   * @return validation result (if any)
   */
  private static @Nullable ValidationResult getValidationResult(final @NotNull CommandLinePart commandLinePart) {
    final CommandLineFile commandLineFile = commandLinePart.getCommandLineFile();
    if (commandLineFile == null) {
      return null;
    }
    final ValidationResult validationResult = commandLineFile.getValidationResult();
    if (validationResult == null) {
      return null;
    }
    return validationResult;
  }

  /**
   * Returns option name regardless it is long or short.
   *
   * @param o option
   * @return name (if any)
   */
  static @Nullable @NonNls String getOptionName(final @NotNull CommandLineOption o) {
    final PsiElement longNameToken = o.getLongOptionNameToken();
    if (longNameToken != null) {
      return longNameToken.getText();
    }
    final PsiElement shortOptionNameToken = o.getShortOptionNameToken();
    if (shortOptionNameToken != null) {
      return shortOptionNameToken.getText();
    }
    return null;
  }
}
