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

use super::*;
use crate::types::Rect;

#[ test ]
fn value_clamped_on_creation()
{
	let s = slider::<()>( 1.5 );
	assert_eq!( s.value, 1.0 );
	let s = slider::<()>( -0.5 );
	assert_eq!( s.value, 0.0 );
}

#[ test ]
fn value_from_x_left_edge()
{
	let s = slider::<()>( 0.5 );
	let rect = Rect { x: 0.0, y: 0.0, width: 200.0, height: 36.0 };
	let v = s.value_from_x( rect, 0.0 );
	assert_eq!( v, 0.0 );
}

#[ test ]
fn value_from_x_right_edge()
{
	let s = slider::<()>( 0.5 );
	let rect = Rect { x: 0.0, y: 0.0, width: 200.0, height: 36.0 };
	let v = s.value_from_x( rect, 200.0 );
	assert_eq!( v, 1.0 );
}

#[ test ]
fn value_from_x_center()
{
	let s = slider::<()>( 0.0 );
	let rect = Rect { x: 0.0, y: 0.0, width: 200.0, height: 36.0 };
	let v = s.value_from_x( rect, 100.0 );
	assert!( (v - 0.5).abs() < 0.1 );
}

#[ test ]
fn axis_dispatch_horizontal_uses_x()
{
	let rect = Rect { x: 0.0, y: 0.0, width: 200.0, height: 36.0 };
	let v = value_from_pos_in_rect(
		rect,
		Point { x: 200.0, y: 9999.0 },
		SliderAxis::Horizontal,
		super::thumb_design_px(),
	);
	assert_eq!( v, 1.0 );
}

#[ test ]
fn axis_dispatch_vertical_uses_y()
{
	let rect = Rect { x: 0.0, y: 0.0, width: 56.0, height: 160.0 };
	// y=0 at the top of a vertical rect is value=1.0, regardless of x.
	let v = value_from_pos_in_rect(
		rect,
		Point { x: 9999.0, y: 0.0 },
		SliderAxis::Vertical,
		super::thumb_design_px(),
	);
	assert_eq!( v, 1.0 );
}

#[ test ]
fn value_from_x_respects_thumb_pad()
{
	let rect = Rect { x: 0.0, y: 0.0, width: 100.0, height: 36.0 };
	// A 20 px thumb → 10 px pad → the track spans x in [10, 90].
	assert_eq!( super::value_from_x_in_rect( rect, 10.0, 20.0 ), 0.0 );
	assert_eq!( super::value_from_x_in_rect( rect, 90.0, 20.0 ), 1.0 );
	assert_eq!( super::value_from_x_in_rect( rect, 50.0, 20.0 ), 0.5 );
	// A bigger thumb → bigger pad → the same x maps to a smaller value.
	assert!(
		super::value_from_x_in_rect( rect, 20.0, 40.0 )
		< super::value_from_x_in_rect( rect, 20.0, 20.0 )
	);
}

#[ test ]
fn track_paint_default_is_none()
{
	let s = slider::<()>( 0.5 );
	assert!( s.track_paint.is_none() );
}

#[ test ]
fn track_paint_builder_stores_paint()
{
	use crate::theme::{ ColorStop, GradientSpace, LinearGradient, Paint };
	use crate::types::Color;
	let g = Paint::Linear( LinearGradient
	{
		angle_deg: 90.0,
		stops:     vec![
			ColorStop { position: 0.0, color: Color::rgba( 1.0, 0.0, 0.0, 1.0 ) },
			ColorStop { position: 1.0, color: Color::rgba( 0.0, 0.0, 1.0, 1.0 ) },
		],
		space:     GradientSpace::Srgb,
	} );
	let s = slider::<()>( 0.5 ).track_paint( g );
	assert!( s.track_paint.is_some() );
}
