AddSegmentLights(ushort segmentId,
                         bool startNode,
                         RoadBaseAI.TrafficLightState lightState =
                         RoadBaseAI.TrafficLightState.Red)
        {
#if DEBUG
            Log._Trace($"CustomTrafficLights.AddSegmentLights: Adding segment light: {segmentId} @ startNode={startNode}");
#endif
            if (!Services.NetService.IsSegmentValid(segmentId))
            {
                return(null);
            }

            CustomSegment customSegment = customSegments_[segmentId];

            if (customSegment == null)
            {
                customSegment = new CustomSegment();
                customSegments_[segmentId] = customSegment;
            }
            else
            {
                ICustomSegmentLights existingLights =
                    startNode ? customSegment.StartNodeLights : customSegment.EndNodeLights;

                if (existingLights != null)
                {
                    existingLights.SetLights(lightState);
                    return(existingLights);
                }
            }

            if (startNode)
            {
                customSegment.StartNodeLights = new CustomSegmentLights(
                    this,
                    segmentId,
                    startNode,
                    false);

                customSegment.StartNodeLights.SetLights(lightState);
                return(customSegment.StartNodeLights);
            }
            else
            {
                customSegment.EndNodeLights = new CustomSegmentLights(
                    this,
                    segmentId,
                    startNode,
                    false);

                customSegment.EndNodeLights.SetLights(lightState);
                return(customSegment.EndNodeLights);
            }
        }
        /// <summary>
        /// Adds custom traffic lights at the specified node and segment.
        /// Light stats are set to the given light state, or to "Red" if no light state is given.
        /// </summary>
        /// <param name="segmentId"></param>
        /// <param name="startNode"></param>
        /// <param name="lightState">(optional) light state to set</param>
        public ICustomSegmentLights AddSegmentLights(ushort segmentId, bool startNode, RoadBaseAI.TrafficLightState lightState = RoadBaseAI.TrafficLightState.Red)
        {
#if DEBUG
            Log._Debug($"CustomTrafficLights.AddSegmentLights: Adding segment light: {segmentId} @ startNode={startNode}");
#endif

            SegmentGeometry segGeometry = SegmentGeometry.Get(segmentId);
            if (segGeometry == null)
            {
                Log.Error($"CustomTrafficLightsManager.AddSegmentLights: Segment {segmentId} is invalid.");
                return(null);
            }

            SegmentEndGeometry endGeometry = segGeometry.GetEnd(startNode);
            if (endGeometry == null)
            {
                Log.Error($"CustomTrafficLightsManager.AddSegmentLights: Segment {segmentId} is not connected to a node @ start {startNode}");
                return(null);
            }

            CustomSegment customSegment = CustomSegments[segmentId];
            if (customSegment == null)
            {
                customSegment             = new CustomSegment();
                CustomSegments[segmentId] = customSegment;
            }
            else
            {
                ICustomSegmentLights existingLights = startNode ? customSegment.StartNodeLights : customSegment.EndNodeLights;

                if (existingLights != null)
                {
                    existingLights.SetLights(lightState);
                    return(existingLights);
                }
            }

            SubscribeToSegmentGeometry(segmentId);
            if (startNode)
            {
                customSegment.StartNodeLights = new CustomSegmentLights(this, segmentId, startNode, false);
                customSegment.StartNodeLights.SetLights(lightState);
                return(customSegment.StartNodeLights);
            }
            else
            {
                customSegment.EndNodeLights = new CustomSegmentLights(this, segmentId, startNode, false);
                customSegment.EndNodeLights.SetLights(lightState);
                return(customSegment.EndNodeLights);
            }
        }