import React, { useState, useEffect, useCallback } from 'react';
import SongTable from './SongTable';
import NowPlaying from './NowPlaying';
import AddSongPanel from './AddSongPanel';
import { TabView, Tab } from './TabView';
import { API, getDisplayTitle, PlaylistItem, ServerEvent } from '../api/player';
import { FaMusic, FaHeart } from 'react-icons/fa';
import useWebSocket from 'react-use-websocket';

enum Tabs {
  Playlist = "playlist",
  Favorites = "favorites",
}

const EmptyContent: React.FC<{ label: string}> = ({label}) => (
  <div className="flex items-center justify-center h-full">
    <div className="text-white text-2xl font-bold">{label}</div>
  </div>
);

interface SonglistContentProps {
  songs: PlaylistItem[];
  isPlaying: boolean;
  onNeedsRefresh: () => void;
}

const PlaylistContent: React.FC<SonglistContentProps> = ({ songs, isPlaying, onNeedsRefresh }) => {
  const handleDelete = (index: number) => {
    API.removeFromPlaylist(index);
    onNeedsRefresh();
  };

  const handleSkipTo = (index: number) => {
    API.skipTo(index);
    onNeedsRefresh();
  };

  return (
    songs.length > 0 ? (
      <SongTable 
        songs={songs} 
        isPlaying={isPlaying} 
        onDelete={handleDelete} 
        onSkipTo={handleSkipTo} 
      />
    ) : (
      <EmptyContent label="Playlist is empty" />
    )
  );
};

const FavoritesContent: React.FC<SonglistContentProps> = ({ songs, isPlaying, onNeedsRefresh }) => {
  const handleDelete = (index: number) => {
    API.removeFromFavorites(index);
    onNeedsRefresh();
  };

  const handleSkipTo = (index: number) => {
    API.replaceCurrentFile(songs[index].filename);
    API.play();
    onNeedsRefresh();
  };

  return (
    songs.length > 0 ? (
      <SongTable 
        songs={songs} 
        isPlaying={isPlaying} 
        onDelete={handleDelete} 
        onSkipTo={handleSkipTo} 
      />
    ) : (
      <EmptyContent label="Favorites are empty" />
    )
  );
};

const App: React.FC = () => {
  const [isPlaying, setIsPlaying] = useState(false);
  const [nowPlayingSong, setNowPlayingSong] = useState<string | null>(null);
  const [nowPlayingFileName, setNowPlayingFileName] = useState<string | null>(null);
  const [volume, setVolume] = useState(100);
  const [volumeSettingIsLocked, setVolumeSettingIsLocked] = useState(false);
  const [playlist, setPlaylist] = useState<PlaylistItem[]>([]);
  const [favorites, setFavorites] = useState<PlaylistItem[]>([]);
  const [selectedTab, setSelectedTab] = useState<Tabs>(Tabs.Playlist);

  const fetchPlaylist = useCallback(async () => {
    const playlist = await API.getPlaylist();
    setPlaylist(playlist);
  }, []);

  const fetchFavorites = useCallback(async () => {
    const favorites = await API.getFavorites();
    setFavorites(favorites);
  }, []);

  const fetchNowPlaying = useCallback(async () => {
    if (volumeSettingIsLocked) {
      // We are actively changing the volume, which we do actually want to send events
      // continuously to the server, but we don't want to refresh our state while doing that. 
      return;
    }

    const nowPlaying = await API.getNowPlaying();
    setNowPlayingSong(getDisplayTitle(nowPlaying.playingItem));
    setNowPlayingFileName(nowPlaying.playingItem.filename);
    setIsPlaying(!nowPlaying.isPaused);
    setVolume(nowPlaying.volume);
  }, [volumeSettingIsLocked]);

  const handleAddURL = async (url: string) => {
    const urlToAdd = url.trim();
    if (urlToAdd) {
      if (selectedTab === Tabs.Favorites) {
        await API.addToFavorites(urlToAdd);
        fetchFavorites();
      } else {
        await API.addToPlaylist(urlToAdd);
        fetchPlaylist();
      }

      if (!isPlaying) {
        await API.play();
      }
    }
  };

  const togglePlayPause = async () => {
    if (isPlaying) {
      await API.pause();
    } else {
      await API.play();
    }

    fetchNowPlaying();
  };

  const handleSkip = async () => {
    await API.skip();
    fetchNowPlaying();
  };

  const handlePrevious = async () => {
    await API.previous();
    fetchNowPlaying();
  };

  const handleVolumeSettingChange = async (volume: number) => {
    setVolume(volume);
    await API.setVolume(volume);
  };

  const handleWebSocketEvent = useCallback((message: MessageEvent) => {
    const event = JSON.parse(message.data);
    switch (event.event) {
      case ServerEvent.PlaylistUpdate:
      case ServerEvent.NowPlayingUpdate:
      case ServerEvent.MetadataUpdate:
      case ServerEvent.MPDUpdate:
        fetchPlaylist();
        fetchNowPlaying();
        break;
      case ServerEvent.VolumeUpdate:
        if (!volumeSettingIsLocked) {
          fetchNowPlaying();
        }

        break;
      case ServerEvent.FavoritesUpdate:
        fetchFavorites();
        break;
    }
  }, [fetchPlaylist, fetchNowPlaying, fetchFavorites]);

  useWebSocket('/api/events', {
    onOpen: () => {
      console.log('WebSocket connected');
    },
    onClose: () => {
      console.log('WebSocket disconnected');
    },
    onError: (error) => {
      console.error('WebSocket error:', error);
    },
    onMessage: handleWebSocketEvent,
    shouldReconnect: () => true,
  });

  // Handle visibility changes
  useEffect(() => {
    const handleVisibilityChange = () => {
      if (document.visibilityState === 'visible') {
        fetchPlaylist();
        fetchNowPlaying();
      }
    };

    document.addEventListener('visibilitychange', handleVisibilityChange);
    return () => {
      document.removeEventListener('visibilitychange', handleVisibilityChange);
    };
  }, [fetchPlaylist, fetchNowPlaying]);

  const refreshContent = () => {
    fetchPlaylist();
    fetchNowPlaying();
    fetchFavorites();
  }

  // Initial data fetch
  useEffect(() => {
    fetchPlaylist();
    fetchNowPlaying();
    fetchFavorites();
  }, [fetchPlaylist, fetchNowPlaying, fetchFavorites]);

  return (
    <div className="flex items-center justify-center h-screen w-screen bg-black md:py-10">
      <div className="bg-violet-900 w-full md:max-w-2xl h-full md:max-h-xl md:border md:rounded-2xl flex flex-col">
        <NowPlaying 
          className="flex flex-row md:rounded-t-2xl" 
          songName={nowPlayingSong || "(Not Playing)"} 
          fileName={nowPlayingFileName || ""} 
          isPlaying={isPlaying} 
          onPlayPause={togglePlayPause} 
          onSkip={handleSkip} 
          onPrevious={handlePrevious} 
          volume={volume}
          onVolumeSettingChange={handleVolumeSettingChange}
          onVolumeWillChange={() => setVolumeSettingIsLocked(true)}
          onVolumeDidChange={() => setVolumeSettingIsLocked(false)}
        />

        <TabView selectedTab={selectedTab} onTabChange={setSelectedTab}>
          <Tab label="Playlist" identifier={Tabs.Playlist} icon={<FaMusic />}>
            <PlaylistContent 
              songs={playlist} 
              isPlaying={isPlaying} 
              onNeedsRefresh={refreshContent} 
            />
          </Tab>
          <Tab label="Favorites" identifier={Tabs.Favorites} icon={<FaHeart />}>
            <FavoritesContent 
              songs={favorites.map(f => ({ ...f, playing: f.filename === nowPlayingFileName }))} 
              isPlaying={isPlaying} 
              onNeedsRefresh={refreshContent} 
            />
          </Tab>
        </TabView>

        <AddSongPanel onAddURL={handleAddURL} />
      </div>
    </div>
  );
};

export default App;