use uom::si::f64::{Angle, Length};

use crate::a11y::{AccessNode, Role};
use crate::frame::FrameCtx;
use crate::layout::{LayoutPos, LayoutPx, LayoutRect, LayoutSize};
use crate::strings::StringKey;
use crate::theme::Step12;
use crate::widget_id::WidgetId;

use super::checkbox::{Checkbox, CheckboxState, show_checkbox};
use super::dimensioned_input::DimensionedInput;
use super::dropdown::{Dropdown, DropdownItem, DropdownState, show_dropdown};
use super::paint::{LabelText, WidgetPaint};
use super::parsed_input::show_parsed_input;
use super::text_input::{AlwaysValid, Clipboard, TextInput, TextInputState, show_text_input};

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct PropertyCell {
    pub row_id: WidgetId,
    pub label: StringKey,
    pub rect: LayoutRect,
    pub read_only: bool,
}

pub trait PropertyEditor {
    fn render(
        &mut self,
        ctx: &mut FrameCtx<'_>,
        cell: PropertyCell,
        clipboard: &mut dyn Clipboard,
        paint: &mut Vec<WidgetPaint>,
    ) -> bool;
}

pub struct PropertyRow<'a> {
    pub id: WidgetId,
    pub label: StringKey,
    pub editor: &'a mut dyn PropertyEditor,
    pub read_only: bool,
}

pub struct PropertyGrid<'a, 'rows> {
    pub id: WidgetId,
    pub rect: LayoutRect,
    pub label: StringKey,
    pub rows: &'a mut [PropertyRow<'rows>],
    pub row_height: LayoutPx,
    pub label_width: LayoutPx,
    pub padding: LayoutPx,
}

impl<'a, 'rows> PropertyGrid<'a, 'rows> {
    #[must_use]
    pub fn new(
        id: WidgetId,
        rect: LayoutRect,
        label: StringKey,
        rows: &'a mut [PropertyRow<'rows>],
    ) -> Self {
        Self {
            id,
            rect,
            label,
            rows,
            row_height: LayoutPx::new(28.0),
            label_width: LayoutPx::new(120.0),
            padding: LayoutPx::new(8.0),
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct PropertyGridResponse {
    pub changed_rows: Vec<WidgetId>,
    pub paint: Vec<WidgetPaint>,
}

#[must_use]
pub fn show_property_grid(
    ctx: &mut FrameCtx<'_>,
    grid: PropertyGrid<'_, '_>,
    clipboard: &mut dyn Clipboard,
) -> PropertyGridResponse {
    let PropertyGrid {
        id,
        rect,
        label,
        rows,
        row_height,
        label_width,
        padding,
    } = grid;
    ctx.a11y
        .push(id, rect, AccessNode::new(Role::Form).with_label(label));
    let mut paint = vec![WidgetPaint::Surface {
        rect,
        fill: ctx.theme().colors.surface(crate::theme::SurfaceLevel::L0),
        border: None,
        radius: ctx.theme().radius.none,
        elevation: None,
    }];
    let changed_rows = rows
        .iter_mut()
        .enumerate()
        .filter_map(|(idx, row)| {
            let row_rect = property_row_rect(rect, idx, row_height);
            paint.push(WidgetPaint::Label {
                rect: label_rect_at(row_rect, label_width, padding),
                text: LabelText::Key(row.label),
                color: ctx.theme().colors.text_secondary(),
                role: ctx.theme().typography.label,
            });
            paint.push(WidgetPaint::Surface {
                rect: divider_rect(row_rect),
                fill: ctx.theme().colors.neutral.step(Step12::SUBTLE_BORDER),
                border: None,
                radius: ctx.theme().radius.none,
                elevation: None,
            });
            let cell = PropertyCell {
                row_id: row.id,
                label: row.label,
                rect: editor_rect_at(row_rect, label_width, padding),
                read_only: row.read_only,
            };
            row.editor
                .render(ctx, cell, clipboard, &mut paint)
                .then_some(row.id)
        })
        .collect();
    PropertyGridResponse {
        changed_rows,
        paint,
    }
}

fn property_row_rect(grid: LayoutRect, idx: usize, row_height: LayoutPx) -> LayoutRect {
    #[allow(clippy::cast_precision_loss, reason = "row index fits f32 mantissa")]
    let i = idx as f32;
    LayoutRect::new(
        LayoutPos::new(
            grid.origin.x,
            LayoutPx::new(grid.origin.y.value() + i * row_height.value()),
        ),
        LayoutSize::new(grid.size.width, row_height),
    )
}

fn label_rect_at(row: LayoutRect, label_width: LayoutPx, padding: LayoutPx) -> LayoutRect {
    LayoutRect::new(
        LayoutPos::new(
            LayoutPx::new(row.origin.x.value() + padding.value()),
            row.origin.y,
        ),
        LayoutSize::new(
            LayoutPx::saturating_nonneg(label_width.value() - padding.value()),
            row.size.height,
        ),
    )
}

fn editor_rect_at(row: LayoutRect, label_width: LayoutPx, padding: LayoutPx) -> LayoutRect {
    let editor_x = row.origin.x.value() + label_width.value();
    let editor_w = (row.size.width.value() - label_width.value() - padding.value()).max(0.0);
    let row_pad = 4.0;
    LayoutRect::new(
        LayoutPos::new(
            LayoutPx::new(editor_x),
            LayoutPx::new(row.origin.y.value() + row_pad),
        ),
        LayoutSize::new(
            LayoutPx::new(editor_w),
            LayoutPx::saturating_nonneg(row.size.height.value() - 2.0 * row_pad),
        ),
    )
}

fn divider_rect(row: LayoutRect) -> LayoutRect {
    LayoutRect::new(
        LayoutPos::new(
            row.origin.x,
            LayoutPx::new(row.origin.y.value() + row.size.height.value() - 1.0),
        ),
        LayoutSize::new(row.size.width, LayoutPx::new(1.0)),
    )
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BoolEditor {
    pub value: bool,
}

impl BoolEditor {
    #[must_use]
    pub const fn new(value: bool) -> Self {
        Self { value }
    }
}

impl PropertyEditor for BoolEditor {
    fn render(
        &mut self,
        ctx: &mut FrameCtx<'_>,
        cell: PropertyCell,
        _clipboard: &mut dyn Clipboard,
        paint: &mut Vec<WidgetPaint>,
    ) -> bool {
        let cb_state = if self.value {
            CheckboxState::Checked
        } else {
            CheckboxState::Unchecked
        };
        let response = show_checkbox(
            ctx,
            Checkbox::new(cell.row_id, cell.rect, cell.label, cb_state).disabled(cell.read_only),
        );
        paint.extend(response.paint);
        if response.toggled {
            self.value = response.state.is_active();
            true
        } else {
            false
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct TextEditor {
    pub value: String,
    pub buffer: TextInputState,
}

impl TextEditor {
    #[must_use]
    pub fn new(value: impl Into<String>) -> Self {
        let value = value.into();
        Self {
            buffer: TextInputState::from_text(value.clone()),
            value,
        }
    }
}

impl PropertyEditor for TextEditor {
    fn render(
        &mut self,
        ctx: &mut FrameCtx<'_>,
        cell: PropertyCell,
        clipboard: &mut dyn Clipboard,
        paint: &mut Vec<WidgetPaint>,
    ) -> bool {
        let editing = ctx.is_focused(cell.row_id);
        if !editing && self.buffer.text != self.value {
            self.buffer = TextInputState::from_text(self.value.clone());
        }
        let response = show_text_input(
            ctx,
            TextInput {
                id: cell.row_id,
                rect: cell.rect,
                placeholder: cell.label,
                state: &mut self.buffer,
                disabled: cell.read_only,
                validator: AlwaysValid,
            },
            clipboard,
        );
        paint.extend(response.paint);
        if response.edits.is_empty() {
            false
        } else {
            self.value = self.buffer.text.clone();
            true
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct LengthEditor {
    pub value: Length,
    pub buffer: TextInputState,
}

impl LengthEditor {
    #[must_use]
    pub fn new(value: Length) -> Self {
        Self {
            buffer: TextInputState::from_text(format_length(value)),
            value,
        }
    }
}

impl PropertyEditor for LengthEditor {
    fn render(
        &mut self,
        ctx: &mut FrameCtx<'_>,
        cell: PropertyCell,
        clipboard: &mut dyn Clipboard,
        paint: &mut Vec<WidgetPaint>,
    ) -> bool {
        let editing = ctx.is_focused(cell.row_id);
        let formatted = format_length(self.value);
        if !editing && self.buffer.text != formatted {
            self.buffer = TextInputState::from_text(formatted);
        }
        let response = show_parsed_input::<Length, _>(
            ctx,
            DimensionedInput::<Length>::new(cell.row_id, cell.rect, cell.label, &mut self.buffer)
                .disabled(cell.read_only),
            clipboard,
        );
        paint.extend(response.paint);
        match response.committed {
            Some(v) => {
                self.value = v;
                true
            }
            None => false,
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct AngleEditor {
    pub value: Angle,
    pub buffer: TextInputState,
}

impl AngleEditor {
    #[must_use]
    pub fn new(value: Angle) -> Self {
        Self {
            buffer: TextInputState::from_text(format_angle(value)),
            value,
        }
    }
}

impl PropertyEditor for AngleEditor {
    fn render(
        &mut self,
        ctx: &mut FrameCtx<'_>,
        cell: PropertyCell,
        clipboard: &mut dyn Clipboard,
        paint: &mut Vec<WidgetPaint>,
    ) -> bool {
        let editing = ctx.is_focused(cell.row_id);
        let formatted = format_angle(self.value);
        if !editing && self.buffer.text != formatted {
            self.buffer = TextInputState::from_text(formatted);
        }
        let response = show_parsed_input::<Angle, _>(
            ctx,
            DimensionedInput::<Angle>::new(cell.row_id, cell.rect, cell.label, &mut self.buffer)
                .disabled(cell.read_only),
            clipboard,
        );
        paint.extend(response.paint);
        match response.committed {
            Some(v) => {
                self.value = v;
                true
            }
            None => false,
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct PropertyOption {
    pub label: StringKey,
}

#[derive(Clone, Debug, PartialEq)]
pub struct SelectionEditor {
    pub options: Vec<PropertyOption>,
    pub current: Option<usize>,
    pub state: DropdownState,
}

impl SelectionEditor {
    #[must_use]
    pub fn new(options: Vec<PropertyOption>, current: Option<usize>) -> Self {
        Self {
            options,
            current,
            state: DropdownState::closed(),
        }
    }
}

impl PropertyEditor for SelectionEditor {
    fn render(
        &mut self,
        ctx: &mut FrameCtx<'_>,
        cell: PropertyCell,
        _clipboard: &mut dyn Clipboard,
        paint: &mut Vec<WidgetPaint>,
    ) -> bool {
        let items: Vec<DropdownItem<usize>> = self
            .options
            .iter()
            .enumerate()
            .map(|(i, opt)| DropdownItem {
                value: i,
                label: opt.label,
            })
            .collect();
        let response = show_dropdown(
            ctx,
            Dropdown::new(
                cell.row_id,
                cell.rect,
                LayoutPx::new(24.0),
                items,
                self.current,
                cell.label,
                &mut self.state,
            )
            .disabled(cell.read_only),
        );
        paint.extend(response.paint);
        if response.changed {
            self.current = response.selected;
            true
        } else {
            false
        }
    }
}

fn format_length(value: Length) -> String {
    use uom::si::length::millimeter;
    format!("{} mm", value.get::<millimeter>())
}

fn format_angle(value: Angle) -> String {
    use uom::si::angle::degree;
    format!("{} deg", value.get::<degree>())
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::{
        BoolEditor, PropertyGrid, PropertyRow, SelectionEditor, TextEditor, show_property_grid,
    };
    use crate::focus::FocusManager;
    use crate::frame::FrameCtx;
    use crate::hit_test::{HitFrame, HitState, resolve};
    use crate::hotkey::HotkeyTable;
    use crate::input::{
        FrameInstant, InputSnapshot, PointerButton, PointerButtonMask, PointerSample,
    };
    use crate::layout::{LayoutPos, LayoutPx, LayoutRect, LayoutSize};
    use crate::strings::{StringKey, StringTable};
    use crate::theme::Theme;
    use crate::widget_id::{WidgetId, WidgetKey};
    use crate::widgets::text_input::MemoryClipboard;

    fn grid_id() -> WidgetId {
        WidgetId::ROOT.child(WidgetKey::new("grid"))
    }

    fn rect() -> LayoutRect {
        LayoutRect::new(
            LayoutPos::new(LayoutPx::ZERO, LayoutPx::ZERO),
            LayoutSize::new(LayoutPx::new(280.0), LayoutPx::new(200.0)),
        )
    }

    fn press(pos: LayoutPos) -> InputSnapshot {
        let mut s = InputSnapshot::idle(FrameInstant::ZERO);
        s.pointer = Some(PointerSample::new(pos));
        s.buttons_pressed = PointerButtonMask::just(PointerButton::Primary);
        s
    }

    fn release(pos: LayoutPos) -> InputSnapshot {
        let mut s = InputSnapshot::idle(FrameInstant::ZERO);
        s.pointer = Some(PointerSample::new(pos));
        s.buttons_released = PointerButtonMask::just(PointerButton::Primary);
        s
    }

    fn idle(pos: LayoutPos) -> InputSnapshot {
        let mut s = InputSnapshot::idle(FrameInstant::ZERO);
        s.pointer = Some(PointerSample::new(pos));
        s
    }

    #[test]
    fn click_bool_row_marks_row_changed_and_flips_value() {
        let row_id = grid_id().child(WidgetKey::new("show_grid"));
        let mut bool_editor = BoolEditor::new(false);
        let mut clipboard = MemoryClipboard::default();
        let mut focus = FocusManager::new();
        let mut prev = HitState::new();
        let click_pos = LayoutPos::new(LayoutPx::new(160.0), LayoutPx::new(14.0));
        let theme = Arc::new(Theme::light());
        let table = HotkeyTable::new();
        let mut last: Option<super::PropertyGridResponse> = None;
        [press(click_pos), release(click_pos), idle(click_pos)]
            .into_iter()
            .for_each(|mut snap| {
                let mut hits = HitFrame::new();
                let response = {
                    let mut shaper = bone_text::Shaper::new();
                    let mut a11y = crate::a11y::AccessTreeBuilder::new();
                    let mut ctx = FrameCtx::new(
                        theme.clone(),
                        &mut snap,
                        &mut focus,
                        &table,
                        StringTable::empty(),
                        &mut hits,
                        &prev,
                        &mut a11y,
                        &mut shaper,
                    );
                    let mut rows = [PropertyRow {
                        id: row_id,
                        label: StringKey::new("prop.show_grid"),
                        editor: &mut bool_editor,
                        read_only: false,
                    }];
                    show_property_grid(
                        &mut ctx,
                        PropertyGrid::new(
                            grid_id(),
                            rect(),
                            StringKey::new("test.grid"),
                            &mut rows,
                        ),
                        &mut clipboard,
                    )
                };
                last = Some(response);
                prev = resolve(&prev, &hits, &snap, focus.focused());
            });
        let Some(response) = last else {
            panic!("response missing")
        };
        assert_eq!(response.changed_rows, vec![row_id]);
        assert!(bool_editor.value);
    }

    #[test]
    fn unfocused_text_editor_adopts_external_value_change() {
        let row_id = grid_id().child(WidgetKey::new("name"));
        let mut text_editor = TextEditor::new("alpha");
        let mut clipboard = MemoryClipboard::default();
        let mut focus = FocusManager::new();
        let prev = HitState::new();
        let mut snap = InputSnapshot::idle(FrameInstant::ZERO);
        let theme = Arc::new(Theme::light());
        let table = HotkeyTable::new();
        let mut hits = HitFrame::new();
        {
            let mut shaper = bone_text::Shaper::new();
            let mut a11y = crate::a11y::AccessTreeBuilder::new();
            let mut ctx = FrameCtx::new(
                theme.clone(),
                &mut snap,
                &mut focus,
                &table,
                StringTable::empty(),
                &mut hits,
                &prev,
                &mut a11y,
                &mut shaper,
            );
            let mut rows = [PropertyRow {
                id: row_id,
                label: StringKey::new("prop.name"),
                editor: &mut text_editor,
                read_only: false,
            }];
            let _ = show_property_grid(
                &mut ctx,
                PropertyGrid::new(grid_id(), rect(), StringKey::new("test.grid"), &mut rows),
                &mut clipboard,
            );
        }
        text_editor.value = "beta".into();
        let mut snap = InputSnapshot::idle(FrameInstant::ZERO);
        let mut hits = HitFrame::new();
        {
            let mut shaper = bone_text::Shaper::new();
            let mut a11y = crate::a11y::AccessTreeBuilder::new();
            let mut ctx = FrameCtx::new(
                theme,
                &mut snap,
                &mut focus,
                &table,
                StringTable::empty(),
                &mut hits,
                &prev,
                &mut a11y,
                &mut shaper,
            );
            let mut rows = [PropertyRow {
                id: row_id,
                label: StringKey::new("prop.name"),
                editor: &mut text_editor,
                read_only: false,
            }];
            let _ = show_property_grid(
                &mut ctx,
                PropertyGrid::new(grid_id(), rect(), StringKey::new("test.grid"), &mut rows),
                &mut clipboard,
            );
        }
        assert_eq!(text_editor.buffer.text, "beta");
    }

    #[test]
    fn tab_advances_focus_through_rows_in_order() {
        use crate::focus::FocusRequest;

        let mut bool_a = BoolEditor::new(false);
        let mut bool_b = BoolEditor::new(false);
        let mut clipboard = MemoryClipboard::default();
        let mut focus = FocusManager::new();
        let prev = HitState::new();
        let theme = Arc::new(Theme::light());
        let table = HotkeyTable::new();
        let alpha_id = grid_id().child(WidgetKey::new("row_a"));
        let beta_id = grid_id().child(WidgetKey::new("row_b"));
        let mut render = |focus: &mut FocusManager,
                          bool_a: &mut BoolEditor,
                          bool_b: &mut BoolEditor,
                          mut snap: InputSnapshot| {
            let mut hits = HitFrame::new();
            let mut shaper = bone_text::Shaper::new();
            let mut a11y = crate::a11y::AccessTreeBuilder::new();
            let mut ctx = FrameCtx::new(
                theme.clone(),
                &mut snap,
                focus,
                &table,
                StringTable::empty(),
                &mut hits,
                &prev,
                &mut a11y,
                &mut shaper,
            );
            let mut rows = [
                PropertyRow {
                    id: alpha_id,
                    label: StringKey::new("prop.a"),
                    editor: bool_a,
                    read_only: false,
                },
                PropertyRow {
                    id: beta_id,
                    label: StringKey::new("prop.b"),
                    editor: bool_b,
                    read_only: false,
                },
            ];
            let _ = show_property_grid(
                &mut ctx,
                PropertyGrid::new(grid_id(), rect(), StringKey::new("test.grid"), &mut rows),
                &mut clipboard,
            );
        };

        focus.request(FocusRequest::Advance);
        render(
            &mut focus,
            &mut bool_a,
            &mut bool_b,
            InputSnapshot::idle(FrameInstant::ZERO),
        );
        assert_eq!(focus.focused(), Some(alpha_id));

        focus.request(FocusRequest::Advance);
        render(
            &mut focus,
            &mut bool_a,
            &mut bool_b,
            InputSnapshot::idle(FrameInstant::ZERO),
        );
        assert_eq!(focus.focused(), Some(beta_id));

        focus.request(FocusRequest::Retreat);
        render(
            &mut focus,
            &mut bool_a,
            &mut bool_b,
            InputSnapshot::idle(FrameInstant::ZERO),
        );
        assert_eq!(focus.focused(), Some(alpha_id));
    }

    #[test]
    fn rows_paint_label_per_row() {
        let mut bool_editor = BoolEditor::new(false);
        let mut select_editor = SelectionEditor::new(
            vec![super::PropertyOption {
                label: StringKey::new("opt"),
            }],
            None,
        );
        let mut clipboard = MemoryClipboard::default();
        let mut focus = FocusManager::new();
        let mut snap = InputSnapshot::idle(FrameInstant::ZERO);
        let prev = HitState::new();
        let theme = Arc::new(Theme::light());
        let table = HotkeyTable::new();
        let mut hits = HitFrame::new();
        let response = {
            let mut shaper = bone_text::Shaper::new();
            let mut a11y = crate::a11y::AccessTreeBuilder::new();
            let mut ctx = FrameCtx::new(
                theme,
                &mut snap,
                &mut focus,
                &table,
                StringTable::empty(),
                &mut hits,
                &prev,
                &mut a11y,
                &mut shaper,
            );
            let mut rows = [
                PropertyRow {
                    id: grid_id().child(WidgetKey::new("a")),
                    label: StringKey::new("prop.a"),
                    editor: &mut bool_editor,
                    read_only: false,
                },
                PropertyRow {
                    id: grid_id().child(WidgetKey::new("b")),
                    label: StringKey::new("prop.b"),
                    editor: &mut select_editor,
                    read_only: false,
                },
            ];
            show_property_grid(
                &mut ctx,
                PropertyGrid::new(grid_id(), rect(), StringKey::new("test.grid"), &mut rows),
                &mut clipboard,
            )
        };
        let label_count = response
            .paint
            .iter()
            .filter(|p| matches!(p, super::WidgetPaint::Label { .. }))
            .count();
        assert!(label_count >= 2);
    }
}
