import React, { useContext, useState } from 'react';
import { useInView } from 'react-intersection-observer';
import type { MediaRequest } from '../../../../server/entity/MediaRequest';
import {
  useIntl,
  FormattedDate,
  FormattedRelativeTime,
  defineMessages,
} from 'react-intl';
import { useUser, Permission } from '../../../hooks/useUser';
import { LanguageContext } from '../../../context/LanguageContext';
import type { MovieDetails } from '../../../../server/models/Movie';
import type { TvDetails } from '../../../../server/models/Tv';
import useSWR from 'swr';
import Badge from '../../Common/Badge';
import StatusBadge from '../../StatusBadge';
import Table from '../../Common/Table';
import {
  MediaRequestStatus,
  MediaStatus,
} from '../../../../server/constants/media';
import Button from '../../Common/Button';
import axios from 'axios';
import globalMessages from '../../../i18n/globalMessages';
import Link from 'next/link';
import { useToasts } from 'react-toast-notifications';

const messages = defineMessages({
  requestedby: 'Requested by {username}',
  seasons: 'Seasons',
  notavailable: 'N/A',
  failedretry: 'Something went wrong retrying the request',
});

const isMovie = (movie: MovieDetails | TvDetails): movie is MovieDetails => {
  return (movie as MovieDetails).title !== undefined;
};

interface RequestItemProps {
  request: MediaRequest;
  revalidateList: () => void;
}

const RequestItem: React.FC<RequestItemProps> = ({
  request,
  revalidateList,
}) => {
  const { ref, inView } = useInView({
    triggerOnce: true,
  });
  const { addToast } = useToasts();
  const intl = useIntl();
  const { hasPermission } = useUser();
  const { locale } = useContext(LanguageContext);
  const url =
    request.type === 'movie'
      ? `/api/v1/movie/${request.media.tmdbId}`
      : `/api/v1/tv/${request.media.tmdbId}`;
  const { data: title, error } = useSWR<MovieDetails | TvDetails>(
    inView ? `${url}?language=${locale}` : null
  );
  const { data: requestData, revalidate, mutate } = useSWR<MediaRequest>(
    `/api/v1/request/${request.id}`,
    {
      initialData: request,
    }
  );

  const [isRetrying, setRetrying] = useState(false);

  const modifyRequest = async (type: 'approve' | 'decline') => {
    const response = await axios.get(`/api/v1/request/${request.id}/${type}`);

    if (response) {
      revalidate();
    }
  };

  const deleteRequest = async () => {
    await axios.delete(`/api/v1/request/${request.id}`);

    revalidateList();
  };

  const retryRequest = async () => {
    setRetrying(true);

    try {
      const result = await axios.post(`/api/v1/request/${request.id}/retry`);
      mutate(result.data);
    } catch (e) {
      addToast(intl.formatMessage(messages.failedretry), {
        autoDismiss: true,
        appearance: 'error',
      });
    } finally {
      setRetrying(false);
    }
  };

  if (!title && !error) {
    return (
      <tr className="w-full h-24 bg-gray-800 animate-pulse" ref={ref}>
        <td colSpan={6}></td>
      </tr>
    );
  }

  if (!title || !requestData) {
    return (
      <tr className="w-full h-24 bg-gray-800 animate-pulse">
        <td colSpan={6}></td>
      </tr>
    );
  }

  return (
    <tr className="relative w-full h-24 p-2 text-white bg-gray-800">
      <Table.TD>
        <div className="flex items-center">
          <Link
            href={
              request.type === 'movie'
                ? `/movie/${request.media.tmdbId}`
                : `/tv/${request.media.tmdbId}`
            }
          >
            <a className="flex-shrink-0 hidden mr-4 sm:block">
              <img
                src={`//image.tmdb.org/t/p/w600_and_h900_bestv2${title.posterPath}`}
                alt=""
                className="w-12 transition duration-300 scale-100 rounded-md shadow-sm cursor-pointer transform-gpu hover:scale-105 hover:shadow-md"
              />
            </a>
          </Link>
          <div className="flex-shrink overflow-hidden">
            <Link
              href={
                requestData.type === 'movie'
                  ? `/movie/${requestData.media.tmdbId}`
                  : `/tv/${requestData.media.tmdbId}`
              }
            >
              <a className="min-w-0 mr-2 text-xl text-white truncate hover:underline">
                {isMovie(title) ? title.title : title.name}
              </a>
            </Link>
            <div className="text-sm">
              {intl.formatMessage(messages.requestedby, {
                username: requestData.requestedBy.username,
              })}
            </div>
            {requestData.seasons.length > 0 && (
              <div className="items-center hidden mt-2 text-sm sm:flex">
                <span className="mr-2">
                  {intl.formatMessage(messages.seasons)}
                </span>
                {requestData.seasons.map((season) => (
                  <span key={`season-${season.id}`} className="mr-2">
                    <Badge>{season.seasonNumber}</Badge>
                  </span>
                ))}
              </div>
            )}
          </div>
        </div>
      </Table.TD>
      <Table.TD>
        {requestData.media.status === MediaStatus.UNKNOWN ? (
          <Badge badgeType="danger">
            {intl.formatMessage(globalMessages.failed)}
          </Badge>
        ) : (
          <StatusBadge status={requestData.media.status} />
        )}
      </Table.TD>
      <Table.TD>
        <div className="flex flex-col">
          <span className="text-sm text-gray-300">
            <FormattedDate value={requestData.createdAt} />
          </span>
        </div>
      </Table.TD>
      <Table.TD>
        <div className="flex flex-col">
          {requestData.modifiedBy ? (
            <span className="text-sm text-gray-300">
              {requestData.modifiedBy.username} (
              <FormattedRelativeTime
                value={Math.floor(
                  (new Date(requestData.updatedAt).getTime() - Date.now()) /
                    1000
                )}
                updateIntervalInSeconds={1}
              />
              )
            </span>
          ) : (
            <span className="text-sm text-gray-300">N/A</span>
          )}
        </div>
      </Table.TD>
      <Table.TD alignText="right">
        {requestData.media.status === MediaStatus.UNKNOWN &&
          hasPermission(Permission.MANAGE_REQUESTS) && (
            <Button
              className="mr-2"
              buttonType="primary"
              buttonSize="sm"
              disabled={isRetrying}
              onClick={() => retryRequest()}
            >
              <svg
                className="w-4 h-4 mr-0 sm:mr-1"
                fill="currentColor"
                xmlns="http://www.w3.org/2000/svg"
                viewBox="0 0 24 24"
                width="18px"
                height="18px"
              >
                <path d="M0 0h24v24H0z" fill="none" />
                <path d="M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z" />
              </svg>
              <span className="hidden sm:block">
                {intl.formatMessage(globalMessages.retry)}
              </span>
            </Button>
          )}
        {requestData.status !== MediaRequestStatus.PENDING &&
          hasPermission(Permission.MANAGE_REQUESTS) && (
            <Button
              buttonType="danger"
              buttonSize="sm"
              onClick={() => deleteRequest()}
            >
              <svg
                className="w-4 h-4 mr-0 sm:mr-1"
                fill="currentColor"
                viewBox="0 0 20 20"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  fillRule="evenodd"
                  d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z"
                  clipRule="evenodd"
                />
              </svg>
              <span className="hidden sm:block">
                {intl.formatMessage(globalMessages.delete)}
              </span>
            </Button>
          )}
        {requestData.status === MediaRequestStatus.PENDING &&
          hasPermission(Permission.MANAGE_REQUESTS) && (
            <>
              <span className="mr-2">
                <Button
                  buttonType="success"
                  buttonSize="sm"
                  onClick={() => modifyRequest('approve')}
                >
                  <svg
                    className="w-4 h-4 mr-0 sm:mr-1"
                    fill="currentColor"
                    viewBox="0 0 20 20"
                    xmlns="http://www.w3.org/2000/svg"
                  >
                    <path
                      fillRule="evenodd"
                      d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
                      clipRule="evenodd"
                    />
                  </svg>
                  <span className="hidden sm:block">
                    {intl.formatMessage(globalMessages.approve)}
                  </span>
                </Button>
              </span>
              <span>
                <Button
                  buttonType="danger"
                  buttonSize="sm"
                  onClick={() => modifyRequest('decline')}
                >
                  <svg
                    className="w-4 h-4 mr-0 sm:mr-1"
                    fill="currentColor"
                    viewBox="0 0 20 20"
                    xmlns="http://www.w3.org/2000/svg"
                  >
                    <path
                      fillRule="evenodd"
                      d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
                      clipRule="evenodd"
                    />
                  </svg>
                  <span className="hidden sm:block">
                    {intl.formatMessage(globalMessages.decline)}
                  </span>
                </Button>
              </span>
            </>
          )}
      </Table.TD>
    </tr>
  );
};

export default RequestItem;
