//! A widget that displays text

use crate::graphic::font::Font;
use crate::messagebox::MessageBox;
use crate::natives_and_messaging::*;
use crate::widget::NWidget;
use crate::window::Attributes;

#[cfg(windows)]
#[derive(Clone,Copy)]
pub struct Label {
    pub handle: windows_sys::Win32::Foundation::HWND,
}

#[cfg(windows)]
impl Label {
    pub fn new(attributes: Attributes) -> Self {
        use windows_sys::Win32::UI::WindowsAndMessaging::*;
        use windows_sys::Win32::Foundation::WPARAM;
        use windows_sys::Win32::Graphics::Gdi::OUT_DEFAULT_PRECIS;
        use windows_sys::Win32::Graphics::Gdi::{CLIP_DEFAULT_PRECIS, CreateFontA, DEFAULT_QUALITY, FF_ROMAN, FW_NORMAL, SHIFTJIS_CHARSET, VARIABLE_PITCH};
        unsafe {
            let handle = CreateWindowExA(
                0,
                "Static\0".as_ptr(),
                format!("{}\0", attributes.title).as_ptr(),
                0,
                attributes.x,
                attributes.y,
                attributes.width,
                attributes.height,
                0,
                0,
                0,
                std::ptr::null_mut(),
            );
            if handle == 0 {
                match MessageBox::new("NXUI Warning", "NXUI Warning - Code 0002\rWidget could not be initialized\rRunning it as is may disrupt the UI\rPress Yes to continue program execution\r \rDetailed information:\rPanicked at nxui::widget::label\rTo resolve this error, contact the application developer or\rPlease report at https://github.com/LatteSyobon/nxui/issues", MS_WARNING, BS_YESNO).show()
                {
                    MR_NO => {
                        panic!("Exit request from user");
                    }
                    _ => {}
                };
            }
            SendMessageA(handle, WM_SETFONT, CreateFontA(26, 0, 0, 0, FW_NORMAL as i32, 0, 0, 0, SHIFTJIS_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH | FF_ROMAN, "Yu Gothic".as_ptr()) as WPARAM, 1);
            info!("Label has been created. handle: {}", handle);
            Self { handle }
        }
    }

    pub fn get_widget(&self) -> windows_sys::Win32::Foundation::HWND {
        self.handle
    }

    pub fn show(&self) {
        use windows_sys::Win32::UI::WindowsAndMessaging::ShowWindow;
        use windows_sys::Win32::UI::WindowsAndMessaging::SW_SHOWNORMAL;
        unsafe {
            ShowWindow(self.handle, SW_SHOWNORMAL);
            info!("handle: {}", self.handle);
        }
    }
}

#[cfg(windows)]
impl NWidget for Label {
    fn get_handle(self) -> windows_sys::Win32::Foundation::HWND {
        self.handle
    }
    fn set_font(&self,font: Font) {
        use windows_sys::Win32::Foundation::WPARAM;
        use windows_sys::Win32::UI::WindowsAndMessaging::*;
        unsafe {
            SendMessageA(self.handle, WM_SETFONT, font.get_font() as WPARAM, 1);
        }
    }
}

#[cfg(unix)]
pub struct Label {
    pub handle: gtk::Label,
}

#[cfg(unix)]
impl Label {
    pub fn new(attributes: Attributes) -> Self {
        use gtk::prelude::*;
        let handle = gtk::Label::new(Some(&*attributes.title));
        handle.set_height_request(attributes.height / 10);
        handle.set_width_request(attributes.width / 10);
        handle.set_margin_top(attributes.y);
        handle.set_margin_start(attributes.x);
        Self { handle }
    }

    pub fn show(&self) {
        use gtk::prelude::*;
        self.handle.show();
    }
}

#[cfg(unix)]
impl NWidget for Label {
    fn get_handle(self) -> dyn gtk::prelude::WidgetExt {
        self.handle
    }

    fn set_font(&self, font: Font) {

    }
}
