use crate::handwriting::{Handwriting, ToolEvent};
use egui::Pos2;

pub const RADIUS: f32 = 12.0;

/// Handle a [ToolEvent]. Returns true if a stroke was completed.
pub fn on_tool_event(handwriting: &mut Handwriting, tool_event: ToolEvent) -> bool {
    match tool_event {
        ToolEvent::Press { at } => {
            erase(handwriting, at, RADIUS)
        }
        ToolEvent::Move { to } => {
            erase(handwriting, to, RADIUS)
        }
        ToolEvent::Release => {
            false            
        }
    }

}

fn erase(handwriting: &mut Handwriting, at: Pos2, radius: f32) -> bool {
    let strokes = handwriting.strokes.len();

    handwriting.strokes.retain(|stroke| {
        stroke.iter().all(|&point| {
            (point - at).length() > radius
        })
    });

    handwriting.strokes.len() < strokes
}
