using System;
using System.Linq;
using System.Windows;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;
using NavisworksTransport.Core;

namespace NavisworksTransport.Commands
{
    [PluginAttribute("ReadTransformTest", "YourDeveloperID", DisplayName = "读取Transform测试")]
    [AddInPluginAttribute(AddInLocation.AddIn)]
    public class ReadTransformTestCommand : AddInPlugin
    {
        public override int Execute(params string[] parameters)
        {
            try
            {
                var doc = Autodesk.Navisworks.Api.Application.ActiveDocument;
                var selection = doc.CurrentSelection.SelectedItems;

                if (selection.Count == 0)
                {
                    MessageBox.Show("请先选择一个对象！", "提示");
                    return 0;
                }

                var item = selection.First();
                
                // 读取Transform
                var transform = item.Transform;
                var components = transform.Factor();
                var bbox = item.BoundingBox();
                var center = bbox.Center;

                var info = $"=== Transform信息 ===\n\n";
                info += $"对象: {item.DisplayName}\n\n";
                info += $"包围盒中心:\n";
                info += $"  X = {center.X:F3}\n";
                info += $"  Y = {center.Y:F3}\n";
                info += $"  Z = {center.Z:F3}\n\n";
                info += $"Transform.Translation:\n";
                info += $"  X = {components.Translation.X:F3}\n";
                info += $"  Y = {components.Translation.Y:F3}\n";
                info += $"  Z = {components.Translation.Z:F3}\n\n";
                info += $"Transform.Scale:\n";
                info += $"  X = {components.Scale.X:F3}\n";
                info += $"  Y = {components.Scale.Y:F3}\n";
                info += $"  Z = {components.Scale.Z:F3}\n\n";
                info += $"Transform.Rotation:\n";
                info += $"  Axis = ({components.Rotation.Axis.X:F3}, {components.Rotation.Axis.Y:F3}, {components.Rotation.Axis.Z:F3})\n";
                info += $"  Angle = {components.Rotation.Angle:F6} rad = {components.Rotation.Angle * 180 / Math.PI:F2}°\n";

                LogManager.Info(info);
                MessageBox.Show(info, "Transform信息", MessageBoxButton.OK);

                return 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误: {ex.Message}", "错误");
                LogManager.Error($"读取Transform失败: {ex.Message}\n{ex.StackTrace}");
                return 1;
            }
        }
    }
}
