import { env } from "vscode";
import { basename } from "path";
import type { QueryHistoryConfig } from "../config";
import type { LocalQueryInfo } from "../query-results";
import type { QueryHistoryInfo } from "./query-history-info";
import {
  buildRepoLabel,
  getLanguage,
  getRawQueryName,
} from "./query-history-info";
import type { VariantAnalysisHistoryItem } from "./variant-analysis-history-item";
import { assertNever } from "../common/helpers-pure";
import { pluralize } from "../common/word";
import { humanizeQueryStatus } from "./query-status";

interface InterpolateReplacements {
  t: string; // Start time
  q: string; // Query name
  d: string; // Database/repositories count
  r: string; // Result count/Empty
  s: string; // Status
  f: string; // Query file name
  l: string; // Query language
  "%": "%"; // Percent sign
}

export class HistoryItemLabelProvider {
  constructor(private config: QueryHistoryConfig) {
    /**/
  }

  getLabel(item: QueryHistoryInfo) {
    let replacements: InterpolateReplacements;
    switch (item.t) {
      case "local":
        replacements = this.getLocalInterpolateReplacements(item);
        break;
      case "variant-analysis":
        replacements = this.getVariantAnalysisInterpolateReplacements(item);
        break;
      default:
        assertNever(item);
    }

    const rawLabel = item.userSpecifiedLabel ?? (this.config.format || "%q");

    return this.interpolate(rawLabel, replacements);
  }

  /**
   * If there is a user-specified label for this query, interpolate and use that.
   * Otherwise, use the raw name of this query.
   *
   * @returns the name of the query, unless there is a custom label for this query.
   */
  getShortLabel(item: QueryHistoryInfo): string {
    return item.userSpecifiedLabel
      ? this.getLabel(item)
      : getRawQueryName(item);
  }

  private interpolate(
    rawLabel: string,
    replacements: InterpolateReplacements,
  ): string {
    const label = rawLabel.replace(
      /%(.)/g,
      (match, key: keyof InterpolateReplacements) => {
        const replacement = replacements[key];
        return replacement !== undefined ? replacement : match;
      },
    );

    return label.replace(/\s+/g, " ");
  }

  private getLocalInterpolateReplacements(
    item: LocalQueryInfo,
  ): InterpolateReplacements {
    const { resultCount = 0, message = "in progress" } =
      item.completedQuery || {};
    return {
      t: item.startTime,
      q: item.getQueryName(),
      d: item.databaseName,
      r: `(${resultCount} results)`,
      s: message,
      f: item.getQueryFileName(),
      l: this.getLanguageLabel(item),
      "%": "%",
    };
  }

  private getVariantAnalysisInterpolateReplacements(
    item: VariantAnalysisHistoryItem,
  ): InterpolateReplacements {
    const resultCount = item.resultCount
      ? `(${pluralize(item.resultCount, "result", "results")})`
      : "";
    return {
      t: new Date(item.variantAnalysis.executionStartTime).toLocaleString(
        env.language,
      ),
      q: `${item.variantAnalysis.query.name} (${item.variantAnalysis.language})`,
      d: buildRepoLabel(item),
      r: resultCount,
      s: humanizeQueryStatus(item.status),
      f: basename(item.variantAnalysis.query.filePath),
      l: this.getLanguageLabel(item),
      "%": "%",
    };
  }

  private getLanguageLabel(item: QueryHistoryInfo): string {
    const language = getLanguage(item);
    return language === undefined ? "unknown" : `${language}`;
  }
}
