use super::{control_panel::ControlPanelModel, render_panel::RenderPanelModel};
use gtk::prelude::GtkApplicationExt;
use gtk::{
    prelude::{ApplicationExt, ButtonExt, DialogExt, GtkWindowExt, ToggleButtonExt, WidgetExt},
    traits::OrientableExt,
};
use relm4::*;
use adw::prelude::*;


#[derive(Debug)]
pub enum AppMode {
    View,
    Edit,
    Export,
}

#[derive(Debug)]
pub enum AppMsg {
    SetMode(AppMode),
    CloseRequest,
    Close,
}

pub struct AppModel {
    mode: AppMode,
    control: Controller<ControlPanelModel>,
    render: Controller<RenderPanelModel>,
}
relm4::new_action_group!(WindowActionGroup, "win");
relm4::new_stateless_action!(ExampleAction, WindowActionGroup, "example");
relm4::new_stateful_action!(ExampleU8Action, WindowActionGroup, "example2", u8, u8);

#[relm4::component(pub)]
impl SimpleComponent for AppModel {
    type Init = AppMode;
    type Input = AppMsg;
    type Output = ();

    view! {
        main_window=adw::ApplicationWindow {
            set_default_width: 1200,
            set_default_height: 900,
            set_focus_on_click:true,
            // set_titlebar: Some(&gtk::HeaderBar::new()),
            gtk::Box{
                set_orientation: gtk::Orientation::Vertical,
                set_valign:gtk::Align::Fill,
                adw::HeaderBar {
                    #[wrap(Some)]
                    set_title_widget = &adw::WindowTitle {
                    }
                },
                model.control.widget(),
                model.render.widget(),
            },
            connect_close_request[sender] => move |_| {
                sender.input(AppMsg::CloseRequest);
                gtk::Inhibit(true)
            }
        }
    }

    fn init(
        params: Self::Init,
        root: &Self::Root,
        sender: ComponentSender<Self>,
    ) -> ComponentParts<Self> {
        let control = ControlPanelModel::builder()
            .launch(0)
            .forward(sender.input_sender(), |msg| AppMsg::Close);

        let render = RenderPanelModel::builder()
            .launch(())
            .forward(sender.input_sender(), |a| AppMsg::Close);

        relm4::menu! {
        main_menu: {
            custom: "my_widget",
            "Example" => ExampleAction,
            "Example2" => ExampleAction,
            "Example toggle" => ExampleU8Action(1_u8),
            section! {
                "Section example" => ExampleAction,
                "Example toggle" => ExampleU8Action(1_u8),
            },
            section! {
                "Example" => ExampleAction,
                "Example2" => ExampleAction,
                "Example Value" => ExampleU8Action(1_u8),
            },
            "submenu1" {
                "Example" => ExampleAction,
                "Example2" => ExampleAction,
                "Example toggle" => ExampleU8Action(1_u8),
                "submenu2" {
                    "Example" => ExampleAction,
                    "Example2" => ExampleAction,
                    "Example toggle" => ExampleU8Action(1_u8),
                    "submenu3" {
                        "Example" => ExampleAction,
                        "Example2" => ExampleAction,
                        "Example toggle" => ExampleU8Action(1_u8),
                    }
                }
            }
        }
        };

        let app = relm4::main_application();
        relm4_icons::initialize_icons();
        app.set_menubar(Some(&main_menu));

        let model = AppModel {
            mode: AppMode::View,
            control: control,
            render: render,
        };

        let widgets = view_output!();

        ComponentParts { model, widgets }
    }

    fn update(&mut self, msg: Self::Input, _sender: ComponentSender<Self>) {}
}
