use acadrust::entities::Arc;
use truck_modeling::{builder, Point3};

use crate::command::EntityTransform;
use crate::entities::common::{
    center_grip, edit_angle_prop as edit_angle, edit_prop as edit, parse_f64, ro_prop as ro, square_grip,
};
use crate::entities::traits::TruckConvertible;
use crate::scene::convert::acad_to_truck::{extrusion_wall_tris, TruckEntity, TruckObject};
use crate::scene::model::object::{GripApply, GripDef, PropSection};
use crate::scene::model::wire_model::{SnapHint, TangentGeom};

const TAU: f64 = std::f64::consts::TAU;

fn to_truck(arc: &Arc) -> TruckEntity {
    let cx = arc.center.x;
    let cy = arc.center.y;
    let cz = arc.center.z;
    let r = arc.radius;
    let sa = arc.start_angle;
    let ea = arc.end_angle;
    let normal = (arc.normal.x, arc.normal.y, arc.normal.z);

    // Compute OCS basis vectors for this entity's normal.
    let (ax, ay) = crate::scene::view::transform::ocs_axes(normal);

    let ccw_end = if ea >= sa { ea } else { ea + TAU };
    let mid_a = sa + (ccw_end - sa) * 0.5;

    // Arc centre in WCS.
    let (cwx, cwy, cwz) = crate::scene::view::transform::ocs_point_to_wcs((cx, cy, cz), normal);

    // Arc points in WCS: centre_wcs + r*cos(a)*Ax + r*sin(a)*Ay
    let arc_pt = |a: f64| {
        let (c, s) = (a.cos(), a.sin());
        Point3::new(
            cwx + r * c * ax.0 + r * s * ay.0,
            cwy + r * c * ax.1 + r * s * ay.1,
            cwz + r * c * ax.2 + r * s * ay.2,
        )
    };

    let cv = glam::DVec3::new(cwx, cwy, cwz);
    // Arc-length centre — one well-defined midpoint snap. Circles and
    // ellipses (closed curves) deliberately don't emit this; see #34.
    let mid_pt_3 = arc_pt(mid_a);
    let mv = glam::DVec3::new(mid_pt_3.x, mid_pt_3.y, mid_pt_3.z);
    let tangent = TangentGeom::Circle {
        center: [cwx as f32, cwy as f32, cwz as f32],
        radius: r as f32,
    };

    if arc.thickness.abs() > 1e-10 {
        let t = arc.thickness;
        let (nx, ny, nz) = normal;
        let n = 32usize;
        let ccw_end = if ea >= sa { ea } else { ea + TAU };
        let (start_a, end_a) = (sa, ccw_end);
        let base: Vec<[f64; 3]> = (0..=n)
            .map(|i| {
                let p = arc_pt(start_a + (end_a - start_a) * (i as f64 / n as f64));
                [p.x, p.y, p.z]
            })
            .collect();
        let mut pts: Vec<[f64; 3]> = Vec::with_capacity((n + 1) * 2 + 8);
        pts.extend_from_slice(&base);
        pts.push([f64::NAN; 3]);
        for &[x, y, z] in &base {
            pts.push([x + t * nx, y + t * ny, z + t * nz]);
        }
        pts.push([f64::NAN; 3]);
        let ps = arc_pt(sa);
        pts.push([ps.x, ps.y, ps.z]);
        pts.push([ps.x + t * nx, ps.y + t * ny, ps.z + t * nz]);
        pts.push([f64::NAN; 3]);
        let pe = arc_pt(ea);
        pts.push([pe.x, pe.y, pe.z]);
        pts.push([pe.x + t * nx, pe.y + t * ny, pe.z + t * nz]);
        return TruckEntity {
            pick_tris: extrusion_wall_tris(&base, [t * nx, t * ny, t * nz]),
            object: TruckObject::Lines(pts),
            snap_pts: vec![(cv, SnapHint::Center), (mv, SnapHint::Midpoint)],
            tangent_geoms: vec![tangent],
            key_vertices: vec![],
            fill_tris: vec![],
        };
    }

    let p_start = arc_pt(sa);
    let p_end = arc_pt(ea);
    let p_mid = arc_pt(mid_a);
    let v_start = builder::vertex(p_start);
    let v_end = builder::vertex(p_end);
    let edge = builder::circle_arc(&v_start, &v_end, p_mid);
    TruckEntity {
        pick_tris: Vec::new(),
        object: TruckObject::Curve(edge),
        snap_pts: vec![(cv, SnapHint::Center), (mv, SnapHint::Midpoint)],
        tangent_geoms: vec![tangent],
        key_vertices: vec![],
        fill_tris: vec![],
    }
}

fn angle_span(start: f32, end: f32) -> f32 {
    let mut span = end - start;
    if span < 0.0 {
        span += std::f32::consts::TAU;
    }
    span
}

fn grips(arc: &Arc) -> Vec<GripDef> {
    let ctr = glam::DVec3::new(arc.center.x, arc.center.y, arc.center.z);
    let r = arc.radius;
    let sa = arc.start_angle as f32;
    let ea = arc.end_angle as f32;
    let ma = (sa + angle_span(sa, ea) * 0.5) as f64;
    let (sa, ea) = (arc.start_angle, arc.end_angle);
    vec![
        center_grip(0, ctr),
        square_grip(1, ctr + glam::DVec3::new(r * sa.cos(), r * sa.sin(), 0.0)),
        square_grip(2, ctr + glam::DVec3::new(r * ea.cos(), r * ea.sin(), 0.0)),
        center_grip(3, ctr + glam::DVec3::new(r * ma.cos(), r * ma.sin(), 0.0)),
    ]
}

fn properties(arc: &Arc) -> Vec<PropSection> {
    let r = arc.radius;
    let sa = arc.start_angle;
    let ea = arc.end_angle;
    let sweep = (ea - sa).rem_euclid(TAU);
    let total_angle = sweep.to_degrees();
    let arc_length = r * sweep;
    let area = 0.5 * r * r * sweep;

    let normal = (arc.normal.x, arc.normal.y, arc.normal.z);
    let (ax, ay) = crate::scene::view::transform::ocs_axes(normal);
    let (cwx, cwy, cwz) = crate::scene::view::transform::ocs_point_to_wcs(
        (arc.center.x, arc.center.y, arc.center.z),
        normal,
    );
    let arc_pt = |a: f64| {
        let (c, s) = (a.cos(), a.sin());
        (
            cwx + r * c * ax.0 + r * s * ay.0,
            cwy + r * c * ax.1 + r * s * ay.1,
            cwz + r * c * ax.2 + r * s * ay.2,
        )
    };
    let (sx, sy, sz) = arc_pt(sa);
    let (ex, ey, ez) = arc_pt(ea);

    vec![PropSection {
        title: "Geometry".into(),
        props: vec![
            ro("Start X", "start_x", format!("{sx:.4}")),
            ro("Start Y", "start_y", format!("{sy:.4}")),
            ro("Start Z", "start_z", format!("{sz:.4}")),
            edit("Center X", "center_x", arc.center.x),
            edit("Center Y", "center_y", arc.center.y),
            edit("Center Z", "center_z", arc.center.z),
            ro("End X", "end_x", format!("{ex:.4}")),
            ro("End Y", "end_y", format!("{ey:.4}")),
            ro("End Z", "end_z", format!("{ez:.4}")),
            edit("Radius", "radius", arc.radius),
            edit_angle("Start angle", "start_angle", sa.to_degrees()),
            edit_angle("End angle", "end_angle", ea.to_degrees()),
            ro("Total angle", "total_angle", format!("{total_angle:.2}")),
            ro("Arc length", "arc_length", format!("{arc_length:.4}")),
            ro("Area", "area", format!("{area:.4}")),
            ro("Normal X", "normal_x", format!("{:.4}", arc.normal.x)),
            ro("Normal Y", "normal_y", format!("{:.4}", arc.normal.y)),
            ro("Normal Z", "normal_z", format!("{:.4}", arc.normal.z)),
        ],
    }]
}

fn apply_geom_prop(arc: &mut Arc, field: &str, value: &str) {
    let Some(v) = parse_f64(value) else {
        return;
    };
    match field {
        "center_x" => arc.center.x = v,
        "center_y" => arc.center.y = v,
        "center_z" => arc.center.z = v,
        "radius" if v > 0.0 => arc.radius = v,
        "start_angle" => arc.start_angle = v.to_radians(),
        "end_angle" => arc.end_angle = v.to_radians(),
        _ => {}
    }
}

fn apply_grip(arc: &mut Arc, grip_id: usize, apply: GripApply) {
    match (grip_id, apply) {
        (0, GripApply::Translate(d)) => {
            arc.center.x += d.x;
            arc.center.y += d.y;
            arc.center.z += d.z;
        }
        (0, GripApply::Absolute(p)) => {
            arc.center.x = p.x;
            arc.center.y = p.y;
            arc.center.z = p.z;
        }
        (1, GripApply::Absolute(p)) => {
            let dx = p.x - arc.center.x;
            let dy = p.y - arc.center.y;
            arc.start_angle = dy.atan2(dx);
        }
        (2, GripApply::Absolute(p)) => {
            let dx = p.x - arc.center.x;
            let dy = p.y - arc.center.y;
            arc.end_angle = dy.atan2(dx);
        }
        (3, GripApply::Translate(d)) => {
            let sa = arc.start_angle;
            let ea = arc.end_angle;
            let span = angle_span(sa as f32, ea as f32) as f64;
            let mid_a = sa + span * 0.5;
            let current_mid_x = arc.center.x + arc.radius * mid_a.cos();
            let current_mid_y = arc.center.y + arc.radius * mid_a.sin();
            let new_mid_x = current_mid_x + d.x;
            let new_mid_y = current_mid_y + d.y;
            let dx = new_mid_x - arc.center.x;
            let dy = new_mid_y - arc.center.y;
            let new_r = (dx * dx + dy * dy).sqrt();
            if new_r > 1e-6 {
                arc.radius = new_r;
            }
        }
        _ => {}
    }
}

fn apply_transform(arc: &mut Arc, t: &EntityTransform) {
    crate::scene::view::transform::apply_standard_entity_transform(arc, t, |entity, p1, p2| {
        crate::scene::view::transform::reflect_xy_point(
            &mut entity.center.x,
            &mut entity.center.y,
            p1,
            p2,
        );
        let dx = (p2.x - p1.x) as f64;
        let dy = (p2.y - p1.y) as f64;
        let line_angle = dy.atan2(dx);
        let tmp = entity.start_angle;
        entity.start_angle = 2.0 * line_angle - entity.end_angle;
        entity.end_angle = 2.0 * line_angle - tmp;
    });
}

impl TruckConvertible for Arc {
    fn to_truck(&self, _document: &acadrust::CadDocument) -> Option<TruckEntity> {
        Some(to_truck(self))
    }
}

impl crate::entities::traits::Grippable for Arc {
    fn grips(&self) -> Vec<GripDef> {
        grips(self)
    }
    fn apply_grip(&mut self, grip_id: usize, apply: GripApply) {
        apply_grip(self, grip_id, apply);
    }
    fn grip_menu(&self, grip_id: usize) -> Vec<crate::scene::model::object::GripMenuItem> {
        use crate::scene::model::object::{GripMenuAction, GripMenuItem};
        match grip_id {
            0 => vec![GripMenuItem {
                label: "Stretch",
                action: GripMenuAction::Stretch,
            }],
            3 => vec![
                GripMenuItem {
                    label: "Stretch",
                    action: GripMenuAction::Stretch,
                },
                GripMenuItem {
                    label: "Radius",
                    action: GripMenuAction::Radius,
                },
                GripMenuItem {
                    label: "Arc Length",
                    action: GripMenuAction::ArcLength,
                },
            ],
            _ => vec![
                GripMenuItem {
                    label: "Stretch",
                    action: GripMenuAction::Stretch,
                },
                GripMenuItem {
                    label: "Lengthen",
                    action: GripMenuAction::Lengthen,
                },
            ],
        }
    }
    fn apply_grip_menu(&mut self, _grip_id: usize, _action: crate::scene::model::object::GripMenuAction) {
        // Radius / Arc Length / Lengthen all need a follow-up prompt;
        // the actual edit happens in `apply_grip_menu_value`.
    }

    fn grip_menu_value_prompt(
        &self,
        _grip_id: usize,
        action: crate::scene::model::object::GripMenuAction,
    ) -> Option<&'static str> {
        use crate::scene::model::object::GripMenuAction as A;
        match action {
            A::Radius => Some("New radius"),
            A::ArcLength => Some("New arc length"),
            A::Lengthen => Some("Distance"),
            _ => None,
        }
    }

    fn apply_grip_menu_value(
        &mut self,
        grip_id: usize,
        action: crate::scene::model::object::GripMenuAction,
        value: f64,
    ) {
        use crate::scene::model::object::GripMenuAction as A;
        match action {
            A::Radius if value > 0.0 => self.radius = value,
            A::ArcLength if value > 0.0 && self.radius > 1e-9 => {
                // Hold start_angle, derive new end_angle from arc length
                // = r * Δθ.
                let new_span = value / self.radius;
                self.end_angle = self.start_angle + new_span;
            }
            A::Lengthen => {
                // Extend either end by `value` arc-length units along
                // the arc. Positive `value` lengthens; negative
                // shortens. Grip 1 = start endpoint, grip 2 = end endpoint.
                if self.radius < 1e-9 {
                    return;
                }
                let dtheta = value / self.radius;
                match grip_id {
                    1 => self.start_angle -= dtheta,
                    2 => self.end_angle += dtheta,
                    _ => {}
                }
            }
            _ => {}
        }
    }
}

impl crate::entities::traits::PropertyEditable for Arc {
    fn geometry_properties(&self, _text_style_names: &[String]) -> Vec<PropSection> {
        properties(self)
    }
    fn apply_geom_prop(&mut self, field: &str, value: &str) {
        apply_geom_prop(self, field, value);
    }
}

impl crate::entities::traits::Transformable for Arc {
    fn apply_transform(&mut self, t: &EntityTransform) {
        apply_transform(self, t);
    }
}

impl crate::entities::traits::MassPropsCalc for acadrust::entities::Arc {
    fn mass_props(&self) -> crate::entities::traits::MassProps {
        use std::f64::consts::TAU;
        let r = self.radius;
        let span = {
            let s = (self.end_angle - self.start_angle).rem_euclid(TAU);
            if s < 1e-6 {
                TAU
            } else {
                s
            }
        };
        // Sector area (pie slice)
        let area = 0.5 * r * r * span;
        let arc_len = r * span;
        // Centroid of arc (chord midpoint direction)
        let mid_rad = self.start_angle + span / 2.0;
        crate::entities::traits::MassProps {
            area,
            perimeter: arc_len,
            cx: self.center.x + r * mid_rad.cos(),
            cy: self.center.y + r * mid_rad.sin(),
        }
    }
}
