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 { DisplayMediaListFull } from '@/components/media/DisplayMediaListFull';
import { TypeEditPopUp } from '@/components/popup/GenderEditPopUp';
import { MediaEditPopUp } from '@/components/popup/TrackEditPopUp';
import { useColorModeValue } from '@/components/ui/color-mode';
import {
  useActivePlaylistService,
  useSpecificSeason,
  useSpecificSeries,
  useSpecificType,
  useTypeSeriesSeasonGetVideo,
} from '@/service';

export const TypesSeriesSeasonDetailPage = () => {
  const { typeId, seriesId, seasonId } = useParams();
  const typeIdInt = typeId ? parseInt(typeId, 10) : undefined;
  const seriesIdInt = seriesId ? parseInt(seriesId, 10) : undefined;
  const seasonIdInt = seasonId ? parseInt(seasonId, 10) : undefined;
  const { playInList } = useActivePlaylistService();
  const { dataType } = useSpecificType(typeIdInt);
  const { dataSeries } = useSpecificSeries(seriesIdInt);
  const { dataSeason } = useSpecificSeason(seasonIdInt);
  const { videoWithType: videos } = useTypeSeriesSeasonGetVideo(typeIdInt, seriesIdInt, seasonIdInt);
  const navigate = useNavigate();
  const onSelectItem = (mediaId: number) => {
    let currentPlay = 0;
    const listMediaId: number[] = [];
    if (!videos) {
      console.log('Fail to get Type...');
      return;
    }
    for (let iii = 0; iii < videos.length; iii++) {
      listMediaId.push(videos[iii].id);
      if (videos[iii].id === mediaId) {
        currentPlay = iii;
      }
    }
    playInList(currentPlay, listMediaId);
  };

  console.log(`dataType = ${JSON.stringify(dataType, null, 2)}`);
  if (!dataType) {
    return (
      <>
        <TopBar title="Type detail" />
        <PageLayoutInfoCenter>
          Fail to load Series id: {typeId}/{seriesId}/{seasonId}
        </PageLayoutInfoCenter>
      </>
    );
  }
  return (
    <>
      <TopBar title="season detail">
        <Button
          {...BUTTON_TOP_BAR_PROPERTY}
          onClick={() =>
            navigate(
              `/Type/${typeId}/series/${seriesId}/season/${seasonId}/edit-season/${seasonId}`
            )
          }
        >
          <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">
              type: {dataType?.name}
            </Text>
            <Text fontSize="24px" fontWeight="bold">
              series: {dataSeries?.name}
            </Text>
            <Text fontSize="24px" fontWeight="bold">
              season: {dataSeason?.name}
            </Text>
            {dataSeason?.description && (
              <Text>Description: {dataSeason?.description}</Text>
            )}
          </Flex>
        </Flex>

        <HStack
          wrap="wrap"
          gap="20px"
          marginX="auto"
          padding="20px"
          justify="center"
        >
          {videos?.map((data) => (
            <Box
              key={data.id}
              width={{sm:"95%", base:"calc(max(300px,50%))"}}
              height="60px"
              border="1px"
              borderColor="brand.900"
              backgroundColor={useColorModeValue('#FFFFFF88', '#00000088')}
              padding="5px"
              as="button"
              _hover={{
                boxShadow: 'outline-over',
                bgColor: useColorModeValue('#FFFFFFF7', '#000000F7'),
              }}
            >
              <DisplayMediaListFull
                media={data}
                onClick={() => onSelectItem(data.id)}
                contextMenu={[
                  {
                    name: 'Edit',
                    onClick: () => {
                      navigate(
                        `/type/${typeId}/series/${seriesId}/season/${seasonId}/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 />} />
          {/* <Route path="edit-season/:seasonId" element={<TypeSeasonPopUp />} /> */}
        </Routes>
      </PageLayout>
    </>
  );
};
