using System;
using System.Threading.Tasks;
using CommonUsage.Protocols.VDA5050.Objects;
using SimpleLite.CADTools;
using SimpleLite.Props;
using SimpleLite.UI;
using SimpleCore;
using SimpleCore.Library;
using SimpleCore.PropType;

namespace StandardScene.CarTypes
{
    /// <summary>
    /// Intermediate data structure for linking VDA5050 nodes & edges with MDCS sites & tracks.
    /// </summary>
    internal class VDA5050Segment
    {
        public VDA5050Segment(sequenceItem item, SegState state)
        {
            Item = item;
            State = state;
            if (IsNode()) FinishToken = new TaskCompletionSource<int>();
        }

        public enum SegState
        {
            Waiting,
            BaseSending,
            BaseAcknowledged,
            HorizonSending,
            HorizonAcknowledged,
            Executed,
        }

        public bool IsNode()
        {
            return Item is node || Item is nodeState;
        }

        public float GetTrackLength()
        {
            var track = VDA5050Helper.FindTrackByVDA5050Id(((edge)Item).edgeId);
            if (track is UITrack lineTrack)
            {
                var sa = SimpleLib.GetSite(track.siteA);
                var sb = SimpleLib.GetSite(track.siteB);
                return (float)LessMath.dist(sa.x, sa.y, sb.x, sb.y);
            }
            else if (track is UICircularArcTrack arcTrack)
            {
                throw new Exception("not implemented yet");
            }
            else if (track is UIBezierTrack bezierTrack)
            {
                var sa = SimpleLib.GetSite(track.siteA);
                var sb = SimpleLib.GetSite(track.siteB);
                return (float)LessMath.dist(sa.x, sa.y, sb.x, sb.y);

            }
            else if(track is UINurbsTrack)
            {
                var sa = SimpleLib.GetSite(track.siteA);
                var sb = SimpleLib.GetSite(track.siteB);
                return (float)LessMath.dist(sa.x, sa.y, sb.x, sb.y);
            }
            else throw new Exception("not implemented yet");
        }

        public sequenceItem Item = null;

        public SegState State;

        public bool FinishTriggered = false;

        public TaskCompletionSource<int> FinishToken = null;
    }
}
