use rltk::{Console, Rltk};
use specs::{Join, ReadStorage, ReadExpect, System};
use crate::components::{Position, Renderable};
use crate::map::Map;

pub struct DrawRenderables<'r> {
    pub ctx: &'r mut Rltk,
}

impl<'a, 'r> System<'a> for DrawRenderables<'r> {
    type SystemData = (
        ReadStorage<'a, Position>,
        ReadStorage<'a, Renderable>,
        ReadExpect<'a, Map>,
    );

    fn run(&mut self, (positions, renderables, map): Self::SystemData) {
        for (pos, render) in (&positions, &renderables).join() {
            let idx = map.xy_idx(pos.x, pos.y);
            if map.visible_tiles[idx] {
                self.ctx.set(pos.x, pos.y, render.fg, render.bg, render.glyph);
            }
        }
    }
}
