#include "component.hh"

#include "modern-qt/core/application.hh"
#include "modern-qt/layout/group.hh"
#include "modern-qt/layout/linear.hh"
#include "modern-qt/layout/mutual-exclusion-group.hh"
#include "modern-qt/utility/material-icon.hh"
#include "modern-qt/utility/theme/theme.hh"
#include "modern-qt/widget/buttons/icon-button.hh"
#include "modern-qt/widget/cards/filled-card.hh"
#include "modern-qt/widget/image.hh"

using namespace creeper;
namespace fc = filled_card::pro;
namespace sg = select_group::pro;
namespace ln = linear::pro;
namespace im = image::pro;
namespace ic = icon_button::pro;

auto NavComponent(NavComponentState& state) noexcept -> raw_pointer<QWidget> {

    const auto AvatarComponent = new Image {
        im::FixedSize {60, 60},
        im::Radius {-1},
        im::ContentScale {ContentScale::CROP},
        im::BorderWidth {3},
        im::PainterResource {
            ":/images/images/logo.png",
            // "./images/logo.png",
        },
    };
    state.manager.append_handler(AvatarComponent, [AvatarComponent](const ThemeManager& manager) {
        const auto colorscheme = manager.color_scheme();
        const auto colorborder = colorscheme.secondary_container;
        AvatarComponent->set_border_color(colorborder);
    });

    const auto navigation_icons_config = std::tuple {
        ic::ThemeManager {state.manager},
        ic::ColorStandard,
        ic::ShapeRound,
        ic::TypesToggleUnselected,
        ic::WidthDefault,
        ic::Font {material::regular::font_1},
        ic::FixedSize {IconButton::kSmallContainerSize},
    };

    return new FilledCard {
        fc::ThemeManager {state.manager},
        fc::Radius {0},
        fc::Level {CardLevel::HIGHEST},
        fc::Layout<Col> {
            ln::Spacing {10},
            ln::Margin {15},
            ln::Item {
                {0, Qt::AlignHCenter},
                AvatarComponent,
            },
            ln::SpacingItem {20},
            ln::Item<SelectGroup<Col, IconButton>> {
                {0, Qt::AlignHCenter},
                ln::Margin {0},
                ln::SpacingItem {10},
                sg::Compose {
                    state.buttons_context | std::views::enumerate,
                    [&](int index, const auto& context) {
                        const auto& [name, icon] = context;

                        const auto status = (index == 0)
                            ? ic::TypesToggleSelected
                            : ic::TypesToggleUnselected;

                        return new IconButton {
                            navigation_icons_config,
                            status,
                            ic::FontIcon(icon.data()),
                            ic::Clickable {[=]{state.switch_callback(index, name);}},
                        };
                    },
                    Qt::AlignHCenter,
                },
                sg::SignalInjection{&IconButton::clicked},
            },
            ln::SpacingItem {40},
            ln::Stretch {255},
            ln::Item<IconButton> {
                {0, Qt::AlignHCenter},
                navigation_icons_config,
                ic::TypesDefault,
                ic::FontIcon {material::icon::kLogout},
                ic::Clickable {&app::quit},
            },
            ln::Item<IconButton> {
                {0, Qt::AlignHCenter},
                navigation_icons_config,
                ic::ColorFilled,
                ic::FontIcon {material::icon::kDarkMode},
                ic::Clickable{[&]{state.manager.toggle_color_mode();state.manager.apply_theme();}},
            }
        }
    };
}

