# home.py

from gi.repository import Gtk, Adw, GLib, Gio
from ...integrations import get_current_integration
from ..album import AlbumButton
from ..artist import ArtistButton
from ..playlist import PlaylistButton
from ..song import SongSmallRow
import threading

@Gtk.Template(resource_path='/com/jeffser/Nocturne/pages/home.ui')
class HomePage(Adw.NavigationPage):
    __gtype_name__ = 'NocturneHomePage'

    header_bar = Gtk.Template.Child()
    search_toggle = Gtk.Template.Child()
    search_bar = Gtk.Template.Child()
    search_entry = Gtk.Template.Child()
    main_stack = Gtk.Template.Child()
    main_clamp = Gtk.Template.Child()
    song_wrapbox = Gtk.Template.Child()
    album_carousel = Gtk.Template.Child()
    artist_carousel = Gtk.Template.Child()
    playlist_carousel = Gtk.Template.Child()

    def __init__(self):
        super().__init__()

        self.settings = Gio.Settings(schema_id="com.jeffser.Nocturne")
        self.max_songs = self.settings.get_value('n-songs-home').unpack()
        self.max_albums = self.settings.get_value('n-albums-home').unpack()
        self.max_artists = self.settings.get_value('n-artists-home').unpack()
        self.max_playlists = self.settings.get_value('n-playlists-home').unpack()
        self.searching = False

        list(self.search_bar)[0].set_transition_type(Gtk.RevealerTransitionType.CROSSFADE)
        self.song_wrapbox.set_header(
            label=_("Songs"),
            icon_name="music-note-symbolic",
            page_tag="songs-all"
        )
        self.song_wrapbox.list_el.set_margin_start(10)
        self.song_wrapbox.list_el.set_margin_end(10)
        self.song_wrapbox.list_el.set_justify(Adw.JustifyMode.FILL)
        self.song_wrapbox.list_el.set_justify_last_line(True)
        self.song_wrapbox.list_el.set_child_spacing(5)
        self.song_wrapbox.list_el.set_line_spacing(5)
        self.album_carousel.set_header(
            label=_("Albums"),
            icon_name="music-queue-symbolic",
            page_tag="albums-all"
        )
        self.artist_carousel.set_header(
            label=_("Artists"),
            icon_name="music-artist-symbolic",
            page_tag="artists"
        )
        self.playlist_carousel.set_header(
            label=_("Playlists"),
            icon_name="playlist-symbolic",
            page_tag="playlists"
        )

    def get_default_results(self) -> dict:
        if integration := get_current_integration():
            songs = integration.getRandomSongs(size=self.max_songs) if self.max_songs > 0 else []
            albums = integration.getAlbumList(list_type="random", size=self.max_albums) if self.max_albums > 0 else []
            artists = integration.getArtists(size=self.max_artists) if self.max_artists > 0 else []
            playlists = integration.getPlaylists()[:self.max_playlists]
            return {
                'song': songs,
                'album': albums,
                'artist': artists,
                'playlist': playlists
            }
        return {}

    def reload(self):
        self.max_songs = self.settings.get_value('n-songs-home').unpack()
        self.max_albums = self.settings.get_value('n-albums-home').unpack()
        self.max_artists = self.settings.get_value('n-artists-home').unpack()
        self.max_playlists = self.settings.get_value('n-playlists-home').unpack()
        threading.Thread(target=self.search).start()
        GLib.idle_add(self.search_mode_toggled, self.search_toggle)

    def reset(self):
        threading.Thread(target=self.song_wrapbox.set_widgets, args=([],), daemon=True).start()
        threading.Thread(target=self.album_carousel.set_widgets, args=([],), daemon=True).start()
        threading.Thread(target=self.artist_carousel.set_widgets, args=([],), daemon=True).start()
        threading.Thread(target=self.playlist_carousel.set_widgets, args=([],), daemon=True).start()

    def search(self):
        if self.searching:
            return
        self.searching = True
        GLib.idle_add(self.main_stack.set_visible_child_name, 'loading')
        if integration := get_current_integration():
            if query := self.search_entry.get_text():
                search_results = integration.search(
                    query=query,
                    songCount=self.max_songs,
                    artistCount=self.max_artists,
                    albumCount=self.max_albums,
                    playlistCount=self.max_playlists
                )
            else:
                search_results = self.get_default_results()
            threading.Thread(
                target=self.song_wrapbox.set_widgets,
                args=([SongSmallRow(id) for id in search_results.get('song', [])],),
                daemon=True
            ).start()
            threading.Thread(
                target=self.album_carousel.set_widgets,
                args=([AlbumButton(id) for id in search_results.get('album', [])],),
                daemon=True
            ).start()
            threading.Thread(
                target=self.artist_carousel.set_widgets,
                args=([ArtistButton(id) for id in search_results.get('artist', [])],),
                daemon=True
            ).start()
            threading.Thread(
                target=self.playlist_carousel.set_widgets,
                args=([PlaylistButton(id) for id in search_results.get('playlist', [])],),
                daemon=True
            ).start()
            has_results = any([len(search_results.get(key)) > 0 for key in list(search_results)])
        else:
            has_results = False
        GLib.idle_add(self.main_stack.set_visible_child_name, 'content' if has_results else 'no-content')
        self.searching = False

    @Gtk.Template.Callback()
    def search_mode_toggled(self, button):
        self.main_clamp.set_margin_top(0 if button.get_active() else self.header_bar.get_height() or 46)

    @Gtk.Template.Callback()
    def on_search(self, entry):
        threading.Thread(target=self.search).start()
