use crate::slide::Slide;
use crate::slide_show_config::Context;
use crate::slides::config::SlideDeckConfig;
use macroquad::camera::set_default_camera;
use macroquad::input::{KeyCode, is_key_pressed};

pub mod config;
pub mod demo;
pub mod projects;
pub mod standard_slide;
pub mod title;

pub struct Slideshow<'a> {
    pub current_slide: usize,
    pub slides: Vec<Box<dyn Slide>>,
    pub ctx: Context<'a>,
}

impl<'a> Slideshow<'a> {
    pub async fn new(ctx: Context<'a>, slide_deck: SlideDeckConfig) -> Self {
        let mut slides = vec![];
        let asset_dir = slide_deck.assets_dir.clone();

        for slide in slide_deck.slides {
            slides.push(slide.slide(&asset_dir).await);
        }

        Self {
            current_slide: 0,
            slides,
            ctx,
        }
    }

    pub fn handle_slide_show(&mut self) -> bool {
        set_default_camera();
        if is_key_pressed(KeyCode::Escape) {
            return true;
        } else if is_key_pressed(KeyCode::Right) {
            self.current_slide += 1;
        } else if is_key_pressed(KeyCode::Left) {
            self.current_slide = self.current_slide.saturating_sub(1);
        }
        self.current_slide = self.current_slide.clamp(0, self.slides.len() - 1);

        self.slides[self.current_slide].display(&self.ctx);

        false
    }
}
