use godot::classes::notify::NodeNotification;
use godot::prelude::*;
use godot::classes::SubViewport;

#[derive(GodotClass)]
#[class(base=SubViewport)]
struct DynamicSubViewport {
    base: Base<SubViewport>
}

use godot::classes::ISubViewport;

#[godot_api]
impl ISubViewport for DynamicSubViewport {

    fn init(base: Base<SubViewport>) -> Self {
        Self {
            base,
        }
    }

    fn on_notification(&mut self, what: NodeNotification) {
        match what {
            NodeNotification::READY => {
                let root = self.base_mut().get_tree().unwrap().get_root();
                match root {
                    Some(v) => {
                            self.base_mut().set_size(v.get_size());
                    },
                    _ => {}
                }
            }
            NodeNotification::PHYSICS_PROCESS => {
                let root = self.base_mut().get_tree().unwrap().get_root();
                match root {
                    Some(v) => {
                            self.base_mut().set_size(v.get_size());
                    },
                    _ => {}
                }
            }
            // FIXME: WM_SIZE_CHANGED is not what we are looking for, find a better way of detecting
            // when the viewport size is changed so we don't have to make the viewport the same size as root every frame 
            
            // NodeNotification::WM_SIZE_CHANGED => {
            //     let root = self.base_mut().get_tree().unwrap().get_root();
            //     match root {
            //         Some(v) => {
            //                 self.base_mut().set_size(v.get_size());
            //         },
            //         _ => {}
            //     }
            // }
            _ => {}
        }
    }
}