import _variants from "@/components/ui/button/_variants";
import { ChangePasswordSchema } from "@/schemas/account/_changepassword";
import { FormBuilderElement } from "@/types/app-component-formbuilder";
import { faPaperPlane } from "@fortawesome/free-regular-svg-icons";
import { Dispatch, SetStateAction } from "react";
import { UseFormReturn } from "react-hook-form";

type Field = FormBuilderElement<ChangePasswordSchema, typeof _variants>;

type ChangePasswordFieldProps = {
  isUserValid: boolean;
  isPassForgotten: boolean;
  setIsPassForgotten: Dispatch<SetStateAction<boolean>>;
  handleConfirmAccount: () => void;
  handleSendCode: () => void;
  onSubmit: (values: ChangePasswordSchema) => void;
  isPending: boolean;
  form: UseFormReturn<ChangePasswordSchema>;
  closeModal: (id: string) => void;
  dict: Dictionary;
};

export default function getChangePasswordField({
  isUserValid,
  isPassForgotten,
  setIsPassForgotten,
  handleConfirmAccount,
  handleSendCode,
  onSubmit,
  isPending,
  form,
  closeModal,
  dict,
}: ChangePasswordFieldProps) {
  return [
    {
      type: "text",
      label: "Email",
      className: "w-full",
      name: "user",
      required: true,
      disabled: true,
    },
    ...(!isUserValid
      ? !isPassForgotten
        ? [
            {
              type: "password",
              label: dict.pages.settings.account.form.fields.password.label,
              className: "w-full",
              name: "password",
              required: true,
              errors: dict.pages.settings.account.form.fields.password.errors,
            },
            {
              type: "label",
              label: dict.pages.general.forgot_password,
              className: "text-xs text-primary-500 cursor-pointer",
              onClick: () => {
                form.setValue("password", "");
                setIsPassForgotten(true);
              },
            },
            {
              type: "group",
              className: "w-full flex justify-end gap-2",
              fields: [
                {
                  type: "button",
                  label: dict.pages.general.close,
                  variant: "solid",
                  color: "danger",
                  onClick: () => closeModal("change-password"),
                },
                {
                  type: "button",
                  label: dict.pages.general.confirm,
                  variant: "solid",
                  color: "primary",
                  disabled: isPending || form.getFieldState("password").invalid,
                  onClick: handleConfirmAccount,
                },
              ],
            },
          ]
        : [
            {
              type: "group",
              className: "w-full flex items-end gap-2",
              fields: [
                {
                  type: "text",
                  label: dict.pages.settings.account.form.fields.code.label,
                  className: "",
                  name: "code",
                  required: true,
                  errors: dict.pages.settings.account.form.fields.code.errors,
                },
                {
                  type: "button",
                  variant: "soft",
                  color: "primary",
                  decorator: faPaperPlane,
                  isIcon: true,
                  className: "border-neutral-200",
                  tooltip: dict.pages.settings.account.form.send,
                  onClick: handleSendCode,
                },
              ],
            },
            {
              type: "group",
              className: "w-full flex justify-end gap-2",
              fields: [
                {
                  type: "button",
                  label: dict.pages.general.close,
                  variant: "solid",
                  color: "danger",
                  onClick: () => closeModal("change-password"),
                },
                {
                  type: "button",
                  label: dict.pages.general.confirm,
                  variant: "solid",
                  color: "primary",
                  disabled: isPending || form.getFieldState("code").invalid,
                  onClick: handleConfirmAccount,
                },
              ],
            },
          ]
      : [
          {
            type: "password",
            label: dict.pages.settings.account.form.fields.new_password.label,
            className: "w-full",
            name: "new-password",
            required: true,
            errors: dict.pages.settings.account.form.fields.new_password.errors,
          },
          {
            type: "password",
            label: dict.pages.settings.account.form.fields.rep_password.label,
            className: "w-full",
            name: "rep-password",
            required: true,
            errors: dict.pages.settings.account.form.fields.rep_password.errors,
          },
          {
            type: "group",
            className: "w-full flex justify-end gap-2",
            fields: [
              {
                type: "button",
                label: dict.pages.general.close,
                variant: "solid",
                color: "danger",
                onClick: () => closeModal("change-password"),
              },
              {
                type: "button",
                label: dict.pages.general.confirm,
                variant: "solid",
                color: "primary",
                disabled:
                  isPending ||
                  form.getFieldState("new-password").invalid ||
                  form.getFieldState("rep-password").invalid,
                onClick: () => onSubmit(form.getValues()),
              },
            ],
          },
        ]),
  ] as Field[];
}
