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

#[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::component(pub)]
impl SimpleComponent for AppModel {
    type Init = AppMode;
    type Input = AppMsg;
    type Output = ();

    view! {
        main_window = gtk::Window {
            set_default_width: 1000,
            set_default_height: 700,
            set_titlebar: Some(&gtk::HeaderBar::new()),
            gtk::Box{
                set_orientation: gtk::Orientation::Vertical,
                set_valign:gtk::Align::Fill,
                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(0)
            .forward(sender.input_sender(), |a| AppMsg::Close);

        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>) {}
}
