mod common;
mod detail;

pub mod explore;
pub mod home;
pub mod installed;
pub mod local_install;
pub mod search;
pub mod settings;
pub mod system;
pub mod updates;
pub mod webapps;

pub(crate) use common::scroll_to_top;

use super::{Message, NavPage, SoftwareApp};
use cosmic::iced::Size;
use cosmic::Element;

impl SoftwareApp {
    pub(crate) fn view_responsive(&self, size: Size) -> Element<'_, Message> {
        let spacing = cosmic::theme::active().cosmic().spacing;
        let grid_width = (size.width - 2.0 * spacing.space_s as f32)
            .floor()
            .max(0.0) as usize;

        if self.local_file.is_some() {
            return local_install::view(self);
        }

        if let Some(app) = &self.selected {
            return detail::view(self, app, grid_width);
        }

        if !self.search_input.is_empty() && self.search_active && self.active_page() != NavPage::WebApps {
            return search::view(self, grid_width);
        }

        match self.active_page() {
            NavPage::Home => home::view(self, grid_width),
            NavPage::Installed => installed::view(self, grid_width),
            NavPage::Updates => updates::view(self),
            NavPage::WebApps => webapps::view(self, grid_width),
            NavPage::System => system::view(self),
            NavPage::Settings => settings::view(self),
            page if page.is_subcategory() || page.is_top_category() => {
                explore::category_view(self, page.title(), grid_width)
            }
            _ => common::message_page("RakuOS Software", "Select a category to browse apps."),
        }
    }
}

pub(crate) fn review_popup(app: &SoftwareApp) -> Element<'static, Message> {
    detail::write_review_popup(app)
}

pub(crate) fn addons_popup(app: &SoftwareApp) -> Element<'static, Message> {
    detail::addons_popup(app)
}

pub(crate) fn system_popup(app: &SoftwareApp) -> Element<'static, Message> {
    system::popup(app)
}

pub(crate) fn flatpak_add_remote_popup(app: &SoftwareApp) -> Element<'static, Message> {
    settings::flatpak_add_remote_popup(app)
}

pub(crate) fn custom_webapp_popup(app: &SoftwareApp) -> Element<'static, Message> {
    webapps::custom_webapp_popup(app)
}
