import { Box, Button, Flex, HStack, Text } from '@chakra-ui/react';
import { LuDisc3 } from 'react-icons/lu';
import { MdEdit } from 'react-icons/md';
import { Route, Routes, useNavigate, useParams } from 'react-router-dom';

import { Covers } from '@/components/Cover';
import { EmptyEnd } from '@/components/EmptyEnd';
import { PageLayout } from '@/components/Layout/PageLayout';
import { PageLayoutInfoCenter } from '@/components/Layout/PageLayoutInfoCenter';
import { BUTTON_TOP_BAR_PROPERTY, TopBar } from '@/components/TopBar/TopBar';
//import { useMediasOfAType } from '@/service/Media';
import { DisplayMediaFull } from '@/components/media/DisplayMediaFull';
import { TypeEditPopUp } from '@/components/popup/GenderEditPopUp';
import { MediaEditPopUp } from '@/components/popup/TrackEditPopUp';
import { DisplaySeries } from '@/components/series/DisplaySeries';
import { useColorModeValue } from '@/components/ui/color-mode';
import {
  useActivePlaylistService,
  useSeriesWithType,
  useSpecificType,
  useTypeGetVideo,
} from '@/service';

export const TypeDetailPage = () => {
  const { typeId } = useParams();
  const typeIdInt = typeId ? parseInt(typeId, 10) : undefined;
  const { playInList } = useActivePlaylistService();
  const { dataType } = useSpecificType(typeIdInt);
  const { videoWithType } = useTypeGetVideo(typeIdInt);
  const { seriesWithType } = useSeriesWithType(typeIdInt);
  const navigate = useNavigate();
  const onSelectItem = (mediaId: number) => {
    let currentPlay = 0;
    const listMediaId: number[] = [];
    if (!videoWithType) {
      console.log('Fail to get Type...');
      return;
    }
    for (let iii = 0; iii < videoWithType.length; iii++) {
      listMediaId.push(videoWithType[iii].id);
      if (videoWithType[iii].id === mediaId) {
        currentPlay = iii;
      }
    }
    playInList(currentPlay, listMediaId);
  };
  const onSelectSeriesItem = (seriesId: number) => {
    navigate(`/type/${typeId}/series/${seriesId}`);
  };

  console.log(`dataType = ${JSON.stringify(dataType, null, 2)}`);
  if (!dataType) {
    return (
      <>
        <TopBar title="Type detail" />
        <PageLayoutInfoCenter>
          Fail to load Series id: {typeId}
        </PageLayoutInfoCenter>
      </>
    );
  }
  return (
    <>
      <TopBar title="Type detail">
        <Button
          {...BUTTON_TOP_BAR_PROPERTY}
          onClick={() => navigate(`/Type/${typeId}/edit-type/${typeId}`)}
        >
          <MdEdit />
        </Button>
      </TopBar>
      <PageLayout>
        <Flex
          direction="row"
          width="80%"
          marginX="auto"
          padding="10px"
          gap="10px"
        >
          <Covers data={dataType?.covers} iconEmpty={<LuDisc3 />} slideshow />
          <Flex direction="column" width="80%" marginRight="auto">
            <Text fontSize="24px" fontWeight="bold">
              {dataType?.name}
            </Text>
            {dataType?.description && (
              <Text>Description: {dataType?.description}</Text>
            )}
          </Flex>
        </Flex>

        <HStack
          wrap="wrap"
          gap="20px"
          marginX="auto"
          padding="20px"
          justify="center"
        >
          {seriesWithType?.map((data) => (
            <Box
              width="200px"
              height="280px"
              border="1px"
              borderColor="brand.900"
              backgroundColor={useColorModeValue('#FFFFFF88', '#00000088')}
              padding="5px"
              as="button"
              _hover={{
                boxShadow: 'outline-over',
                bgColor: useColorModeValue('#FFFFFFF7', '#000000F7'),
              }}
              onClick={() => onSelectSeriesItem(data.id)}
            >
              <DisplaySeries
                dataSeries={data}
                //  >onClick={() => onSelectItem(data.id)}
                // contextMenu={[
                //   {
                //     name: 'Edit',
                //     onClick: () => {
                //       navigate(`/Type/${TypeId}/edit-media/${data.id}`);
                //     },
                //   },
                //   { name: 'Add Playlist', onClick: () => {} },
                // ]}
              />
            </Box>
          ))}
          {videoWithType?.map((data) => (
            <Box
              width="200px"
              height="280px"
              border="1px"
              borderColor="brand.900"
              backgroundColor={useColorModeValue('#FFFFFF88', '#00000088')}
              padding="5px"
              as="button"
              _hover={{
                boxShadow: 'outline-over',
                bgColor: useColorModeValue('#FFFFFFF7', '#000000F7'),
              }}
            >
              <DisplayMediaFull
                media={data}
                onClick={() => onSelectItem(data.id)}
                contextMenu={[
                  {
                    name: 'Edit',
                    onClick: () => {
                      navigate(`/Type/${typeId}/edit-media/${data.id}`);
                    },
                  },
                  { name: 'Add Playlist', onClick: () => {} },
                ]}
              />
            </Box>
          ))}
          <EmptyEnd />
        </HStack>
        <Routes>
          <Route path="edit-media/:MediaId" element={<MediaEditPopUp />} />
          <Route path="edit-type/:TypeId" element={<TypeEditPopUp />} />
        </Routes>
      </PageLayout>
    </>
  );
};
