"use client"

import { useState } from "react"

import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"

import { dt } from "@/lib/dt"

import Image from "@/components/Image"
import LoadingSpinner from "@/components/LoadingSpinner"
import Link from "@/components/TempDesignSystem/Link"
import Caption from "@/components/TempDesignSystem/Text/Caption"
import Title from "@/components/TempDesignSystem/Text/Title"
import useLang from "@/hooks/useLang"

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

import type { StayCardProps } from "@/types/components/myPages/stays/stayCard"

export default function StayCard({ stay }: StayCardProps) {
  const lang = useLang()

  // TODO: Temporary loading. Remove when current web is deleted.
  const [loading, setLoading] = useState(false)

  const { checkinDate, checkoutDate, hotelInformation, bookingUrl } =
    stay.attributes

  const arrival = dt(checkinDate).locale(lang)
  const arrivalDate = arrival.format("DD MMM")
  const arrivalDateTime = arrival.format("YYYY-MM-DD")
  const depart = dt(checkoutDate).locale(lang)
  const departDate = depart.format("DD MMM YYYY")
  const departDateTime = depart.format("YYYY-MM-DD")

  return (
    <Link
      href={bookingUrl}
      className={styles.link}
      onClick={() => setLoading(true)}
    >
      <article className={styles.stay}>
        <Image
          className={styles.image}
          alt={hotelInformation.hotelContent.images.metaData.altText}
          src={hotelInformation.hotelContent.images.imageSizes.small}
          width={420}
          height={240}
        />
        <footer className={styles.footer}>
          <Title as="h4" className={styles.hotel} level="h3">
            {hotelInformation.hotelName}
          </Title>
          <div className={styles.date}>
            <MaterialIcon
              icon="calendar_month"
              color="Icon/Interactive/Default"
            />
            <Caption asChild>
              <time dateTime={arrivalDateTime}>{arrivalDate}</time>
            </Caption>
            {/* eslint-disable-next-line formatjs/no-literal-string-in-jsx */}
            {" - "}
            <Caption asChild>
              <time dateTime={departDateTime}>{departDate}</time>
            </Caption>
          </div>
        </footer>
      </article>
      {loading && (
        <div className={styles.loadingcontainer}>
          <LoadingSpinner />
        </div>
      )}
    </Link>
  )
}
