import { useState } from 'react';
import { usePlayerStore } from '../store/playerStore';
import { MiniPlayer } from './MiniPlayer';
import { AudioPlayer } from './AudioPlayer';
import { Dialog, DialogContent, DialogTrigger } from '@/components/ui/dialog';
import { cn } from '@/lib/utils';
import { Maximize2, Minimize2 } from 'lucide-react';
import { Button } from '@/components/ui/button';

export function GlobalPlayer() {
  const currentTrack = usePlayerStore((state) => state.currentTrack);
  const [isExpanded, setIsExpanded] = useState(false);

  // Don't render anything if no track is playing (or perform null check inside components)
  if (!currentTrack) return null;

  return (
    <>
      <MiniPlayer
        isVisible={!isExpanded}
        onToggle={() => setIsExpanded(true)}
        className="border-t bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60"
      />

      <Dialog open={isExpanded} onOpenChange={setIsExpanded}>
        <DialogContent className="max-w-4xl h-[80vh] p-0 overflow-hidden border-none bg-transparent shadow-none sm:rounded-xl">
          <div className="relative w-full h-full bg-background rounded-xl overflow-y-auto border shadow-2xl">
            <div className="absolute top-4 right-4 z-50">
              <Button
                variant="ghost"
                size="icon"
                onClick={() => setIsExpanded(false)}
              >
                <Minimize2 className="h-5 w-5" />
              </Button>
            </div>
            <AudioPlayer
              className="h-full border-none shadow-none"
              compact={false}
            />
          </div>
        </DialogContent>
      </Dialog>
    </>
  );
}
