use crate::PlaylistCard;
use adw::prelude::AdwApplicationWindowExt;
use gio::resources_lookup_data;
use gtk::{prelude::*, STYLE_PROVIDER_PRIORITY_USER};
use std::iter::Iterator;

#[derive(Clone)]
pub struct ApplicationWindow {
    pub window: adw::ApplicationWindow,
    pub layout: gtk::FlowBox,
    pub playlists: Vec<PlaylistCard>,
}

impl ApplicationWindow {
    pub fn new(app: &adw::Application) -> Self {
        let window = adw::ApplicationWindow::builder()
            .application(app)
            .title("GM-control-panel")
            .width_request(500)
            .build();

        let stylesheet = String::from_utf8(
            resources_lookup_data(
                "/com/luminescent-dreams/gm-control-panel/style.css",
                gio::ResourceLookupFlags::NONE,
            )
            .expect("stylesheet should just be available")
            .to_vec(),
        )
        .expect("to parse stylesheet");

        let provider = gtk::CssProvider::new();
        provider.load_from_data(&stylesheet);
        let context = window.style_context();
        context.add_provider(&provider, STYLE_PROVIDER_PRIORITY_USER);

        let layout = gtk::FlowBox::new();

        let playlists: Vec<PlaylistCard> = vec![
            "Creepy Cathedral",
            "Joyful Tavern",
            "Exploring",
            "Out on the streets",
            "The North Abbey",
        ]
        .into_iter()
        .map(|name| {
            let playlist = PlaylistCard::default();
            playlist.set_name(name);
            playlist
        })
        .collect();

        playlists.iter().for_each(|card| layout.append(card));

        window.set_content(Some(&layout));

        Self {
            window,
            layout,
            playlists,
        }
    }
}
