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

import { CurrencyEnum } from "@scandic-hotels/common/constants/currency"
import { useLazyPathname } from "@scandic-hotels/common/hooks/useLazyPathname"
import { formatPrice } from "@scandic-hotels/common/utils/numberFormatting"
import Checkbox from "@scandic-hotels/design-system/Form/Checkbox"
import { LoginButton } from "@scandic-hotels/design-system/LoginButton"
import { TextLink } from "@scandic-hotels/design-system/TextLink"
import { Typography } from "@scandic-hotels/design-system/Typography"
import { trackEvent } from "@scandic-hotels/tracking/base"
import { trackLoginClick } from "@scandic-hotels/tracking/navigation"

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

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

type Props = {
  name?: string
}
export function JoinScandicFriendsCard({ name = "join" }: Props) {
  const lang = useLang()
  const intl = useIntl()
  const { routes } = useBookingFlowConfig()
  const loginPathname = useLazyPathname({ includeSearchParams: true })
  const {
    room,
    actions: { updateJoin },
  } = useRoomContext()

  function onChange(event: { target: { value: boolean } }) {
    if (event.target.value) {
      trackEvent({
        event: "memberPriceActivated",
        cta: {
          name: "join scandic friends now",
          position: "banner enter details page",
        },
      })
    }
    updateJoin(event.target.value)
  }

  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 */}
            {`${intl.formatMessage({
              id: "enterDetails.joinScandicFriendsCard.title",
              defaultMessage: "Get the member room price",
            })}: `}
          </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({
              id: "enterDetails.joinScandicFriendsCard.joinCheckboxLabel",
              defaultMessage: "Join Scandic Friends now",
            })}
          </div>
        </Typography>
      </Checkbox>

      <LoginButton
        color="Primary"
        variant="Tertiary"
        size="sm"
        lang={lang}
        className={styles.login}
        loginPosition="enter-details"
        onClick={() => {
          trackLoginClick("enter details")
        }}
        redirectTo={loginPathname}
      >
        {intl.formatMessage({
          id: "enterDetails.joinScandicFriendsCard.loginButtonText",
          defaultMessage: "Log in",
        })}
      </LoginButton>

      <Typography variant="Body/Supporting text (caption)/smRegular">
        <p className={styles.terms}>
          {intl.formatMessage(
            {
              id: "enterDetails.joinScandicFriendsCard.terms",
              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) => (
                <TextLink
                  typography="Link/sm"
                  target="_blank"
                  href={routes.membershipTermsAndConditions[lang]}
                >
                  {str}
                </TextLink>
              ),
            }
          )}
        </p>
      </Typography>
    </div>
  )
}
