/* eslint-disable */

// @ts-nocheck

import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
import {
  Alert,
  Button,
  FormGroup,
  InputGroup,
  InputGroupItem,
  Split,
  SplitItem,
} from "../../../shared/@patternfly/react-core";
import { useFormContext, Controller } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { PasswordInput, HelpItem } from "../../../shared/keycloak-ui-shared";
import { useAdminClient } from "../../admin-client";
import { useAlerts } from "../../../shared/keycloak-ui-shared";
import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog";
import { useAccess } from "../../context/access/Access";
import useFormatDate from "../../utils/useFormatDate";
import { CopyToClipboardButton } from "../../components/copy-to-clipboard-button/CopyToClipboardButton";

export type ClientSecretProps = {
  client: ClientRepresentation;
  secret: string;
  toggle: () => void;
  refresh: () => void;
};

type SecretInputProps = ClientSecretProps & {
  id: string;
  buttonLabel: string;
  isReadOnly?: boolean;
};

const SecretInput = ({
  id,
  buttonLabel,
  client,
  secret,
  toggle,
  isReadOnly = false,
}: SecretInputProps) => {
  const { t } = useTranslation();
  const form = useFormContext<ClientRepresentation>();
  const { hasAccess } = useAccess();
  const isManager = hasAccess("manage-clients") || client.access?.configure;

  return (
    <Split hasGutter>
      <SplitItem isFilled>
        <InputGroup>
          <InputGroupItem isFill>
            {isReadOnly ? (
              <PasswordInput id={id} value={secret} isDisabled />
            ) : (
              <Controller
                name="secret"
                control={form.control}
                render={({ field }) => (
                  <PasswordInput id={id} {...field} isDisabled={!isManager} />
                )}
              />
            )}
          </InputGroupItem>
          <InputGroupItem>
            <CopyToClipboardButton
              id={id}
              text={secret}
              label="clientSecret"
              variant="control"
            />
          </InputGroupItem>
        </InputGroup>
      </SplitItem>
      <SplitItem>
        <Button
          variant="secondary"
          isDisabled={form.formState.isDirty || !isManager}
          onClick={toggle}
        >
          {t(buttonLabel)}
        </Button>
      </SplitItem>
    </Split>
  );
};

const ExpireDateFormatter = ({ time }: { time: number }) => {
  const { t } = useTranslation();
  const formatDate = useFormatDate();
  const unixTimeToString = (time: number) =>
    time
      ? t("secretExpiresOn", {
          time: formatDate(new Date(time * 1000), {
            dateStyle: "full",
            timeStyle: "long",
          }),
        })
      : undefined;

  return <div className="pf-v5-u-my-md">{unixTimeToString(time)}</div>;
};

export const ClientSecret = ({
  client,
  secret,
  toggle,
  refresh,
}: ClientSecretProps) => {
  const { adminClient } = useAdminClient();

  const { t } = useTranslation();
  const { addAlert, addError } = useAlerts();

  const secretRotated = client.attributes?.["client.secret.rotated"];
  const secretExpirationTime: number =
    client.attributes?.["client.secret.expiration.time"];
  const secretRotatedExpirationTime: number =
    client.attributes?.["client.secret.rotated.expiration.time"];

  const expired = (time: number) => new Date().getTime() >= time * 1000;

  const [toggleInvalidateConfirm, InvalidateConfirm] = useConfirmDialog({
    titleKey: "invalidateRotatedSecret",
    messageKey: "invalidateRotatedSecretExplain",
    continueButtonLabel: "confirm",
    onConfirm: async () => {
      try {
        await adminClient.clients.invalidateSecret({
          id: client.id!,
        });
        addAlert(t("invalidateRotatedSuccess"));
        refresh();
      } catch (error) {
        addError("invalidateRotatedError", error);
      }
    },
  });

  return (
    <>
      <InvalidateConfirm />
      <FormGroup
        label={t("clientSecret")}
        fieldId="kc-client-secret"
        className="pf-v5-u-my-md"
        labelIcon={
          <HelpItem
            helpText={t("oidcClientSecretHelp")}
            fieldLabelId="kc-client-secret"
          />
        }
      >
        <SecretInput
          id="kc-client-secret"
          client={client}
          secret={secret}
          toggle={toggle}
          refresh={refresh}
          buttonLabel="regenerate"
        />
        <ExpireDateFormatter time={secretExpirationTime} />
        {expired(secretExpirationTime) && (
          <Alert variant="warning" isInline title={t("secretHasExpired")} />
        )}
      </FormGroup>
      {secretRotated && (
        <FormGroup label={t("secretRotated")} fieldId="secretRotated">
          <SecretInput
            id="secretRotated"
            client={client}
            secret={secretRotated}
            toggle={toggleInvalidateConfirm}
            refresh={refresh}
            buttonLabel="invalidateSecret"
            isReadOnly
          />
          <ExpireDateFormatter time={secretRotatedExpirationTime} />
        </FormGroup>
      )}
    </>
  );
};
