use crate::effects::star_field;
use crate::slide::Slide;
use crate::slide_show_config::Context;
use crate::text_drawer::TextConfigBuilder;
use macroquad::color::{BLACK, DARKGREEN, LIME};
use macroquad::math::Vec2;
use macroquad::miniquad::window::screen_size;
use macroquad::prelude::{clear_background, screen_height};
use macroquad::rand::ChooseRandom;
use macroquad::window::screen_width;

#[derive(Debug)]
pub struct Title {
    title: String,
}

impl Title {
    pub fn new(title: String) -> Box<Self> {
        Box::new(Self { title })
    }
}

impl Slide for Title {
    fn display(&mut self, ctx: &Context<'_>) {
        clear_background(BLACK);
        star_field();

        let title_builder: TextConfigBuilder = ctx.slide_show_theme.title_config.clone().into();
        let shadow_style = title_builder.clone().color(DARKGREEN).build();
        let title_style = title_builder.color(LIME).build();

        let center = Vec2::from(screen_size()) * 0.5;
        let center_offset = center + Vec2::from(screen_size()) * 0.005;
        shadow_style.draw_text(&self.title, center_offset);
        title_style.draw_text(&self.title, center);

        let subtitle_builder: TextConfigBuilder =
            ctx.slide_show_theme.heading_config.clone().into();
        let subtitle_style = subtitle_builder.font_size(100).build();

        let author_pos = center + Vec2::new(0.0, screen_height() * 0.33);
        subtitle_style.draw_text("By Joey Hines", author_pos);

        let bits = ["0", "1"];
        let bit_text_size = subtitle_style.measure_text("0");

        let count_needed = (screen_width() / bit_text_size.width).ceil() as usize;

        let mut bit_pos = Vec2::new(0.0, center.y) + Vec2::new(0.0, screen_height() * 0.05);
        for _ in 0..count_needed {
            let bit = bits.choose().unwrap();
            let info = subtitle_style.draw_text(bit, bit_pos);

            bit_pos = info.pos + Vec2::new(info.text_dimensions.width, 0.0);
        }
    }
}
