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 { AlbumEditPopUp } from '@/components/popup/AlbumEditPopUp';
import { TrackEditPopUp } from '@/components/popup/TrackEditPopUp';
import { DisplayTrack } from '@/components/track/DisplayTrack';
import { DisplayTrackFull } from '@/components/track/DisplayTrackFull';
import { useActivePlaylistService } from '@/service/ActivePlaylist';
import { useSpecificAlbum } from '@/service/Album';
import { useTracksOfAnAlbum } from '@/service/Track';
import { useThemeMode } from '@/utils/theme-tools';
import { BASE_WRAP_SPACING } from '@/constants/genericSpacing';

export const AlbumDetailPage = () => {
  const { albumId } = useParams();
  const albumIdInt = albumId ? parseInt(albumId, 10) : undefined;
  const { mode } = useThemeMode();
  const { playInList } = useActivePlaylistService();
  const { dataAlbum } = useSpecificAlbum(albumIdInt);
  const { tracksOnAnAlbum } = useTracksOfAnAlbum(albumIdInt);
  const navigate = useNavigate();
  const onSelectItem = (trackId: number) => {
    //navigate(`/artist/${artistIdInt}/album/${albumId}`);
    let currentPlay = 0;
    const listTrackId: number[] = [];
    if (!tracksOnAnAlbum) {
      console.log('Fail to get album...');
      return;
    }
    for (let iii = 0; iii < tracksOnAnAlbum.length; iii++) {
      listTrackId.push(tracksOnAnAlbum[iii].id);
      if (tracksOnAnAlbum[iii].id === trackId) {
        currentPlay = iii;
      }
    }
    playInList(currentPlay, listTrackId);
  };

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

        <Flex
          direction="column"
          gap={BASE_WRAP_SPACING}
          marginX="auto"
          padding="20px"
          width="80%"
        >
          {tracksOnAnAlbum?.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(`/album/${albumId}/edit-track/${data.id}`);
                    },
                  },
                  { name: 'Add Playlist', onClick: () => { } },
                ]}
              />
            </Box>
          ))}
          <EmptyEnd />
        </Flex>
        <Routes>
          <Route path="edit-track/:trackId" element={<TrackEditPopUp />} />
          <Route path="edit-album/:albumId" element={<AlbumEditPopUp />} />
        </Routes>
      </PageLayout>
    </>
  );
};
