use sdl2::rect::{Rect, Point};
use sdl2::event::Event as Sdl2Event;
use sdl2::keyboard::Keycode;
use sdl2::EventPump;

use crate::core::*;
use crate::ui::{ClickArea, ClickAreas, ScreenPos, UI};

pub fn poll(sdl_events: &mut EventPump, click_areas: &ClickAreas, ui: &UI) -> Option<UserInput> {
    for event in sdl_events.poll_iter() {
        match event {
            Sdl2Event::Quit { .. }
            | Sdl2Event::KeyDown {
                keycode: Some(Keycode::Escape),
                ..
            } => return Some(UserInput::Exit()),

            Sdl2Event::MouseMotion { xrel, yrel, .. } => {
                let is_scrolling = ui.scrolling.as_ref().map(|s| s.is_scrolling).unwrap_or(false);
                if is_scrolling {
                    return Some(UserInput::ScrollTo(
                        ui.pixel_ratio as i32 * xrel,
                        ui.pixel_ratio as i32 * yrel,
                    ));
                }
            }

            Sdl2Event::MouseButtonUp { x, y, .. } => {
                let has_scrolled = ui.scrolling.as_ref().map(|s| s.has_scrolled).unwrap_or(false);
                if has_scrolled {
                    return Some(UserInput::EndScrolling());
                } else {
                    let p = ScreenPos(ui.pixel_ratio as i32 * x, ui.pixel_ratio as i32 * y); 

                    for ClickArea {clipping_area, action} in click_areas.iter() {
                        if contains_point(*clipping_area, p) {
                            return Some(action(p));
                        }
                    }
                }
            }

            Sdl2Event::MouseButtonDown { .. } => 


            _ => {}
        }
    }

    None
}


fn contains_point((x, y, w, h): (i32, i32, u32, u32), p: ScreenPos) -> bool {
    Rect::new(x, y, w, h).contains_point(Point::new(p.0, p.1))
}

{
                return Some(UserInput::StartScrolling());
            }