using System.Collections.Generic;
using System.Numerics;
using Autodesk.Navisworks.Api;

namespace NavisworksTransport.UI.WPF.ViewModels
{
    internal enum RailAssemblyWorkflowMode
    {
        None,
        CreateNewRailPath,
        EditSelectedRail
    }

    internal sealed class RailAssemblyWorkflowContext
    {
        private const string DefaultTerminalObjectName = "未选择";
        private const string DefaultTerminalObjectInfo = "请选择终点处已安装箱体";
        private const string DefaultAssemblyStartPointText = "未选择";

        private readonly double _defaultReferenceRodLengthInMeters;
        private readonly double _defaultReferenceRodDiameterInMeters;
        private readonly double _defaultAnchorVerticalOffsetInMeters;
        private readonly double _defaultSphereCenterX;
        private readonly double _defaultSphereCenterY;
        private readonly double _defaultSphereCenterZ;

        public RailAssemblyWorkflowContext(
            double defaultReferenceRodLengthInMeters,
            double defaultReferenceRodDiameterInMeters,
            double defaultAnchorVerticalOffsetInMeters,
            double defaultSphereCenterX,
            double defaultSphereCenterY,
            double defaultSphereCenterZ)
        {
            _defaultReferenceRodLengthInMeters = defaultReferenceRodLengthInMeters;
            _defaultReferenceRodDiameterInMeters = defaultReferenceRodDiameterInMeters;
            _defaultAnchorVerticalOffsetInMeters = defaultAnchorVerticalOffsetInMeters;
            _defaultSphereCenterX = defaultSphereCenterX;
            _defaultSphereCenterY = defaultSphereCenterY;
            _defaultSphereCenterZ = defaultSphereCenterZ;

            TerminalObjectName = DefaultTerminalObjectName;
            TerminalObjectInfo = DefaultTerminalObjectInfo;
            ReferenceRodLengthInMeters = _defaultReferenceRodLengthInMeters;
            ReferenceRodDiameterInMeters = _defaultReferenceRodDiameterInMeters;
            AnchorVerticalOffsetInMeters = _defaultAnchorVerticalOffsetInMeters;
            SphereCenterX = _defaultSphereCenterX;
            SphereCenterY = _defaultSphereCenterY;
            SphereCenterZ = _defaultSphereCenterZ;
            StartPointText = DefaultAssemblyStartPointText;
            MountMode = RailMountMode.UnderRail;
            InstallationSeedPoints = new List<Point3D>();
            EndFaceSeedPoints = new List<Point3D>();
        }

        public RailAssemblyWorkflowMode WorkflowMode { get; set; }

        public string TerminalObjectName { get; set; }

        public string TerminalObjectInfo { get; set; }

        public double ReferenceRodLengthInMeters { get; set; }

        public double ReferenceRodDiameterInMeters { get; set; }

        public double AnchorVerticalOffsetInMeters { get; set; }

        public double SphereCenterX { get; set; }

        public double SphereCenterY { get; set; }

        public double SphereCenterZ { get; set; }

        public string StartPointText { get; set; }

        public RailMountMode MountMode { get; set; }

        public bool HasTerminalObject { get; set; }

        public bool IsSelectingStartPoint { get; set; }

        public bool IsSelectingEndFacePoints { get; set; }

        public bool IsSelectingInstallationPoint { get; set; }

        public bool HasEndFaceAnalysis { get; set; }

        public bool HasInstallationReference { get; set; }

        public ModelItem TerminalObject { get; set; }

        public Point3D StartPoint { get; set; }

        public Point3D EndFaceCenterPoint { get; set; }

        public Vector3 EndFaceNormal { get; set; }

        public Point3D InstallationPickPoint { get; set; }

        public Point3D InstallationBaseAnchorPoint { get; set; }

        public Point3D InstallationAnchorPoint { get; set; }

        public Vector3 InstallationPlaneNormal { get; set; }

        public Vector3 InstallationPlaneSpanDirection { get; set; }

        public double InstallationOffsetDistanceInMeters { get; set; }

        public string InstallationReferenceRouteId { get; set; }

        public List<Point3D> InstallationSeedPoints { get; }

        public List<Point3D> EndFaceSeedPoints { get; }

        public void ResetEndFaceAnalysis()
        {
            HasEndFaceAnalysis = false;
            EndFaceCenterPoint = null;
            EndFaceNormal = default(Vector3);
            EndFaceSeedPoints.Clear();
        }

        public void ResetInstallationReference()
        {
            HasInstallationReference = false;
            InstallationPickPoint = null;
            InstallationBaseAnchorPoint = null;
            InstallationAnchorPoint = null;
            InstallationPlaneNormal = default(Vector3);
            InstallationPlaneSpanDirection = default(Vector3);
            InstallationOffsetDistanceInMeters = 0.0;
            InstallationReferenceRouteId = null;
            InstallationSeedPoints.Clear();
            AnchorVerticalOffsetInMeters = _defaultAnchorVerticalOffsetInMeters;
        }

        public void ResetSession()
        {
            WorkflowMode = RailAssemblyWorkflowMode.None;
            HasTerminalObject = false;
            IsSelectingStartPoint = false;
            IsSelectingEndFacePoints = false;
            IsSelectingInstallationPoint = false;
            TerminalObject = null;
            StartPoint = Point3D.Origin;
            TerminalObjectName = DefaultTerminalObjectName;
            TerminalObjectInfo = DefaultTerminalObjectInfo;
            StartPointText = DefaultAssemblyStartPointText;
            ResetEndFaceAnalysis();
            ResetInstallationReference();
        }
    }
}
