import useSWR from 'swr';
import Image from 'next/image';
import { OAuth2ClientListItem } from '../../lib/types/oauth2-client.interface';
import { PaginatedResponse } from '../../lib/types/paginated-response.interface';
import { Paginator } from '../common/Paginator/Paginator';
import styles from './OAuth2Page.module.scss';
import { UPLOADS_URL } from '../../lib/constants';
import application from '../../public/application.png';
import { useState } from 'react';
import useUser from '../../lib/hooks/useUser';
import { Container } from '../common/Container/Container';
import { Header } from '../common/Header/Header';
import Modal from '../common/Modal/Modal/Modal';
import ModalHeader from '../common/Modal/ModalHeader/ModalHeader';
import ModalBody from '../common/Modal/ModalBody/ModalBody';
import ModalFooter from '../common/Modal/ModalFooter/ModalFooter';
import ModalService from '../common/Modal/services/ModalService';

const TestModal = ({ close }: { close: (...args: any[]) => void }) => {
  return (
    <Modal>
      <ModalHeader>
        <h3>Test!</h3>
      </ModalHeader>
      <ModalBody>
        <p>This is only a test</p>
      </ModalBody>
      <ModalFooter>
        <button onClick={() => close(true)}>Close</button>
      </ModalFooter>
    </Modal>
  );
};

const OAuth2ClientCard = ({ client }: { client: OAuth2ClientListItem }) => (
  <div className={styles.clientCard}>
    <div className={styles.pictureWrapper}>
      {client.picture ? (
        <Image
          src={`${UPLOADS_URL}/${client.picture.file}`}
          width={128}
          height={128}
          alt=""
        />
      ) : (
        <Image src={application} alt="" width={128} height={128} />
      )}
    </div>
    <div className={styles.clientInfo}>
      <h2>{client.title}</h2>
      <span className={styles.clientDescription}>{client.description}</span>
      <dl>
        <dt>Client ID</dt>
        <dd>{client.client_id}</dd>
        <dt>Allowed scopes</dt>
        <dd>{client.scope?.split(' ').join(', ')}</dd>
        <dt>Allowed grant types</dt>
        <dd>{client.grants?.split(' ').join(', ')}</dd>
        <dt>Activated</dt>
        <dd>{client.activated ? 'Yes' : <b>NOT ACTIVATED</b>}</dd>
        <dt>Verified</dt>
        <dd>{client.verified ? 'Yes' : 'No'}</dd>
        <dt>Created</dt>
        <dd>{new Date(client.created_at).toDateString()}</dd>
      </dl>
    </div>
  </div>
);

const OAuth2ClientList = ({
  pageIndex,
  searchTerm,
  setPage,
}: {
  pageIndex: number;
  searchTerm: string;
  setPage: (page: number) => void;
}) => {
  const { data } = useSWR<PaginatedResponse<OAuth2ClientListItem>>(
    `/api/admin/oauth2/clients?page=${pageIndex}${
      searchTerm ? `&q=${searchTerm}` : ''
    }`
  );

  return data ? (
    <>
      <button onClick={() => ModalService.open(TestModal)}>test</button>
      <div className={styles.clientList}>
        {data.list.map((client) => (
          <OAuth2ClientCard client={client} key={client.client_id} />
        ))}
        {data?.pagination && (
          <Paginator setPage={setPage} pagination={data.pagination}></Paginator>
        )}
      </div>
    </>
  ) : (
    <span>Nothing found</span>
  );
};

export const OAuth2Page = () => {
  const { user } = useUser({ redirectTo: '/login' });
  const [pageIndex, setPageIndex] = useState(1);
  const [searchTerm, setSearchTerm] = useState('');

  return (
    <>
      <Header user={user}></Header>
      <Container>
        <h1>OAuth2 clients</h1>
        <OAuth2ClientList
          pageIndex={pageIndex}
          searchTerm={searchTerm}
          setPage={setPageIndex}
        />
      </Container>
    </>
  );
};
