/**
 * Copyright (c) 2023-present Plane Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import type { ReactNode } from "react";
import { useRef, useState } from "react";
import { createPortal } from "react-dom";
import { usePopper } from "react-popper";
import { SignalHigh } from "lucide-react";
import { Combobox } from "@headlessui/react";
import { ISSUE_PRIORITIES } from "@plane/constants";
import { useTranslation } from "@plane/i18n";
// types
import { CheckIcon, PriorityIcon, ChevronDownIcon, SearchIcon } from "@plane/propel/icons";
import { Tooltip } from "@plane/propel/tooltip";
import type { TIssuePriorities } from "@plane/types";
// ui
import { ComboDropDown } from "@plane/ui";
// helpers
import { cn } from "@plane/utils";
// hooks
import { useDropdown } from "@/hooks/use-dropdown";
import { usePlatformOS } from "@/hooks/use-platform-os";
// constants
import { BACKGROUND_BUTTON_VARIANTS, BORDER_BUTTON_VARIANTS, BUTTON_VARIANTS_WITHOUT_TEXT } from "./constants";
// types
import type { TDropdownProps } from "./types";

type Props = TDropdownProps & {
  button?: ReactNode;
  dropdownArrow?: boolean;
  dropdownArrowClassName?: string;
  highlightUrgent?: boolean;
  onChange: (val: TIssuePriorities) => void;
  onClose?: () => void;
  value: TIssuePriorities | undefined | null;
  renderByDefault?: boolean;
};

type ButtonProps = {
  className?: string;
  dropdownArrow: boolean;
  dropdownArrowClassName: string;
  hideIcon?: boolean;
  hideText?: boolean;
  isActive?: boolean;
  highlightUrgent: boolean;
  placeholder: string;
  priority: TIssuePriorities | undefined;
  showTooltip: boolean;
  renderToolTipByDefault?: boolean;
};

function BorderButton(props: ButtonProps) {
  const {
    className,
    dropdownArrow,
    dropdownArrowClassName,
    hideIcon = false,
    hideText = false,
    highlightUrgent,
    placeholder,
    priority,
    showTooltip,
    renderToolTipByDefault = true,
  } = props;

  const priorityDetails = ISSUE_PRIORITIES.find((p) => p.key === priority);

  const priorityClasses = {
    urgent: "bg-layer-2 border-priority-urgent px-1",
    high: "bg-layer-2 border-priority-high",
    medium: "bg-layer-2 border-priority-medium",
    low: "bg-layer-2 border-priority-low",
    none: "bg-layer-2 border-strong",
  };

  const { isMobile } = usePlatformOS();
  const { t } = useTranslation();

  return (
    <Tooltip
      tooltipHeading={t("priority")}
      tooltipContent={priorityDetails?.title ?? t("common.none")}
      disabled={!showTooltip}
      isMobile={isMobile}
      renderByDefault={renderToolTipByDefault}
    >
      <div
        className={cn(
          "flex h-full items-center gap-1.5 rounded-sm border-[0.5px] px-2 py-0.5",
          priorityClasses[priority ?? "none"],
          {
            // compact the icons if text is hidden
            "px-0.5": hideText,
            // highlight the whole button if text is hidden and priority is urgent
            "border-priority-urgent": priority === "urgent" && hideText && highlightUrgent,
          },
          className
        )}
      >
        {!hideIcon &&
          (priority ? (
            <PriorityIcon
              priority={priority}
              size={12}
              className={cn("flex-shrink-0", {
                // increase the icon size if text is hidden
                "h-3.5 w-3.5": hideText,
                // centre align the icons if text is hidden
                "translate-x-[0.0625rem]": hideText && priority === "high",
                "translate-x-0.5": hideText && priority === "medium",
                "translate-x-1": hideText && priority === "low",
              })}
            />
          ) : (
            <SignalHigh className="size-3" />
          ))}
        {!hideText && (
          <span
            className={cn("flex-grow truncate text-body-xs-medium", {
              "text-secondary": priority && priority !== "none",
              "text-placeholder": !priority || priority === "none",
            })}
          >
            {priority ? t(priority) : placeholder}
          </span>
        )}
        {dropdownArrow && (
          <ChevronDownIcon className={cn("h-2.5 w-2.5 flex-shrink-0", dropdownArrowClassName)} aria-hidden="true" />
        )}
      </div>
    </Tooltip>
  );
}

function BackgroundButton(props: ButtonProps) {
  const {
    className,
    dropdownArrow,
    dropdownArrowClassName,
    hideIcon = false,
    hideText = false,
    highlightUrgent,
    placeholder,
    priority,
    showTooltip,
    renderToolTipByDefault = true,
  } = props;

  const priorityDetails = ISSUE_PRIORITIES.find((p) => p.key === priority);

  const priorityClasses = {
    urgent: "bg-layer-2",
    high: "bg-layer-2",
    medium: "bg-layer-2",
    low: "bg-layer-2",
    none: "bg-layer-2",
  };

  const { isMobile } = usePlatformOS();
  const { t } = useTranslation();

  return (
    <Tooltip
      tooltipHeading={t("priority")}
      tooltipContent={t(priorityDetails?.key ?? "none")}
      disabled={!showTooltip}
      isMobile={isMobile}
      renderByDefault={renderToolTipByDefault}
    >
      <div
        className={cn(
          "flex h-full items-center gap-1.5 rounded-sm px-2 py-0.5",
          priorityClasses[priority ?? "none"],
          {
            // compact the icons if text is hidden
            "px-0.5": hideText,
            // highlight the whole button if text is hidden and priority is urgent
            "border-priority-urgent": priority === "urgent" && hideText && highlightUrgent,
          },
          className
        )}
      >
        {!hideIcon &&
          (priority ? (
            <PriorityIcon
              priority={priority}
              size={12}
              className={cn("flex-shrink-0", {
                // increase the icon size if text is hidden
                "h-3.5 w-3.5": hideText,
                // centre align the icons if text is hidden
                "translate-x-[0.0625rem]": hideText && priority === "high",
                "translate-x-0.5": hideText && priority === "medium",
                "translate-x-1": hideText && priority === "low",
              })}
            />
          ) : (
            <SignalHigh className="size-3" />
          ))}
        {!hideText && (
          <span
            className={cn("flex-grow truncate text-body-xs-medium", {
              "text-secondary": priority && priority !== "none",
              "text-placeholder": !priority || priority === "none",
            })}
          >
            {priority ? t(priority) : t("common.priority") ?? placeholder}
          </span>
        )}
        {dropdownArrow && (
          <ChevronDownIcon className={cn("h-2.5 w-2.5 flex-shrink-0", dropdownArrowClassName)} aria-hidden="true" />
        )}
      </div>
    </Tooltip>
  );
}

function TransparentButton(props: ButtonProps) {
  const {
    className,
    dropdownArrow,
    dropdownArrowClassName,
    hideIcon = false,
    hideText = false,
    isActive = false,
    highlightUrgent,
    placeholder,
    priority,
    showTooltip,
    renderToolTipByDefault = true,
  } = props;

  const priorityDetails = ISSUE_PRIORITIES.find((p) => p.key === priority);

  const { isMobile } = usePlatformOS();
  const { t } = useTranslation();

  return (
    <Tooltip
      tooltipHeading={t("priority")}
      tooltipContent={priorityDetails?.title ?? t("common.none")}
      disabled={!showTooltip}
      isMobile={isMobile}
      renderByDefault={renderToolTipByDefault}
    >
      <div
        className={cn(
          "flex h-full w-full items-center gap-1.5 rounded-sm px-2 hover:bg-layer-transparent-hover",
          {
            // compact the icons if text is hidden
            "px-0.5": hideText,
            // highlight the whole button if text is hidden and priority is urgent
            "border-priority-urgent": priority === "urgent" && hideText && highlightUrgent,
            "bg-layer-1": isActive,
          },
          className
        )}
      >
        {!hideIcon &&
          (priority ? (
            <PriorityIcon
              priority={priority}
              size={12}
              className={cn("flex-shrink-0", {
                // increase the icon size if text is hidden
                "h-3.5 w-3.5": hideText,
                // centre align the icons if text is hidden
                "translate-x-[0.0625rem]": hideText && priority === "high",
                "translate-x-0.5": hideText && priority === "medium",
                "translate-x-1": hideText && priority === "low",
              })}
            />
          ) : (
            <SignalHigh className="size-3" />
          ))}
        {!hideText && (
          <span
            className={cn("flex-grow truncate text-body-xs-medium", {
              "text-secondary": priority && priority !== "none",
              "text-placeholder": !priority || priority === "none",
            })}
          >
            {priority ? t(priority) : t("common.priority") ?? placeholder}
          </span>
        )}
        {dropdownArrow && (
          <ChevronDownIcon className={cn("h-2.5 w-2.5 flex-shrink-0", dropdownArrowClassName)} aria-hidden="true" />
        )}
      </div>
    </Tooltip>
  );
}

export function PriorityDropdown(props: Props) {
  //hooks
  const { t } = useTranslation();
  const {
    button,
    buttonClassName,
    buttonContainerClassName,
    buttonVariant,
    className = "",
    disabled = false,
    dropdownArrow = false,
    dropdownArrowClassName = "",
    hideIcon = false,
    highlightUrgent = true,
    onChange,
    onClose,
    placeholder = t("common.priority"),
    placement,
    showTooltip = false,
    tabIndex,
    value = "none",
    renderByDefault = true,
  } = props;
  // states
  const [query, setQuery] = useState("");
  const [isOpen, setIsOpen] = useState(false);
  // refs
  const dropdownRef = useRef<HTMLDivElement | null>(null);
  const inputRef = useRef<HTMLInputElement | null>(null);
  // popper-js refs
  const [referenceElement, setReferenceElement] = useState<HTMLButtonElement | null>(null);
  const [popperElement, setPopperElement] = useState<HTMLDivElement | null>(null);
  // popper-js init
  const { styles, attributes } = usePopper(referenceElement, popperElement, {
    strategy: "fixed",
    placement: placement ?? "bottom-start",
    modifiers: [
      {
        name: "preventOverflow",
        options: {
          padding: 12,
        },
      },
    ],
  });

  const options = ISSUE_PRIORITIES.map((priority) => ({
    value: priority.key,
    query: priority.key,
    content: (
      <div className="flex items-center gap-2">
        <PriorityIcon priority={priority.key} size={14} />
        <span className="flex-grow truncate">{t(priority.key)}</span>
      </div>
    ),
  }));

  const filteredOptions =
    query === "" ? options : options.filter((o) => o.query.toLowerCase().includes(query.toLowerCase()));

  const dropdownOnChange = (val: TIssuePriorities) => {
    onChange(val);
    handleClose();
  };

  const { handleClose, handleKeyDown, handleOnClick, searchInputKeyDown } = useDropdown({
    dropdownRef,
    inputRef,
    isOpen,
    onClose,
    query,
    setIsOpen,
    setQuery,
  });

  const ButtonToRender = BORDER_BUTTON_VARIANTS.includes(buttonVariant)
    ? BorderButton
    : BACKGROUND_BUTTON_VARIANTS.includes(buttonVariant)
      ? BackgroundButton
      : TransparentButton;

  const comboButton = button ? (
    <button
      ref={setReferenceElement}
      type="button"
      className={cn("clickable block h-full w-full border-0 bg-transparent shadow-none outline-none", buttonContainerClassName)}
      onClick={handleOnClick}
      disabled={disabled}
      tabIndex={tabIndex}
    >
      {button}
    </button>
  ) : (
    <button
      ref={setReferenceElement}
      type="button"
      className={cn(
        "clickable block h-full max-w-full border-0 bg-transparent shadow-none outline-none",
        {
          "cursor-not-allowed text-secondary": disabled,
          "cursor-pointer": !disabled,
        },
        buttonContainerClassName
      )}
      onClick={handleOnClick}
      disabled={disabled}
      tabIndex={tabIndex}
    >
      <ButtonToRender
        priority={value ?? undefined}
        className={buttonClassName}
        highlightUrgent={highlightUrgent}
        dropdownArrow={dropdownArrow && !disabled}
        dropdownArrowClassName={dropdownArrowClassName}
        hideIcon={hideIcon}
        placeholder={placeholder}
        showTooltip={showTooltip}
        hideText={BUTTON_VARIANTS_WITHOUT_TEXT.includes(buttonVariant)}
        renderToolTipByDefault={renderByDefault}
      />
    </button>
  );

  return (
    <ComboDropDown
      as="div"
      ref={dropdownRef}
      className={cn("h-full", className)}
      value={value}
      onChange={dropdownOnChange}
      disabled={disabled}
      onKeyDown={handleKeyDown}
      button={comboButton}
      renderByDefault={renderByDefault}
    >
      {isOpen &&
        typeof document !== "undefined" &&
        createPortal(
          <Combobox.Options className="fixed z-[760]" static>
            <div
              data-prevent-outside-click
              className="nodedc-dropdown-surface nodedc-external-popup-anchor my-1 w-56"
              ref={setPopperElement}
              style={styles.popper}
              {...attributes.popper}
            >
              <div className="nodedc-dropdown-search">
                <SearchIcon className="h-3.5 w-3.5 text-placeholder" strokeWidth={1.5} />
                <Combobox.Input
                  as="input"
                  ref={inputRef}
                  className="w-full bg-transparent py-0 text-12 text-secondary placeholder:text-placeholder outline-none focus:outline-none"
                  value={query}
                  onChange={(e) => setQuery(e.target.value)}
                  placeholder={t("search")}
                  displayValue={(assigned: any) => assigned?.name}
                  onKeyDown={searchInputKeyDown}
                />
              </div>
              <div className="mt-2 max-h-56 space-y-1 overflow-y-auto">
                {filteredOptions.length > 0 ? (
                  filteredOptions.map((option) => (
                    <Combobox.Option
                      key={option.value}
                      value={option.value}
                      className={({ active, selected }) =>
                        cn(
                          `nodedc-dropdown-option cursor-pointer ${
                            active ? "bg-white/6" : ""
                          } ${selected ? "text-primary" : "text-secondary"}`
                        )
                      }
                    >
                      {({ selected }) => (
                        <>
                          <span className="flex-grow truncate">{option.content}</span>
                          {selected && <CheckIcon className="h-3.5 w-3.5 flex-shrink-0" />}
                        </>
                      )}
                    </Combobox.Option>
                  ))
                ) : (
                  <p className="px-1.5 py-1 text-placeholder italic">{t("no_matching_results")}</p>
                )}
              </div>
            </div>
          </Combobox.Options>,
          document.body
        )}
    </ComboDropDown>
  );
}
