import { useState } from 'react';

import { Flex, HStack } from '@chakra-ui/react';
import { useNavigate } from 'react-router-dom';

import { EmptyEnd } from '@/components/EmptyEnd';
import { PageLayout } from '@/components/Layout/PageLayout';
import { PageLayoutInfoCenter } from '@/components/Layout/PageLayoutInfoCenter';
import { SearchInput } from '@/components/SearchInput';
import { TopBar } from '@/components/TopBar/TopBar';
import { DisplayType } from '@/components/type/DisplayType';
import { useColorModeValue } from '@/components/ui/color-mode';
import { useOrderedTypes } from '@/service/Type';

export const TypesPage = () => {
  const [filterTitle, setFilterTitle] = useState<string | undefined>(undefined);
  const { isLoading, dataTypes } = useOrderedTypes(filterTitle);
  const navigate = useNavigate();
  const onSelectItem = (TypeId: number) => {
    navigate(`/Type/${TypeId}`);
  };

  if (isLoading) {
    return (
      <>
        <TopBar title="All Types" />
        <PageLayoutInfoCenter>No Type available</PageLayoutInfoCenter>
      </>
    );
  }

  return (
    <>
      <TopBar title="All Types">
        <SearchInput onChange={setFilterTitle} />
      </TopBar>
      <PageLayout>
        <HStack
          wrap="wrap"
          gap="20px"
          marginX="auto"
          padding="20px"
          justify="center"
        >
          {dataTypes.map((data) => (
            <Flex
              align="flex-start"
              width={{ lg: "200px", base: "160px" }}
              height={{ lg: "280px", base: "200px" }}
              border="1px"
              borderColor="brand.900"
              backgroundColor={useColorModeValue('#FFFFFF88', '#00000088')}
              padding="5px"
              as="button"
              _hover={{
                boxShadow: 'outline-over',
                bgColor: useColorModeValue('#FFFFFFF7', '#000000F7'),
              }}
              onClick={() => onSelectItem(data.id)}
            >
              <DisplayType dataType={data} />
            </Flex>
          ))}
        </HStack>
        <EmptyEnd />
      </PageLayout>
    </>
  );
};
