using Autodesk.Navisworks.Api;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NavisworksTransport.UI.WPF.ViewModels;
using System.Numerics;

namespace NavisworksTransport.UnitTests.CoordinateSystem
{
    [TestClass]
    public class RailAssemblyWorkflowContextTests
    {
        [TestMethod]
        public void ResetEndFaceAnalysis_ClearsAnalysisStateAndSeedPoints()
        {
            var context = CreateContext();
            context.HasEndFaceAnalysis = true;
            context.EndFaceCenterPoint = new Point3D(1.0, 2.0, 3.0);
            context.EndFaceNormal = new Vector3(1f, 0f, 0f);
            context.EndFaceSeedPoints.Add(new Point3D(4.0, 5.0, 6.0));

            context.ResetEndFaceAnalysis();

            Assert.IsFalse(context.HasEndFaceAnalysis);
            Assert.IsNull(context.EndFaceCenterPoint);
            Assert.AreEqual(default(Vector3), context.EndFaceNormal);
            Assert.AreEqual(0, context.EndFaceSeedPoints.Count);
        }

        [TestMethod]
        public void ResetInstallationReference_RestoresDefaultOffsetAndClearsReferenceState()
        {
            var context = CreateContext();
            context.HasInstallationReference = true;
            context.InstallationPickPoint = new Point3D(1.0, 2.0, 3.0);
            context.InstallationBaseAnchorPoint = new Point3D(4.0, 5.0, 6.0);
            context.InstallationAnchorPoint = new Point3D(7.0, 8.0, 9.0);
            context.InstallationPlaneNormal = new Vector3(0f, 1f, 0f);
            context.InstallationPlaneSpanDirection = new Vector3(1f, 0f, 0f);
            context.InstallationOffsetDistanceInMeters = 2.5;
            context.InstallationReferenceRouteId = "route-1";
            context.InstallationSeedPoints.Add(new Point3D(9.0, 8.0, 7.0));
            context.AnchorVerticalOffsetInMeters = 3.5;

            context.ResetInstallationReference();

            Assert.IsFalse(context.HasInstallationReference);
            Assert.IsNull(context.InstallationPickPoint);
            Assert.IsNull(context.InstallationBaseAnchorPoint);
            Assert.IsNull(context.InstallationAnchorPoint);
            Assert.AreEqual(default(Vector3), context.InstallationPlaneNormal);
            Assert.AreEqual(default(Vector3), context.InstallationPlaneSpanDirection);
            Assert.AreEqual(0.0, context.InstallationOffsetDistanceInMeters, 1e-9);
            Assert.IsNull(context.InstallationReferenceRouteId);
            Assert.AreEqual(0, context.InstallationSeedPoints.Count);
            Assert.AreEqual(0.25, context.AnchorVerticalOffsetInMeters, 1e-9);
        }

        [TestMethod]
        public void ResetSession_ClearsTransientStateButPreservesConfiguration()
        {
            var context = CreateContext();
            context.WorkflowMode = RailAssemblyWorkflowMode.EditSelectedRail;
            context.TerminalObjectName = "箱体A";
            context.TerminalObjectInfo = "info";
            context.ReferenceRodLengthInMeters = 12.0;
            context.ReferenceRodDiameterInMeters = 0.4;
            context.SphereCenterX = 10.0;
            context.SphereCenterY = 11.0;
            context.SphereCenterZ = 12.0;
            context.MountMode = RailMountMode.OverRail;
            context.HasTerminalObject = true;
            context.IsSelectingStartPoint = true;
            context.IsSelectingEndFacePoints = true;
            context.IsSelectingInstallationPoint = true;
            context.StartPoint = new Point3D(1.0, 2.0, 3.0);
            context.StartPointText = "(1,2,3)";
            context.HasEndFaceAnalysis = true;
            context.EndFaceCenterPoint = new Point3D(4.0, 5.0, 6.0);
            context.HasInstallationReference = true;
            context.InstallationReferenceRouteId = "route-2";
            context.AnchorVerticalOffsetInMeters = 1.5;
            context.EndFaceSeedPoints.Add(new Point3D(1.0, 0.0, 0.0));
            context.InstallationSeedPoints.Add(new Point3D(0.0, 1.0, 0.0));

            context.ResetSession();

            Assert.AreEqual(RailAssemblyWorkflowMode.None, context.WorkflowMode);
            Assert.IsFalse(context.HasTerminalObject);
            Assert.IsFalse(context.IsSelectingStartPoint);
            Assert.IsFalse(context.IsSelectingEndFacePoints);
            Assert.IsFalse(context.IsSelectingInstallationPoint);
            Assert.IsNull(context.TerminalObject);
            Assert.IsNotNull(context.StartPoint);
            Assert.AreEqual("未选择", context.TerminalObjectName);
            Assert.AreEqual("请选择终点处已安装箱体", context.TerminalObjectInfo);
            Assert.AreEqual("未选择", context.StartPointText);
            Assert.IsFalse(context.HasEndFaceAnalysis);
            Assert.IsFalse(context.HasInstallationReference);
            Assert.AreEqual(0.25, context.AnchorVerticalOffsetInMeters, 1e-9);
            Assert.AreEqual(12.0, context.ReferenceRodLengthInMeters, 1e-9);
            Assert.AreEqual(0.4, context.ReferenceRodDiameterInMeters, 1e-9);
            Assert.AreEqual(10.0, context.SphereCenterX, 1e-9);
            Assert.AreEqual(11.0, context.SphereCenterY, 1e-9);
            Assert.AreEqual(12.0, context.SphereCenterZ, 1e-9);
            Assert.AreEqual(RailMountMode.OverRail, context.MountMode);
        }

        private static RailAssemblyWorkflowContext CreateContext()
        {
            return new RailAssemblyWorkflowContext(
                defaultReferenceRodLengthInMeters: 20.0,
                defaultReferenceRodDiameterInMeters: 0.3,
                defaultAnchorVerticalOffsetInMeters: 0.25,
                defaultSphereCenterX: 0.0,
                defaultSphereCenterY: 0.0,
                defaultSphereCenterZ: 0.0);
        }
    }
}
