use runmat_builtins::Value;
use runmat_macros::runtime_builtin;

use super::properties::{resolve_plot_handle, set_properties};
use crate::builtins::plotting::type_resolvers::set_type;

#[runtime_builtin(
    name = "set",
    category = "plotting",
    summary = "Set properties on plotting handles.",
    keywords = "set,plotting,handle,property",
    suppress_auto_output = true,
    type_resolver(set_type),
    builtin_path = "crate::builtins::plotting::set"
)]
pub fn set_builtin(args: Vec<Value>) -> crate::BuiltinResult<String> {
    if args.len() < 3 {
        return Err(crate::builtins::plotting::plotting_error(
            "set",
            "set: expected a plotting handle followed by property/value pairs",
        ));
    }
    let handle = resolve_plot_handle(&args[0], "set")?;
    set_properties(handle, &args[1..], "set")?;
    Ok("ok".to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::builtins::plotting::get::get_builtin;
    use crate::builtins::plotting::legend::legend_builtin;
    use crate::builtins::plotting::tests::{ensure_plot_test_env, lock_plot_registry};
    use crate::builtins::plotting::title::title_builtin;
    use crate::builtins::plotting::{clear_figure, clone_figure, reset_hold_state_for_run};
    use runmat_builtins::Value;
    use runmat_plot::plots::{Figure, LinePlot};

    fn setup() -> crate::builtins::plotting::state::PlotTestLockGuard {
        let guard = lock_plot_registry();
        ensure_plot_test_env();
        reset_hold_state_for_run();
        let _ = clear_figure(None);
        guard
    }

    #[test]
    fn set_updates_text_handle_properties() {
        let _guard = setup();
        let h = title_builtin(vec![Value::String("Signal".into())]).unwrap();
        set_builtin(vec![
            Value::Num(h),
            Value::String("String".into()),
            Value::String("Updated".into()),
            Value::String("Visible".into()),
            Value::Bool(false),
        ])
        .unwrap();

        let title = get_builtin(vec![Value::Num(h), Value::String("String".into())]).unwrap();
        assert_eq!(title, Value::String("Updated".into()));
        let visible = get_builtin(vec![Value::Num(h), Value::String("Visible".into())]).unwrap();
        assert_eq!(visible, Value::Bool(false));
    }

    #[test]
    fn set_updates_extended_text_and_legend_properties() {
        let _guard = setup();
        let h = title_builtin(vec![Value::String("Signal".into())]).unwrap();
        set_builtin(vec![
            Value::Num(h),
            Value::String("FontWeight".into()),
            Value::String("bold".into()),
            Value::String("FontAngle".into()),
            Value::String("italic".into()),
        ])
        .unwrap();
        assert_eq!(
            get_builtin(vec![Value::Num(h), Value::String("FontWeight".into())]).unwrap(),
            Value::String("bold".into())
        );
        assert_eq!(
            get_builtin(vec![Value::Num(h), Value::String("FontAngle".into())]).unwrap(),
            Value::String("italic".into())
        );

        let mut figure = Figure::new();
        figure.add_line_plot(
            LinePlot::new(vec![0.0, 1.0], vec![1.0, 2.0])
                .unwrap()
                .with_label("A"),
        );
        let figure = crate::builtins::plotting::import_figure(figure);
        let ax = crate::builtins::plotting::state::encode_axes_handle(figure, 0);
        let legend = legend_builtin(vec![Value::Num(ax)]).unwrap();
        set_builtin(vec![
            Value::Num(legend),
            Value::String("FontWeight".into()),
            Value::String("bold".into()),
            Value::String("FontAngle".into()),
            Value::String("italic".into()),
            Value::String("Interpreter".into()),
            Value::String("none".into()),
            Value::String("Box".into()),
            Value::Bool(false),
            Value::String("Orientation".into()),
            Value::String("horizontal".into()),
        ])
        .unwrap();
        let fig = clone_figure(figure).unwrap();
        let meta = fig.axes_metadata(0).unwrap();
        assert_eq!(meta.legend_style.font_weight.as_deref(), Some("bold"));
        assert_eq!(meta.legend_style.font_angle.as_deref(), Some("italic"));
        assert_eq!(meta.legend_style.interpreter.as_deref(), Some("none"));
        assert_eq!(meta.legend_style.box_visible, Some(false));
        assert_eq!(meta.legend_style.orientation.as_deref(), Some("horizontal"));
    }

    #[test]
    fn set_updates_legend_handle_properties() {
        let _guard = setup();
        let mut figure = Figure::new();
        figure.add_line_plot(
            LinePlot::new(vec![0.0, 1.0], vec![1.0, 2.0])
                .unwrap()
                .with_label("A"),
        );
        let figure = crate::builtins::plotting::import_figure(figure);
        let ax = crate::builtins::plotting::state::encode_axes_handle(figure, 0);
        let legend = legend_builtin(vec![Value::Num(ax)]).unwrap();

        set_builtin(vec![
            Value::Num(legend),
            Value::String("Location".into()),
            Value::String("southwest".into()),
            Value::String("Visible".into()),
            Value::Bool(false),
        ])
        .unwrap();

        let fig = clone_figure(figure).unwrap();
        let meta = fig.axes_metadata(0).unwrap();
        assert_eq!(meta.legend_style.location.as_deref(), Some("southwest"));
        assert!(!meta.legend_enabled);
    }

    #[test]
    fn set_updates_axes_handle_alias_properties() {
        let _guard = setup();
        let h = title_builtin(vec![Value::String("Signal".into())]).unwrap();
        let (figure, axes, _) =
            crate::builtins::plotting::state::decode_plot_object_handle(h).unwrap();
        let ax = crate::builtins::plotting::state::encode_axes_handle(figure, axes);

        set_builtin(vec![
            Value::Num(ax),
            Value::String("Title".into()),
            Value::String("Updated Title".into()),
            Value::String("LegendVisible".into()),
            Value::Bool(false),
        ])
        .unwrap();

        let fig = clone_figure(figure).unwrap();
        let meta = fig.axes_metadata(axes).unwrap();
        assert_eq!(meta.title.as_deref(), Some("Updated Title"));
        assert!(!meta.legend_enabled);
    }

    #[test]
    fn set_updates_axes_local_properties() {
        let _guard = setup();
        let ax = crate::builtins::plotting::subplot::subplot_builtin(
            Value::Num(1.0),
            Value::Num(2.0),
            Value::Num(2.0),
        )
        .unwrap();
        set_builtin(vec![
            Value::Num(ax),
            Value::String("YLim".into()),
            Value::Tensor(runmat_builtins::Tensor {
                rows: 1,
                cols: 2,
                shape: vec![1, 2],
                data: vec![2.0, 8.0],
                dtype: runmat_builtins::NumericDType::F64,
            }),
            Value::String("Colorbar".into()),
            Value::Bool(true),
            Value::String("Colormap".into()),
            Value::String("hot".into()),
        ])
        .unwrap();

        let fig = clone_figure(crate::builtins::plotting::current_figure_handle()).unwrap();
        let meta = fig.axes_metadata(1).unwrap();
        assert_eq!(meta.y_limits, Some((2.0, 8.0)));
        assert!(meta.colorbar_enabled);
        assert_eq!(format!("{:?}", meta.colormap), "Hot");
    }

    #[test]
    fn set_updates_figure_current_axes() {
        let _guard = setup();
        let fig =
            crate::builtins::plotting::figure::figure_builtin(vec![Value::Num(4321.0)]).unwrap();
        let ax = crate::builtins::plotting::subplot::subplot_builtin(
            Value::Num(1.0),
            Value::Num(2.0),
            Value::Num(2.0),
        )
        .unwrap();
        set_builtin(vec![
            Value::Num(fig),
            Value::String("CurrentAxes".into()),
            Value::Num(ax),
        ])
        .unwrap();
        let current =
            get_builtin(vec![Value::Num(fig), Value::String("CurrentAxes".into())]).unwrap();
        assert_eq!(current, Value::Num(ax));
    }

    #[test]
    fn set_updates_figure_sgtitle() {
        let _guard = setup();
        let fig =
            crate::builtins::plotting::figure::figure_builtin(vec![Value::Num(7777.0)]).unwrap();
        set_builtin(vec![
            Value::Num(fig),
            Value::String("SGTitle".into()),
            Value::String("Overview".into()),
        ])
        .unwrap();
        let figure = clone_figure(crate::builtins::plotting::state::FigureHandle::from(7777))
            .expect("figure should exist");
        assert_eq!(figure.sg_title.as_deref(), Some("Overview"));
    }

    #[test]
    fn set_updates_stem_properties() {
        let _guard = setup();
        let handle = crate::builtins::plotting::stem::stem_builtin(vec![Value::Tensor(
            runmat_builtins::Tensor {
                rows: 2,
                cols: 1,
                shape: vec![2],
                data: vec![1.0, 2.0],
                dtype: runmat_builtins::NumericDType::F64,
            },
        )])
        .unwrap();
        set_builtin(vec![
            Value::Num(handle),
            Value::String("BaseValue".into()),
            Value::Num(-2.0),
            Value::String("Filled".into()),
            Value::Bool(true),
        ])
        .unwrap();
        let base =
            get_builtin(vec![Value::Num(handle), Value::String("BaseValue".into())]).unwrap();
        let filled = get_builtin(vec![Value::Num(handle), Value::String("Filled".into())]).unwrap();
        assert_eq!(base, Value::Num(-2.0));
        assert_eq!(filled, Value::Bool(true));
    }

    #[test]
    fn set_rejects_invalid_property_assignments() {
        let _guard = setup();
        let h = title_builtin(vec![Value::String("Signal".into())]).unwrap();
        let err = set_builtin(vec![
            Value::Num(h),
            Value::String("Bogus".into()),
            Value::Num(1.0),
        ])
        .unwrap_err();
        assert!(err.message.contains("unsupported property"));
    }
}
