import { Box, Button, Flex, 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 { GenderEditPopUp } from '@/components/popup/GenderEditPopUp';
import { TrackEditPopUp } from '@/components/popup/TrackEditPopUp';
import { DisplayTrack } from '@/components/track/DisplayTrack';
import { DisplayTrackFull } from '@/components/track/DisplayTrackFull';
import { useActivePlaylistService } from '@/service/ActivePlaylist';
import { useSpecificGender } from '@/service/Gender';
import { useTracksOfAGender } from '@/service/Track';
//import { useTracksOfAGender } from '@/service/Track';
import { useThemeMode } from '@/utils/theme-tools';

export const GenderDetailPage = () => {
  const { genderId } = useParams();
  const genderIdInt = genderId ? parseInt(genderId, 10) : undefined;
  const { mode } = useThemeMode();
  const { playInList } = useActivePlaylistService();
  const { dataGender } = useSpecificGender(genderIdInt);
  const { tracksOnAGender } = useTracksOfAGender(genderIdInt);
  const navigate = useNavigate();
  const onSelectItem = (trackId: number) => {
    //navigate(`/artist/${artistIdInt}/gender/${genderId}`);
    let currentPlay = 0;
    const listTrackId: number[] = [];
    if (!tracksOnAGender) {
      console.log('Fail to get gender...');
      return;
    }
    for (let iii = 0; iii < tracksOnAGender.length; iii++) {
      listTrackId.push(tracksOnAGender[iii].id);
      if (tracksOnAGender[iii].id === trackId) {
        currentPlay = iii;
      }
    }
    playInList(currentPlay, listTrackId);
  };

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

        <Flex
          direction="column"
          gap="20px"
          marginX="auto"
          padding="20px"
          width="80%"
        >
          {tracksOnAGender?.map((data) => (
            <Box
              minWidth="100%"
              //height="60px"
              border="1px"
              borderColor="brand.900"
              backgroundColor={mode('#FFFFFF88', '#00000088')}
              key={data.id}
              padding="5px"
              as="button"
              _hover={{
                boxShadow: 'outline-over',
                bgColor: mode('#FFFFFFF7', '#000000F7'),
              }}
            >
              <DisplayTrackFull
                track={data}
                onClick={() => onSelectItem(data.id)}
                contextMenu={[
                  {
                    name: 'Edit',
                    onClick: () => {
                      navigate(`/gender/${genderId}/edit-track/${data.id}`);
                    },
                  },
                  { name: 'Add Playlist', onClick: () => { } },
                ]}
              />
            </Box>
          ))}
          <EmptyEnd />
        </Flex>
        <Routes>
          <Route path="edit-track/:trackId" element={<TrackEditPopUp />} />
          <Route path="edit-gender/:genderId" element={<GenderEditPopUp />} />
        </Routes>
      </PageLayout>
    </>
  );
};
