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

use crate::types::{ Color, Rect };
use crate::render::Canvas;
use super::Element;

mod theme;

/// A horizontal divider line.
///
/// Renders a 1 px (default) line across the full width of its layout rect,
/// with vertical padding above and below. Use to break a column into
/// visual sections — between settings groups, list categories or content
/// blocks. The line takes the divider colour from the active theme by
/// default; override with [`Self::color`] for custom palettes.
///
/// ```rust,no_run
/// # use ltk::{ column, separator, text, Element };
/// # #[ derive( Clone ) ] enum Msg {}
/// # fn _ex() -> Element<Msg> {
/// // In view():
/// column()
///     .push( text( "General" ) )
///     .push( separator() )
///     .push( text( "Network" ) )
/// .into()
/// # }
/// ```
pub struct Separator
{
	/// Stroke colour. Defaults to the theme's `divider` palette slot.
	pub color:     Color,
	/// Line thickness in logical pixels.
	pub thickness: f32,
	/// Vertical padding above and below the line, baked into
	/// `preferred_size` so the divider visually centres in the row a
	/// parent column allocates.
	pub pad_v:     f32,
}

impl Separator
{
	/// Create a separator with the theme's default divider colour and
	/// 1 px thickness.
	pub fn new() -> Self
	{
		Self
		{
			color:     theme::color(),
			thickness: theme::THICKNESS,
			pad_v:     theme::PAD_V,
		}
	}

	/// Override the stroke colour. Useful for emphasised dividers between
	/// destructive actions.
	pub fn color( mut self, color: Color ) -> Self
	{
		self.color = color;
		self
	}

	/// Override the line thickness. Defaults to 1 logical pixel.
	pub fn thickness( mut self, t: f32 ) -> Self
	{
		self.thickness = t;
		self
	}

	/// Return the preferred `(width, height)`. Width is `max_width`;
	/// height is `thickness + 2 × pad_v`.
	pub fn preferred_size( &self, max_width: f32 ) -> (f32, f32)
	{
		( max_width, self.thickness + self.pad_v * 2.0 )
	}

	/// Draw the divider line into `canvas` at `rect`.
	pub fn draw( &self, canvas: &mut Canvas, rect: Rect )
	{
		let y = rect.y + rect.height / 2.0;
		canvas.draw_line( rect.x, y, rect.x + rect.width, y, self.color, self.thickness );
	}
}

/// Create a default [`Separator`] (theme divider colour, 1 px thickness).
///
/// ```rust,no_run
/// # use ltk::{ column, separator, text, Element };
/// # #[ derive( Clone ) ] enum Msg {}
/// # fn _ex() -> Element<Msg> {
/// column()
///     .push( text( "Section A" ) )
///     .push( separator() )
///     .push( text( "Section B" ) )
/// .into()
/// # }
/// ```
pub fn separator() -> Separator
{
	Separator::new()
}

impl<Msg: Clone> From<Separator> for Element<Msg>
{
	fn from( s: Separator ) -> Self
	{
		Element::Separator( s )
	}
}

#[cfg(test)]
mod tests;
