private bool LeftForwardRManualSegmentLightMode(bool hasLeftSegment, int segmentId, Vector3 screenPos, float lightWidth,
            float pedestrianWidth, float zoom, float lightHeight, ManualSegmentLight segmentDict, bool hoveredSegment,
            bool hasForwardSegment, bool hasRightSegment)
        {
            if (hasLeftSegment)
            {
                // left arrow light
                SetAlpha(segmentId, 3);

                var myRect4 =
                    new Rect(screenPos.x - lightWidth/2 - lightWidth*2 - pedestrianWidth + 5f*zoom,
                        screenPos.y - lightHeight/2, lightWidth, lightHeight);

                switch (segmentDict.LightLeft)
                {
                    case RoadBaseAI.TrafficLightState.Green:
                        GUI.DrawTexture(myRect4, TrafficLightToolTextureResources.GreenLightLeftTexture2D);
                        break;
                    case RoadBaseAI.TrafficLightState.Red:
                        GUI.DrawTexture(myRect4, TrafficLightToolTextureResources.RedLightLeftTexture2D);
                        break;
                }

                if (myRect4.Contains(Event.current.mousePosition))
                {
                    _hoveredButton[0] = segmentId;
                    _hoveredButton[1] = 3;
                    hoveredSegment = true;

                    if (Input.GetMouseButtonDown(0) && !_uiClickedSegment)
                    {
                        _uiClickedSegment = true;
                        segmentDict.ChangeLightLeft();
                    }
                }
            }

            // forward-right arrow light
            SetAlpha(segmentId, 4);

            var myRect5 =
                new Rect(screenPos.x - lightWidth/2 - lightWidth - pedestrianWidth + 5f*zoom,
                    screenPos.y - lightHeight/2, lightWidth, lightHeight);

            if (hasForwardSegment && hasRightSegment)
            {
                switch (segmentDict.LightMain)
                {
                    case RoadBaseAI.TrafficLightState.Green:
                        GUI.DrawTexture(myRect5, TrafficLightToolTextureResources.GreenLightForwardRightTexture2D);
                        break;
                    case RoadBaseAI.TrafficLightState.Red:
                        GUI.DrawTexture(myRect5, TrafficLightToolTextureResources.RedLightForwardRightTexture2D);
                        break;
                }
            }
            else if (!hasRightSegment)
            {
                switch (segmentDict.LightMain)
                {
                    case RoadBaseAI.TrafficLightState.Green:
                        GUI.DrawTexture(myRect5, TrafficLightToolTextureResources.GreenLightStraightTexture2D);
                        break;
                    case RoadBaseAI.TrafficLightState.Red:
                        GUI.DrawTexture(myRect5, TrafficLightToolTextureResources.RedLightStraightTexture2D);
                        break;
                }
            }
            else
            {
                switch (segmentDict.LightMain)
                {
                    case RoadBaseAI.TrafficLightState.Green:
                        GUI.DrawTexture(myRect5, TrafficLightToolTextureResources.GreenLightRightTexture2D);
                        break;
                    case RoadBaseAI.TrafficLightState.Red:
                        GUI.DrawTexture(myRect5, TrafficLightToolTextureResources.RedLightRightTexture2D);
                        break;
                }
            }

            if (!myRect5.Contains(Event.current.mousePosition))
                return hoveredSegment;
            _hoveredButton[0] = segmentId;
            _hoveredButton[1] = 4;

            if (!Input.GetMouseButtonDown(0) || _uiClickedSegment)
                return true;
            _uiClickedSegment = true;
            segmentDict.ChangeLightMain();
            return true;
        }
        private bool IsPedestrianLightHovered(Rect myRect3, int segmentId, bool hoveredSegment, ManualSegmentLight segmentDict)
        {
            if (!myRect3.Contains(Event.current.mousePosition))
                return hoveredSegment;

            _hoveredButton[0] = segmentId;
            _hoveredButton[1] = 2;

            if (!Input.GetMouseButtonDown(0) || _uiClickedSegment)
                return true;
            _uiClickedSegment = true;

            if (!segmentDict.PedestrianEnabled)
            {
                segmentDict.ManualPedestrian();
            }
            else
            {
                segmentDict.ChangeLightPedestrian();
            }
            return true;
        }
        private bool LeftArrowLightMode(int segmentId, float lightWidth, bool hasRightSegment,
            bool hasForwardSegment, Vector3 screenPos, float pedestrianWidth, float zoom, float lightHeight,
            ManualSegmentLight segmentDict, bool hoveredSegment)
        {
            SetAlpha(segmentId, 3);

            var offsetLight = lightWidth;

            if (hasRightSegment)
                offsetLight += lightWidth;

            if (hasForwardSegment)
                offsetLight += lightWidth;

            var myRect4 =
                new Rect(screenPos.x - lightWidth/2 - offsetLight - pedestrianWidth + 5f*zoom,
                    screenPos.y - lightHeight/2, lightWidth, lightHeight);

            switch (segmentDict.LightLeft)
            {
                case RoadBaseAI.TrafficLightState.Green:
                    GUI.DrawTexture(myRect4, TrafficLightToolTextureResources.GreenLightLeftTexture2D);
                    break;
                case RoadBaseAI.TrafficLightState.Red:
                    GUI.DrawTexture(myRect4, TrafficLightToolTextureResources.RedLightLeftTexture2D);
                    break;
            }

            if (!myRect4.Contains(Event.current.mousePosition))
                return hoveredSegment;
            _hoveredButton[0] = segmentId;
            _hoveredButton[1] = 3;

            if (!Input.GetMouseButtonDown(0) || _uiClickedSegment)
                return true;

            _uiClickedSegment = true;
            segmentDict.ChangeLightLeft();

            if (!hasForwardSegment)
            {
                segmentDict.ChangeLightMain();
            }
            return true;
        }
        private bool RightArrowLightMode(int segmentId, Vector3 screenPos, float lightWidth,
            float pedestrianWidth, float zoom, float lightHeight, ManualSegmentLight segmentDict, bool hoveredSegment)
        {
            SetAlpha(segmentId, 5);

            var myRect5 =
                new Rect(screenPos.x - lightWidth/2 - lightWidth - pedestrianWidth + 5f*zoom,
                    screenPos.y - lightHeight/2, lightWidth, lightHeight);

            switch (segmentDict.LightRight)
            {
                case RoadBaseAI.TrafficLightState.Green:
                    GUI.DrawTexture(myRect5, TrafficLightToolTextureResources.GreenLightRightTexture2D);
                    break;
                case RoadBaseAI.TrafficLightState.Red:
                    GUI.DrawTexture(myRect5, TrafficLightToolTextureResources.RedLightRightTexture2D);
                    break;
            }

            if (!myRect5.Contains(Event.current.mousePosition))
                return hoveredSegment;

            _hoveredButton[0] = segmentId;
            _hoveredButton[1] = 5;

            if (!Input.GetMouseButtonDown(0) || _uiClickedSegment)
                return true;

            _uiClickedSegment = true;
            segmentDict.ChangeLightRight();
            return true;
        }
        private bool GetHoveredSegment(Rect myRect1, int segmentId, bool hoveredSegment, ManualSegmentLight segmentDict)
        {
            if (!myRect1.Contains(Event.current.mousePosition))
                return hoveredSegment;

            Debug.Log("mouse in myRect1");
            _hoveredButton[0] = segmentId;
            _hoveredButton[1] = -1;

            if (!Input.GetMouseButtonDown(0) || _uiClickedSegment)
                return true;
            _uiClickedSegment = true;
            segmentDict.ChangeMode();
            return true;
        }
        private bool RenderCounter(int segmentId, Vector3 screenPos, float modeWidth, float modeHeight, float zoom,
            ManualSegmentLight segmentDict, bool hoveredSegment)
        {
            SetAlpha(segmentId, 0);

            var myRectCounter = new Rect(screenPos.x - modeWidth/2, screenPos.y - modeHeight/2 - 6f*zoom, modeWidth, modeHeight);

            GUI.DrawTexture(myRectCounter, TrafficLightToolTextureResources.LightCounterTexture2D);

            var counterSize = 20f*zoom;

            var counter = segmentDict.LastChange;

            var myRectCounterNum = new Rect(screenPos.x - counterSize + 15f*zoom + (counter >= 10 ? -5*zoom : 0f),
                screenPos.y - counterSize + 11f*zoom, counterSize, counterSize);

            _counterStyle.fontSize = (int) (18f*zoom);
            _counterStyle.normal.textColor = new Color(1f, 1f, 1f);

            GUI.Label(myRectCounterNum, counter.ToString(), _counterStyle);

            if (!myRectCounter.Contains(Event.current.mousePosition))
                return hoveredSegment;
            _hoveredButton[0] = segmentId;
            _hoveredButton[1] = 0;
            return true;
        }
        private bool RenderManualPedestrianLightSwitch(float zoom, int segmentId, Vector3 screenPos, float lightWidth,
            ManualSegmentLight segmentDict, bool hoveredSegment)
        {
            var guiColor = GUI.color;
            var manualPedestrianWidth = 36f*zoom;
            var manualPedestrianHeight = 35f*zoom;

            guiColor.a = _hoveredButton[0] == segmentId && (_hoveredButton[1] == 1 || _hoveredButton[1] == 2) ? 0.92f : 0.45f;

            GUI.color = guiColor;

            var myRect2 = new Rect(screenPos.x - manualPedestrianWidth/2 - lightWidth + 5f*zoom,
                screenPos.y - manualPedestrianHeight/2 - 9f*zoom, manualPedestrianWidth, manualPedestrianHeight);

            GUI.DrawTexture(myRect2, segmentDict.PedestrianEnabled ? TrafficLightToolTextureResources.PedestrianModeManualTexture2D : TrafficLightToolTextureResources.PedestrianModeAutomaticTexture2D);

            if (!myRect2.Contains(Event.current.mousePosition))
                return hoveredSegment;

            _hoveredButton[0] = segmentId;
            _hoveredButton[1] = 1;

            if (!Input.GetMouseButtonDown(0) || _uiClickedSegment)
                return true;

            _uiClickedSegment = true;
            segmentDict.ManualPedestrian();
            return true;
        }
        internal void handleNewSegments()
        {
            if (Steps.Count <= 0)
            {
                return;
            }

            NetNode node = Singleton <NetManager> .instance.m_nodes.m_buffer[nodeId];

            for (int s = 0; s < 8; ++s)
            {
                ushort segmentId = node.GetSegment(s);
                if (segmentId <= 0)
                {
                    continue;
                }
                NetSegment segment = Singleton <NetManager> .instance.m_segments.m_buffer[segmentId];

                List <ushort> invalidSegmentIds = new List <ushort>();
                bool          isNewSegment      = true;

                foreach (KeyValuePair <ushort, ManualSegmentLight> e in Steps[0].segmentLightStates)
                {
                    var fromSegmentId = e.Key;
                    var segLightState = e.Value;

                    if (fromSegmentId == segmentId)
                    {
                        isNewSegment = false;
                    }

                    if (!TrafficPriority.IsPrioritySegment(nodeId, fromSegmentId))
                    {
                        invalidSegmentIds.Add(fromSegmentId);
                    }
                }

                if (isNewSegment)
                {
                    Log.Message($"New segment detected: {segmentId} @ {nodeId}");
                    // segment was created
                    TrafficLightsManual.AddLiveSegmentLight(nodeId, segmentId);
                    TrafficPriority.AddPrioritySegment(nodeId, segmentId, PrioritySegment.PriorityType.None);

                    if (invalidSegmentIds.Count > 0)
                    {
                        var oldSegmentId = invalidSegmentIds[0];
                        Log.Message($"Replacing old segment {oldSegmentId} @ {nodeId} with new segment {segmentId}");

                        // replace the old segment with the newly created one
                        for (int i = 0; i < Steps.Count; ++i)
                        {
                            ManualSegmentLight segmentLight = Steps[i].segmentLightStates[oldSegmentId];
                            Steps[i].segmentIds.Remove(oldSegmentId);
                            Steps[i].segmentLightStates.Remove(oldSegmentId);
                            segmentLight.SegmentId = segmentId;
                            Steps[i].segmentLightStates.Add(segmentId, segmentLight);
                            Steps[i].segmentIds.Add(segmentId);
                            Steps[i].rebuildSegmentIds();
                        }
                    }
                    else
                    {
                        Log.Message($"Adding new segment {segmentId} to node {nodeId}");

                        // create a new manual light
                        for (int i = 0; i < Steps.Count; ++i)
                        {
                            Steps[i].addSegment(segmentId);
                            Steps[i].rebuildSegmentIds();
                        }
                    }
                }
            }
        }
		internal void ChangeLightMode(ushort segmentId, ManualSegmentLight.Mode mode) {
			segmentLightStates[segmentId].CurrentMode = mode;
		}
		internal void ChangeLightMode(ushort segmentId, ManualSegmentLight.Mode mode) {
			foreach (TimedTrafficStep step in Steps) {
				step.ChangeLightMode(segmentId, mode);
			}
		}
		private bool RightForwardLSegmentLightMode(int segmentId, Vector3 screenPos, float lightWidth, float pedestrianWidth,
			float zoom, float lightHeight, bool hasForwardSegment, bool hasLeftSegment, ManualSegmentLight segmentDict,
			bool hasRightSegment, bool hoveredSegment) {
			SetAlpha(segmentId, 3);

			var myRect4 = new Rect(screenPos.x - lightWidth / 2 - lightWidth * 2 - pedestrianWidth + 5f * zoom,
				screenPos.y - lightHeight / 2, lightWidth, lightHeight);

			if (hasForwardSegment && hasLeftSegment) {
				switch (segmentDict.LightLeft) {
					case RoadBaseAI.TrafficLightState.Green:
						GUI.DrawTexture(myRect4, TrafficLightToolTextureResources.GreenLightForwardLeftTexture2D);
						break;
					case RoadBaseAI.TrafficLightState.Red:
						GUI.DrawTexture(myRect4, TrafficLightToolTextureResources.RedLightForwardLeftTexture2D);
						break;
				}
			} else if (!hasLeftSegment) {
				if (!hasRightSegment) {
					myRect4 = new Rect(screenPos.x - lightWidth / 2 - lightWidth - pedestrianWidth + 5f * zoom,
						screenPos.y - lightHeight / 2, lightWidth, lightHeight);
				}

				switch (segmentDict.LightMain) {
					case RoadBaseAI.TrafficLightState.Green:
						GUI.DrawTexture(myRect4, TrafficLightToolTextureResources.GreenLightStraightTexture2D);
						break;
					case RoadBaseAI.TrafficLightState.Red:
						GUI.DrawTexture(myRect4, TrafficLightToolTextureResources.RedLightStraightTexture2D);
						break;
				}
			} else {
				if (!hasRightSegment) {
					myRect4 = new Rect(screenPos.x - lightWidth / 2 - lightWidth - pedestrianWidth + 5f * zoom,
						screenPos.y - lightHeight / 2, lightWidth, lightHeight);
				}

				switch (segmentDict.LightMain) {
					case RoadBaseAI.TrafficLightState.Green:
						GUI.DrawTexture(myRect4, TrafficLightToolTextureResources.GreenLightLeftTexture2D);
						break;
					case RoadBaseAI.TrafficLightState.Red:
						GUI.DrawTexture(myRect4, TrafficLightToolTextureResources.RedLightLeftTexture2D);
						break;
				}
			}


			if (myRect4.Contains(Event.current.mousePosition)) {
				_hoveredButton[0] = segmentId;
				_hoveredButton[1] = 3;
				hoveredSegment = true;

				if (checkClicked()) {
					segmentDict.ChangeLightMain();
				}
			}

			var guiColor = GUI.color;
			// right arrow light
			if (hasRightSegment)
				guiColor.a = _hoveredButton[0] == segmentId && _hoveredButton[1] == 4 ? 0.92f : 0.6f;

			GUI.color = guiColor;

			var myRect5 =
				new Rect(screenPos.x - lightWidth / 2 - lightWidth - pedestrianWidth + 5f * zoom,
					screenPos.y - lightHeight / 2, lightWidth, lightHeight);

			switch (segmentDict.LightRight) {
				case RoadBaseAI.TrafficLightState.Green:
					GUI.DrawTexture(myRect5, TrafficLightToolTextureResources.GreenLightRightTexture2D);
					break;
				case RoadBaseAI.TrafficLightState.Red:
					GUI.DrawTexture(myRect5, TrafficLightToolTextureResources.RedLightRightTexture2D);
					break;
			}


			if (!myRect5.Contains(Event.current.mousePosition))
				return hoveredSegment;

			_hoveredButton[0] = segmentId;
			_hoveredButton[1] = 4;

			if (!Input.GetMouseButtonDown(0) || mouseClickProcessed)
				return true;
			mouseClickProcessed = true;
			segmentDict.ChangeLightRight();
			return true;
		}
		private bool GetHoveredSegment(Rect myRect1, int segmentId, bool hoveredSegment, ManualSegmentLight segmentDict) {
			if (!myRect1.Contains(Event.current.mousePosition))
				return hoveredSegment;

			//Log.Message("mouse in myRect1");
			_hoveredButton[0] = segmentId;
			_hoveredButton[1] = -1;

			if (!Input.GetMouseButtonDown(0) || mouseClickProcessed)
				return true;
			mouseClickProcessed = true;
			segmentDict.ChangeMode();
			return true;
		}