//! Tab buttons to be used in the various pages just under the header

use iced::widget::text::LineHeight;
use iced::widget::{Button, Container, Row, Space, Text, button};
use iced::{Alignment, Length, alignment};

use crate::gui::pages::types::settings_page::SettingsPage;
use crate::gui::styles::button::ButtonType;
use crate::gui::styles::container::ContainerType;
use crate::gui::styles::style_constants::FONT_SIZE_SUBTITLE;
use crate::gui::styles::text::TextType;
use crate::gui::types::message::Message;
use crate::{Language, RunningPage, StyleType};

pub fn get_settings_tabs<'a>(
    active: SettingsPage,
    language: Language,
) -> Row<'a, Message, StyleType> {
    let mut tabs = Row::new()
        .width(Length::Fill)
        .align_y(Alignment::Start)
        .spacing(2)
        .padding([0, 3]);

    for page in &SettingsPage::ALL {
        let active = page.eq(&active);
        tabs = tabs.push(new_settings_tab(*page, active, language));
    }
    tabs
}

pub fn get_pages_tabs<'a>(
    active: RunningPage,
    language: Language,
    unread_notifications: usize,
) -> Row<'a, Message, StyleType> {
    let mut tabs = Row::new()
        .width(Length::Fill)
        .align_y(Alignment::Start)
        .spacing(2)
        .padding([0, 3]);

    for page in &RunningPage::ALL {
        let active = page.eq(&active);
        let unread = if page.eq(&RunningPage::Notifications) {
            Some(unread_notifications)
        } else {
            None
        };
        tabs = tabs.push(new_page_tab(*page, active, language, unread));
    }
    tabs
}

fn new_page_tab<'a>(
    page: RunningPage,
    active: bool,
    language: Language,
    unread: Option<usize>,
) -> Button<'a, Message, StyleType> {
    let mut content = Row::new()
        .height(Length::Fill)
        .align_y(Alignment::Center)
        .push(Space::new().width(Length::Fill))
        .push(
            page.icon()
                .size(15)
                .class(if active {
                    TextType::Title
                } else {
                    TextType::Standard
                })
                .align_x(alignment::Alignment::Center)
                .align_y(alignment::Alignment::Center),
        )
        .push(if active {
            Some(
                Text::new(format!(" {}", page.get_tab_label(language)))
                    .size(FONT_SIZE_SUBTITLE)
                    .class(TextType::Title)
                    .align_x(alignment::Alignment::Center)
                    .align_y(alignment::Alignment::Center),
            )
        } else {
            None
        });

    if let Some(num) = unread
        && num > 0
    {
        content = content
            .push(Space::new().width(7))
            .push(notifications_badge(num));
    }

    content = content.push(Space::new().width(Length::Fill));

    button(content)
        .height(if active { 35 } else { 30 })
        .padding(0)
        .width(Length::FillPortion(if active { 3 } else { 2 }))
        .class(if active {
            ButtonType::TabActive
        } else {
            ButtonType::TabInactive
        })
        .on_press(page.action())
}

fn new_settings_tab<'a>(
    page: SettingsPage,
    active: bool,
    language: Language,
) -> Button<'a, Message, StyleType> {
    let content = Row::new()
        .height(Length::Fill)
        .align_y(Alignment::Center)
        .push(Space::new().width(Length::Fill))
        .push(
            page.icon()
                .size(15)
                .class(if active {
                    TextType::Title
                } else {
                    TextType::Standard
                })
                .align_x(alignment::Alignment::Center)
                .align_y(alignment::Alignment::Center),
        )
        .push(if active {
            Some(
                Text::new(format!(" {}", page.get_tab_label(language)))
                    .size(FONT_SIZE_SUBTITLE)
                    .class(TextType::Title)
                    .align_x(alignment::Alignment::Center)
                    .align_y(alignment::Alignment::Center),
            )
        } else {
            None
        })
        .push(Space::new().width(Length::Fill));

    button(content)
        .height(if active { 35 } else { 30 })
        .padding(0)
        .width(Length::FillPortion(if active { 3 } else { 2 }))
        .class(if active {
            ButtonType::TabActive
        } else {
            ButtonType::TabInactive
        })
        .on_press(page.action())
}

pub fn notifications_badge<'a>(num: usize) -> Container<'a, Message, StyleType> {
    Container::new(
        Text::new(num.to_string())
            .size(14)
            .line_height(LineHeight::Relative(1.0)),
    )
    .align_y(Alignment::Center)
    .padding([2, 4])
    .height(20)
    .class(ContainerType::Highlighted)
}
