// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! Wayland pointer → ltk dispatch.
//!
//! Each `wl_pointer` event (motion / press / release / axis) translates
//! into a call on the surface's [`GestureState`](super::gesture::GestureState)
//! plus the follow-up side-effects that can only live at the
//! [`AppData`] level (pending-message push, surface redraw, dirty-cache
//! invalidation, window move). The pointer handler adds three things
//! touch does not have:
//!
//! * **Hover tracking** — motion updates `SurfaceState::hovered_idx`
//!   before delegating the motion to the gesture machine, since the
//!   hovered widget drives visual state independent of whether a press
//!   is active.
//! * **Title-bar interaction** — a press inside the client-side title
//!   bar either closes the window (close button hit) or initiates a
//!   compositor-driven window move. Neither path goes through the
//!   gesture machine.
//! * **Wheel / axis scroll** — routed directly into the per-viewport
//!   `scroll_offsets` map; axis events do not have a press/release
//!   lifecycle so they bypass the state machine entirely.

use smithay_client_toolkit::seat::pointer::{ PointerEvent, PointerEventKind, PointerHandler };
use smithay_client_toolkit::reexports::client::
{
	protocol::wl_pointer::WlPointer,
	Connection, QueueHandle,
};

use crate::app::App;
use crate::event_loop::{ AppData, SurfaceFocus };

pub( crate ) mod enter;
pub( crate ) mod motion;
pub( crate ) mod press;
pub( crate ) mod release;
pub( crate ) mod scroll;
pub( crate ) mod helpers;

impl<A: App> PointerHandler for AppData<A>
{
	fn pointer_frame(
		&mut self,
		conn:    &Connection,
		qh:       &QueueHandle<Self>,
		pointer: &WlPointer,
		events:   &[PointerEvent],
	)
	{
		for event in events
		{
			if self.is_secondary_surface( &event.surface ) { continue; }
			let focus = self.focus_for_surface( &event.surface )
				.unwrap_or( SurfaceFocus::Main );
			self.pointer_focus = focus;
			match event.kind
			{
				PointerEventKind::Enter   { .. }              => self.on_pointer_enter( conn, qh, pointer, event ),
				PointerEventKind::Motion  { .. }              => self.on_pointer_motion( conn, qh, pointer, event ),
				PointerEventKind::Press   { button: 0x110, .. } => self.on_pointer_left_press( conn, qh, pointer, event ),
				PointerEventKind::Press   { button: 0x111, .. } => self.on_pointer_right_click( conn, qh, pointer, event ),
				PointerEventKind::Release { button: 0x110, .. } => self.on_pointer_release( conn, qh, pointer, event ),
				PointerEventKind::Axis    { .. }              => self.on_pointer_axis( conn, qh, pointer, event ),
				_ => {}
			}
		}
	}
}
