import { useTranslation } from 'react-i18next';
import { Play, ListPlus, Heart, Download, ChevronRight, ChevronsRight, User, ListMusic, Star, Share2 } from 'lucide-react';
import { useNavigate } from 'react-router-dom';
import { resolveAlbum, resolveMediaServerId } from '@/features/offline';
import { star, unstar } from '@/lib/api/subsonicStarRating';
import type { SubsonicAlbum } from '@/lib/api/subsonicTypes';
import { songToTrack } from '@/lib/media/songToTrack';
import StarRating from '@/ui/StarRating';
import { AlbumToPlaylistSubmenu } from '@/features/contextMenu/components/AlbumArtistToPlaylistSubmenu';
import { MultiAlbumToPlaylistSubmenu } from '@/features/contextMenu/components/MultiAlbumToPlaylistSubmenu';
import type { ContextMenuItemsProps } from '@/features/contextMenu/components/contextMenuItemTypes';

export default function AlbumContextItems(props: ContextMenuItemsProps) {
  const {
    type, item, playNext, enqueue, closeContextMenu,
    setStarredOverride, userRatingOverrides, setKeyboardRating, keyboardRating,
    playlistSubmenuOpen, setPlaylistSubmenuOpen, cancelPlaylistSubmenuCloseTimer, onPlaylistSubmenuTriggerMouseLeave,
    playlistSongIds, setPlaylistSongIds,
    entityRatingSupport, applyAlbumRating,
    handleAction, downloadAlbum, copyShareLink, isStarred,
    pinToPlaybackServer, navigateLibrary, offlinePolicy,
  } = props;
  const { t } = useTranslation();
  const navigate = useNavigate();
  const goLibrary = pinToPlaybackServer ? navigateLibrary : (path: string) => { navigate(path); };

  return (
    <>
        {type === 'album' && (() => {
          const album = item as SubsonicAlbum;
          const albumRatingDisabled = entityRatingSupport === 'track_only';
          return (
            <>
              <div className="context-menu-item" onClick={() => handleAction(() => goLibrary(`/album/${album.id}`))}>
                <Play size={14} /> {t('contextMenu.openAlbum')}
              </div>
              <div className="context-menu-item" onClick={() => handleAction(async () => {
                const serverId = resolveMediaServerId(album.serverId);
                if (!serverId) return;
                const albumData = await resolveAlbum(serverId, album.id);
                if (!albumData) return;
                const tracks = albumData.songs.map(songToTrack);
                if (tracks.length === 0) return;
                playNext(tracks);
              })}>
                <ChevronsRight size={14} /> {t('contextMenu.playNext')}
              </div>
              <div className="context-menu-item" onClick={() => handleAction(async () => {
                const serverId = resolveMediaServerId(album.serverId);
                if (!serverId) return;
                const albumData = await resolveAlbum(serverId, album.id);
                if (!albumData) return;
                enqueue(albumData.songs.map(songToTrack));
              })}>
                <ListPlus size={14} /> {t('contextMenu.enqueueAlbum')}
              </div>
              <div className="context-menu-divider" />
              <div className="context-menu-item" onClick={() => handleAction(() => goLibrary(`/artist/${album.artistId}`))}>
                <User size={14} /> {t('contextMenu.goToArtist')}
              </div>
              {offlinePolicy.canFavorite && (
                <div className="context-menu-item" onClick={() => handleAction(() => {
                  const starred = isStarred(album.id, album.starred);
                  setStarredOverride(album.id, !starred);
                  const meta = {
                    serverId: album.serverId,
                    name: album.name,
                    artist: album.artist,
                    artistId: album.artistId,
                    coverArtId: album.coverArt,
                    year: album.year,
                  };
                  return starred ? unstar(album.id, 'album', meta) : star(album.id, 'album', meta);
                })}>
                  <Heart size={14} fill={isStarred(album.id, album.starred) ? 'currentColor' : 'none'} />
                  {isStarred(album.id, album.starred) ? t('contextMenu.unfavoriteAlbum') : t('contextMenu.favoriteAlbum')}
                </div>
              )}
              {offlinePolicy.canRate && (
                <div
                  className="context-menu-rating-row"
                  data-rating-kind="album"
                  data-rating-id={album.id}
                  data-rating-disabled={albumRatingDisabled ? 'true' : 'false'}
                  onClick={e => e.stopPropagation()}
                >
                  <Star size={14} className="context-menu-rating-icon" aria-hidden />
                  <StarRating
                    value={keyboardRating?.kind === 'album' && keyboardRating.id === album.id
                      ? keyboardRating.value
                      : userRatingOverrides[album.id] ?? album.userRating ?? 0}
                    disabled={albumRatingDisabled}
                    labelKey="entityRating.albumAriaLabel"
                    onChange={r => { setKeyboardRating({ kind: 'album', id: album.id, value: r }); applyAlbumRating(album, r); }}
                  />
                </div>
              )}
              <div className="context-menu-divider" />
              <div className="context-menu-item" onClick={() => handleAction(() => copyShareLink('album', album.id))}>
                <Share2 size={14} /> {t('contextMenu.shareLink')}
              </div>
              {offlinePolicy.canDownload && (
                <div className="context-menu-item" onClick={() => handleAction(() => downloadAlbum(album.name, album.id))}>
                  <Download size={14} /> {t('contextMenu.download')}
                </div>
              )}
              {offlinePolicy.canAddToPlaylist && (
                <div
                  className={`context-menu-item context-menu-item--submenu ${playlistSubmenuOpen && playlistSongIds[0] === `album:${album.id}` ? 'active' : ''}`}
                  data-playlist-trigger-id={`album:${album.id}`}
                  onMouseEnter={() => { cancelPlaylistSubmenuCloseTimer(); setPlaylistSongIds([`album:${album.id}`]); setPlaylistSubmenuOpen(true); }}
                  onMouseLeave={onPlaylistSubmenuTriggerMouseLeave}
                >
                  <ListMusic size={14} /> {t('contextMenu.addToPlaylist')}
                  <ChevronRight size={13} style={{ marginLeft: 'auto' }} />
                  {playlistSubmenuOpen && playlistSongIds[0] === `album:${album.id}` && (
                    <AlbumToPlaylistSubmenu albumId={album.id} triggerId={`album:${album.id}`} onDone={() => { setPlaylistSubmenuOpen(false); closeContextMenu(); }} />
                  )}
                </div>
              )}
            </>
          );
        })()}

        {type === 'multi-album' && (() => {
          const albums = item as SubsonicAlbum[];
          const albumIds = albums.map(a => a.id);
          const albumRatingDisabled = entityRatingSupport === 'track_only';
          const multiAlbumRatingId = [...albumIds].sort().join('\x1e');
          const unifiedAlbumRating = (() => {
            if (albums.length === 0) return 0;
            const vals = albums.map(a => userRatingOverrides[a.id] ?? a.userRating ?? 0);
            const first = vals[0];
            return vals.every(v => v === first) ? first : 0;
          })();
          return (
            <>
              <div className="context-menu-header" style={{ padding: '8px 12px', fontSize: 13, color: 'var(--text-muted)', borderBottom: '1px solid var(--border-subtle)' }}>
                {t('contextMenu.selectedAlbums', { count: albums.length })}
              </div>
              <div className="context-menu-divider" />
              <div className="context-menu-item" onClick={() => handleAction(async () => {
                const results = await Promise.all(albums.map(async a => {
                  const serverId = resolveMediaServerId(a.serverId);
                  if (!serverId) return null;
                  return resolveAlbum(serverId, a.id);
                }));
                const allTracks = results
                  .filter((r): r is NonNullable<typeof r> => r != null)
                  .flatMap(r => r.songs.map(songToTrack));
                enqueue(allTracks);
              })}>
                <ListPlus size={14} /> {t('contextMenu.enqueueAlbums', { count: albums.length })}
              </div>
              {offlinePolicy.canAddToPlaylist && (
                <div
                  className={`context-menu-item context-menu-item--submenu ${playlistSubmenuOpen && playlistSongIds[0] === `multi-album:${albumIds.join(',')}` ? 'active' : ''}`}
                  data-playlist-trigger-id={`multi-album:${albumIds.join(',')}`}
                  onMouseEnter={() => { cancelPlaylistSubmenuCloseTimer(); setPlaylistSongIds([`multi-album:${albumIds.join(',')}`]); setPlaylistSubmenuOpen(true); }}
                  onMouseLeave={onPlaylistSubmenuTriggerMouseLeave}
                >
                  <ListMusic size={14} /> {t('contextMenu.addToPlaylist')}
                  <ChevronRight size={13} style={{ marginLeft: 'auto' }} />
                  {playlistSubmenuOpen && playlistSongIds[0] === `multi-album:${albumIds.join(',')}` && (
                    <MultiAlbumToPlaylistSubmenu albumIds={albumIds} triggerId={`multi-album:${albumIds.join(',')}`} onDone={() => { setPlaylistSubmenuOpen(false); closeContextMenu(); }} />
                  )}
                </div>
              )}
              {offlinePolicy.canRate && (
                <div
                  className="context-menu-rating-row"
                  data-rating-kind="album"
                  data-rating-id={multiAlbumRatingId}
                  data-rating-disabled={albumRatingDisabled ? 'true' : 'false'}
                  onClick={e => e.stopPropagation()}
                >
                  <Star size={14} className="context-menu-rating-icon" aria-hidden />
                  <StarRating
                    value={
                      keyboardRating?.kind === 'album' && keyboardRating.id === multiAlbumRatingId
                        ? keyboardRating.value
                        : unifiedAlbumRating
                    }
                    disabled={albumRatingDisabled}
                    ariaLabel={t('entityRating.selectedAlbumsRatingAriaLabel', { count: albums.length })}
                    onChange={r => {
                      setKeyboardRating({ kind: 'album', id: multiAlbumRatingId, value: r });
                      for (const a of albums) applyAlbumRating(a, r);
                    }}
                  />
                </div>
              )}
            </>
          );
        })()}

    </>
  );
}
