#![warn(clippy::all,clippy::pedantic)]
use crate::prelude::*;

#[system]
#[read_component(Point)]
#[read_component(Name)]
#[read_component(Health)]
#[read_component(Player)]
#[read_component(FieldOfView)]
pub fn tooltips(
    ecs: &SubWorld,
    #[resource] mouse_pos: &Point,
    #[resource] camera: &Camera
){
    //get mouse position and camera top left, 
    //convert the position to position on map
    let mut position = <(Entity, &Point, &Name)>::query();
    let offset = Point::new(camera.left_x, camera.top_y);
    let map_pos = *mouse_pos + offset;

    let mut fov = <&FieldOfView>::query().filter(component::<Player>());
    let player_fov = fov.iter(ecs).nth(0).unwrap();

    let mut draw_batch = DrawBatch::new();
    draw_batch.target(3);
    position
        .iter(ecs)
        .filter(|(_,pos,_)|
            **pos == map_pos && player_fov.visible_tiles.contains(&pos))
        .for_each(|(entity, _, name)| {
            let screen_pos = *mouse_pos *4; //terminal is 4 times larger
            let display = if let Ok(Health) = ecs.entry_ref(*entity)
                .unwrap()
                .get_component::<Health>()
                {
                    format!("{} : {}/{} hp", &name.0, Health.current, Health.max)
                }else {
                    name.0.clone()
                };
            
            draw_batch.print(screen_pos, &display);
        });
    draw_batch.submit(10100).expect("Batch error");
}