use acadrust::{entities::Line, Entity};
use truck_modeling::{builder, Point3};

use crate::command::EntityTransform;
use crate::entities::common::{
    center_grip, 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::TangentGeom;

fn to_truck(line: &Line) -> TruckEntity {
    // LINE endpoints are stored in WCS — unlike the planar OCS entities
    // (ARC/CIRCLE/LWPOLYLINE/TEXT), the extrusion normal on a LINE only
    // orients its thickness sweep. Remapping the endpoints through the
    // arbitrary-axis OCS mirrored every line carried over from a MIRROR
    // (normal 0,0,-1) to the wrong side of the drawing.
    let normal = (line.normal.x, line.normal.y, line.normal.z);
    let (sx, sy, sz) = (line.start.x, line.start.y, line.start.z);
    let (ex, ey, ez) = (line.end.x, line.end.y, line.end.z);
    let p0 = Point3::new(sx, sy, sz);
    let p1 = Point3::new(ex, ey, ez);
    let kv: Vec<[f64; 3]> = vec![[p0.x, p0.y, p0.z], [p1.x, p1.y, p1.z]];
    let tangent = TangentGeom::Line {
        p1: [kv[0][0] as f32, kv[0][1] as f32, kv[0][2] as f32],
        p2: [kv[1][0] as f32, kv[1][1] as f32, kv[1][2] as f32],
    };

    if line.thickness.abs() > 1e-10 {
        let t = line.thickness;
        let (nx, ny, nz) = normal;
        let p0t = [sx + t * nx, sy + t * ny, sz + t * nz];
        let p1t = [ex + t * nx, ey + t * ny, ez + t * nz];
        let pts: Vec<[f64; 3]> = vec![
            kv[0],
            kv[1],
            [f64::NAN; 3],
            p0t,
            p1t,
            [f64::NAN; 3],
            kv[0],
            p0t,
            [f64::NAN; 3],
            kv[1],
            p1t,
        ];
        return TruckEntity {
            pick_tris: extrusion_wall_tris(&kv, [t * nx, t * ny, t * nz]),
            object: TruckObject::Lines(pts),
            snap_pts: vec![],
            tangent_geoms: vec![tangent],
            key_vertices: kv,
            fill_tris: vec![],
        };
    }

    let v0 = builder::vertex(p0);
    let v1 = builder::vertex(p1);
    let edge = builder::line(&v0, &v1);
    TruckEntity {
        pick_tris: Vec::new(),
        object: TruckObject::Curve(edge),
        snap_pts: vec![],
        tangent_geoms: vec![tangent],
        key_vertices: kv,
        fill_tris: vec![],
    }
}

fn grips(line: &Line) -> Vec<GripDef> {
    let s = glam::DVec3::new(line.start.x, line.start.y, line.start.z);
    let e = glam::DVec3::new(line.end.x, line.end.y, line.end.z);
    let m = (s + e) * 0.5;
    vec![square_grip(0, s), square_grip(1, e), center_grip(2, m)]
}

fn properties(line: &Line) -> Vec<PropSection> {
    let dx = line.end.x - line.start.x;
    let dy = line.end.y - line.start.y;
    let dz = line.end.z - line.start.z;
    let angle = dy.atan2(dx).to_degrees().rem_euclid(360.0);
    vec![PropSection {
        title: "Geometry".into(),
        props: vec![
            edit("Start X", "start_x", line.start.x),
            edit("Start Y", "start_y", line.start.y),
            edit("Start Z", "start_z", line.start.z),
            edit("End X", "end_x", line.end.x),
            edit("End Y", "end_y", line.end.y),
            edit("End Z", "end_z", line.end.z),
            ro("Delta X", "delta_x", format!("{dx:.4}")),
            ro("Delta Y", "delta_y", format!("{dy:.4}")),
            ro("Delta Z", "delta_z", format!("{dz:.4}")),
            ro("Length", "length", format!("{:.4}", line.length())),
            ro("Angle", "angle", format!("{angle:.2}")),
        ],
    }]
}

fn apply_geom_prop(line: &mut Line, field: &str, value: &str) {
    let Some(v) = parse_f64(value) else {
        return;
    };
    match field {
        "start_x" => line.start.x = v,
        "start_y" => line.start.y = v,
        "start_z" => line.start.z = v,
        "end_x" => line.end.x = v,
        "end_y" => line.end.y = v,
        "end_z" => line.end.z = v,
        _ => {}
    }
}

fn apply_grip(line: &mut Line, grip_id: usize, apply: GripApply) {
    match (grip_id, apply) {
        (0, GripApply::Absolute(p)) => {
            line.start.x = p.x as f64;
            line.start.y = p.y as f64;
            line.start.z = p.z as f64;
        }
        (1, GripApply::Absolute(p)) => {
            line.end.x = p.x as f64;
            line.end.y = p.y as f64;
            line.end.z = p.z as f64;
        }
        (2, GripApply::Translate(d)) => {
            line.start.x += d.x as f64;
            line.start.y += d.y as f64;
            line.start.z += d.z as f64;
            line.end.x += d.x as f64;
            line.end.y += d.y as f64;
            line.end.z += d.z as f64;
        }
        _ => {}
    }
}

fn apply_transform(line: &mut Line, t: &EntityTransform) {
    match t {
        EntityTransform::Translate(d) => {
            line.translate(acadrust::types::Vector3::new(
                d.x as f64, d.y as f64, d.z as f64,
            ));
        }
        EntityTransform::Rotate { center, angle_rad } => {
            crate::scene::view::transform::apply_standard_transform(line, *center, *angle_rad);
        }
        EntityTransform::Scale { center, factor } => {
            crate::scene::view::transform::apply_standard_scale(line, *center, *factor);
        }
        EntityTransform::Mirror { p1, p2 } => {
            crate::scene::view::transform::mirror_xy_line(line, *p1, *p2);
        }
    }
}

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

impl crate::entities::traits::Grippable for Line {
    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};
        if grip_id == 2 {
            vec![GripMenuItem {
                label: "Stretch",
                action: GripMenuAction::Stretch,
            }]
        } else {
            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) {
        // Lengthen needs a follow-up distance — handled by
        // `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::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;
        if !matches!(action, A::Lengthen) {
            return;
        }
        let dx = self.end.x - self.start.x;
        let dy = self.end.y - self.start.y;
        let dz = self.end.z - self.start.z;
        let len = (dx * dx + dy * dy + dz * dz).sqrt();
        if len < 1e-12 {
            return;
        }
        let (ux, uy, uz) = (dx / len, dy / len, dz / len);
        match grip_id {
            0 => {
                // Move start endpoint backward along the line by `value`
                // (positive = lengthen; negative = shorten).
                self.start.x -= ux * value;
                self.start.y -= uy * value;
                self.start.z -= uz * value;
            }
            1 => {
                self.end.x += ux * value;
                self.end.y += uy * value;
                self.end.z += uz * value;
            }
            _ => {}
        }
    }
}

impl crate::entities::traits::PropertyEditable for Line {
    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 Line {
    fn apply_transform(&mut self, t: &EntityTransform) {
        apply_transform(self, t);
    }
}

impl crate::entities::traits::MassPropsCalc for acadrust::entities::Line {
    fn mass_props(&self) -> crate::entities::traits::MassProps {
        let dx = self.end.x - self.start.x;
        let dy = self.end.y - self.start.y;
        let len = (dx * dx + dy * dy).sqrt();
        crate::entities::traits::MassProps {
            area: 0.0,
            perimeter: len,
            cx: (self.start.x + self.end.x) / 2.0,
            cy: (self.start.y + self.end.y) / 2.0,
        }
    }
}
