import { Slider } from '@olly/modern-sk';
import { Icon } from '../common/Icon';
import { ArtTile } from '../common/ArtTile';
import { useAppDispatch, useAppSelector } from '../../hooks/useAppDispatch';
import {
  pause,
  resume,
  toggleMute,
  setVolume,
  toggleShuffle,
  setRepeat,
  toggleNowPlaying,
  toggleQueue,
} from '../../store/slices/player';
import { useAudioPlayer } from '../../hooks/useAudioPlayer';
import { formatDuration } from '../../lib/format';
import { getCoverUrl } from '../../api/endpoints/streaming';

export function PersistentPlayer() {
  const dispatch = useAppDispatch();
  const { seek, playNext, playPrev } = useAudioPlayer();
  const player = useAppSelector((s) => s.player);
  const queue = useAppSelector((s) => s.queue);
  const currentEntry = queue.entries[queue.currentIndex];

  if (!currentEntry && !player.currentTrackId) {
    return <div className="player empty">Nothing playing</div>;
  }

  const artUrl = getCoverUrl(currentEntry?.albumArtUrl);
  const seedLabel = currentEntry?.albumTitle ?? currentEntry?.title ?? '';
  // Streaming is the web default; local playback is a mobile-client concern.
  const onStream = true;

  return (
    <div className="player">
      {/* now-playing identity */}
      <div
        className="pl-now"
        onClick={() => dispatch(toggleNowPlaying())}
        style={{ cursor: 'pointer' }}
      >
        <ArtTile seed={seedLabel} size={54} label={seedLabel} src={artUrl} />
        <div className="pl-now-tt">
          <div className="t">{currentEntry?.title ?? '—'}</div>
          <div className="a">{currentEntry?.artistName ?? ''}</div>
          <div
            className="pl-srcbadge"
            style={{ color: onStream ? 'var(--fg-3)' : 'var(--lime)' }}
          >
            <Icon name={onStream ? 'cloud' : 'check-circle'} fill={!onStream} />
            {onStream ? 'Streaming · 320 kbps' : 'Local · FLAC'}
          </div>
        </div>
      </div>

      {/* transport + scrubber */}
      <div className="pl-center">
        <div className="pl-transport">
          <button
            type="button"
            className={`pl-tbtn${player.shuffle ? ' on' : ''}`}
            onClick={() => dispatch(toggleShuffle())}
            title="Shuffle"
          >
            <Icon name="shuffle" />
          </button>
          <button
            type="button"
            className="pl-tbtn"
            onClick={playPrev}
            title="Previous"
          >
            <Icon name="skip-back" fill />
          </button>
          <button
            type="button"
            className="pl-play"
            onClick={() =>
              player.isPlaying ? dispatch(pause()) : dispatch(resume())
            }
            title={player.isPlaying ? 'Pause' : 'Play'}
          >
            <Icon name={player.isPlaying ? 'pause' : 'play'} fill />
          </button>
          <button
            type="button"
            className="pl-tbtn"
            onClick={playNext}
            title="Next"
          >
            <Icon name="skip-forward" fill />
          </button>
          <button
            type="button"
            className={`pl-tbtn${player.repeat !== 'none' ? ' on' : ''}`}
            onClick={() =>
              dispatch(
                setRepeat(
                  player.repeat === 'none'
                    ? 'all'
                    : player.repeat === 'all'
                      ? 'one'
                      : 'none',
                ),
              )
            }
            title={`Repeat: ${player.repeat}`}
          >
            <Icon name="repeat" />
          </button>
        </div>
        <div className="pl-seek">
          <span className="pl-time">
            {formatDuration(player.position * 1000)}
          </span>
          <Slider
            className="pl-seek-slider"
            min={0}
            max={player.duration || 1}
            step={1}
            value={[player.position]}
            onValueChange={([v]) => seek(v)}
            aria-label="Seek"
          />
          <span className="pl-time">
            {formatDuration(player.duration * 1000)}
          </span>
        </div>
      </div>

      {/* volume + queue */}
      <div className="pl-right">
        <button
          type="button"
          className="pl-tbtn"
          onClick={() => dispatch(toggleMute())}
          title={player.muted ? 'Unmute' : 'Mute'}
        >
          <Icon name={player.muted ? 'speaker-x' : 'speaker-high'} />
        </button>
        <div className="pl-vol">
          <Slider
            className="pl-vol-slider"
            min={0}
            max={1}
            step={0.01}
            value={[player.muted ? 0 : player.volume]}
            onValueChange={([v]) => dispatch(setVolume(v))}
            aria-label="Volume"
          />
        </div>
        <button
          type="button"
          className={`iconbtn sm${player.isQueueOpen ? ' on' : ''}`}
          onClick={() => dispatch(toggleQueue())}
          title="Play queue"
        >
          <Icon name="queue" />
        </button>
      </div>
    </div>
  );
}
