use glam::UVec2;
#[cfg(target_arch = "wasm32")]
use web_sys::HtmlCanvasElement;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::WindowExtWebSys;
use winit::window::Window;

#[cfg(target_arch = "wasm32")]
fn attach_canvas_to_dom(canvas: &HtmlCanvasElement, desired: UVec2) {
    if let Some(window) = web_sys::window() {
        if let Some(document) = window.document() {
            if let Some(body) = document.body() {
                let _ = body.append_child(canvas);
            }
        }
    }

    canvas.set_width(desired.x);
    canvas.set_height(desired.y);

    let style = canvas.style();
    let _ = style.set_property("width", &format!("{}px", desired.x));
    let _ = style.set_property("height", &format!("{}px", desired.y));
    let _ = style.set_property("display", "block");
    let _ = style.set_property("background-color", "#000000");
}

#[allow(unused)]
pub fn setup_window(window: &Window, desired: UVec2) {
    #[cfg(target_arch = "wasm32")]
    if let Some(canvas) = window.canvas() {
        attach_canvas_to_dom(&canvas, desired);
    }
}
