use std::sync::Arc;
use winit::window::Window;

use crate::errors::AleaticoResult;
use crate::renderer::Renderer;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::EventLoopExtWebSys;

/// State structure
pub struct State {
    pub(crate) window: Arc<Window>,
    /// Renderer
    pub renderer: Renderer,
}

impl State {
    /// Create a new instance of [`State`]
    pub async fn new(window: Arc<Window>) -> AleaticoResult<Self> {
        let renderer = Renderer::new(window.clone()).await?;
        Ok(Self { window, renderer })
    }

    /// Handle app resize
    pub fn resize(&mut self, width: u32, height: u32) {
        self.renderer.resize(width, height);
    }

    /// Update the state
    pub fn update(&mut self) {}

    /// Render the state
    pub fn render(&mut self) -> AleaticoResult<()> {
        self.window.request_redraw();
        self.renderer.render()
    }
}
