mod custom_layout;
mod imp;
use gtk::{glib, prelude::*, subclass::prelude::*};
use std::cell::{Ref, RefCell, RefMut};
use std::sync::{Arc, Mutex};

glib::wrapper! {
    pub struct DynamicCol(ObjectSubclass<imp::DynamicCol>)
    @extends gtk::Widget;
}

impl Default for DynamicCol {
    fn default() -> Self {
        Self::new()
    }
}

impl DynamicCol {
    pub fn new() -> Self {
        let this: Self = glib::Object::new();
        this
    }

    pub fn set_ratio(&self, ratio: f64) {
        let self_ = self.imp();
        self_.ratio.replace(Some(ratio));
        self.queue_resize();
    }

    pub fn set_start_width(&self, ratio: i32) {
        let self_ = self.imp();
        self_.start_width.replace(Some(ratio));
        self.queue_resize();
    }

    pub fn set_end_width(&self, ratio: i32) {
        let self_ = self.imp();
        self_.end_width.replace(Some(ratio));
        self.queue_resize();
    }

    pub fn get_ratio(&self) -> Option<f64> {
        let self_ = self.imp();
        *self_.ratio.borrow()
    }

    pub fn set_child_paned(&self, widget: Option<&gtk::Paned>) {
        let imp = self.imp();
        let widget = widget.map(|w| w.upcast_ref());

        if widget == imp.child.borrow().as_ref() {
            return;
        }

        if let Some(child) = imp.child.borrow_mut().take() {
            child.unparent();
        }

        if let Some(w) = widget {
            imp.child.replace(Some(w.clone()));
            w.set_parent(self);
        }

        self.queue_resize();
        self.notify("child")
    }

    // pub fn set_child(&self, widget: Option<&impl IsA<gtk::Widget>>) {

    //     let imp = self.imp();
    //     let widget = widget.map(|w| w.upcast_ref());

    //     if widget == imp.child.borrow().as_ref() {
    //         return;
    //     }

    //     if let Some(child) = imp.child.borrow_mut().take() {
    //         child.unparent();
    //     }

    //     if let Some(w) = widget {
    //         imp.child.replace(Some(w.clone()));
    //         w.set_parent(self);
    //     }

    //     self.queue_resize();
    //     self.notify("child")
    // }

}
