Documentation
use termit::prelude::*;
use w3t::{Fashion, Hatmel, W3t};

#[async_std::main]
async fn main() -> anyhow::Result<()> {
    env_logger::init();
    let mut domino = Hatmel::default();
    domino.load_file("index.html")?;
    let mut styl = Fashion::default();
    styl.load_file("style.css")?;

    let mut termit = Terminal::try_system_default()?
        .into_termit::<NoAppEvent>()
        .enter_raw_mode()?
        .capture_mouse(true)?
        .handle_focus_events(true)?
        .use_alternate_screen(true)?;

    let mut app = App {
        is_terminating: false,
        ui: W3t::default(),
    };

    while !app.is_terminating {
        termit.step(&mut domino, &mut app).await?;
    }

    Ok(())
}

pub struct App<W> {
    is_terminating: bool,
    ui: W,
}

impl<M, A: AppEvent, W: Widget<M, A>> Widget<M, A> for App<W> {
    fn update(
        &mut self,
        model: &mut M,
        input: &Event<A>,
        screen: &mut Screen,
        painter: &Painter,
    ) -> Window {
        if matches!(
            input,
            Event::Key(KeyEvent {
                keycode: KeyCode::Char('q'),
                ..
            })
        ) {
            self.is_terminating = true;
        }

        self.ui.update(model, input, screen, painter)
    }
}