"use client"
import { useIntl } from "react-intl"

import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import { membershipTermsAndConditions } from "@scandic-hotels/common/constants/routes/membershipTermsAndConditions"
import { formatPrice } from "@scandic-hotels/common/utils/numberFormatting"
import { Button } from "@scandic-hotels/design-system/Button"
import Footnote from "@scandic-hotels/design-system/Footnote"
import Checkbox from "@scandic-hotels/design-system/Form/Checkbox"
import Link from "@scandic-hotels/design-system/Link"
import { toast } from "@scandic-hotels/design-system/Toast"
import { Typography } from "@scandic-hotels/design-system/Typography"
import { trpc } from "@scandic-hotels/trpc/client"

import { useRoomContext } from "../../../../../contexts/EnterDetails/RoomContext"
import useLang from "../../../../../hooks/useLang"

import styles from "./partnerSASJoinScandicFriendsCard.module.css"

type Props = {
  name?: string
}
export function PartnerSASJoinScandicFriendsCard({ name = "join" }: Props) {
  const lang = useLang()
  const intl = useIntl()
  const { data: euroBonusProfile } =
    trpc.partner.sas.getEuroBonusProfile.useQuery()

  const {
    room,
    actions: { updateJoin },
  } = useRoomContext()

  function onChange(event: { target: { value: boolean } }) {
    updateJoin(event.target.value)
  }

  if (!euroBonusProfile || euroBonusProfile.linkStatus !== "UNLINKED") {
    return null
  }

  if (!("member" in room.roomRate) || !room.roomRate.member) {
    return null
  }

  return (
    <div className={styles.cardContainer}>
      <Typography variant="Title/Subtitle/md">
        <h2 className={styles.priceContainer}>
          <span>
            {/* eslint-disable-next-line formatjs/no-literal-string-in-jsx */}
            <>
              NOT IMPLEMENTED
              <br />
              {intl.formatMessage({
                defaultMessage: "Get the Scandic Friends room price",
              })}
            </>
            {/* eslint-disable-next-line formatjs/no-literal-string-in-jsx */}
            {`: `}
          </span>
          <span className={styles.price}>
            {formatPrice(
              intl,
              room.roomRate.member.localPrice.pricePerStay ?? 0,
              room.roomRate.member.localPrice.currency ?? CurrencyEnum.Unknown
            )}
          </span>
        </h2>
      </Typography>
      <Checkbox
        name={name}
        className={styles.checkBox}
        registerOptions={{ onChange }}
      >
        <Typography variant="Body/Paragraph/mdRegular">
          <div>
            {intl.formatMessage({
              defaultMessage: "Join Scandic Friends now",
            })}
          </div>
        </Typography>
      </Checkbox>

      <Button
        variant={"Tertiary"}
        size={"Small"}
        typography={"Body/Paragraph/mdBold"}
        color="Inverted"
        className={styles.login}
        onClick={() => toast.error("TODO: Not implemented")}
      >
        {intl.formatMessage({
          defaultMessage: "Link",
        })}
      </Button>

      <div className={styles.terms}>
        <Footnote color="uiTextPlaceholder">
          {intl.formatMessage(
            {
              defaultMessage:
                "By joining you accept the <termsAndConditionsLink>Terms and Conditions</termsAndConditionsLink>. The Scandic Friends Membership is valid until further notice, but can at any time be terminated by contacting Scandic Customer Service.",
            },
            {
              termsAndConditionsLink: (str) => (
                <Link
                  textDecoration="underline"
                  size="tiny"
                  target="_blank"
                  href={membershipTermsAndConditions[lang]}
                >
                  {str}
                </Link>
              ),
            }
          )}
        </Footnote>
      </div>
    </div>
  )
}
