/***************************************************************************
*                                                                          *
*  Copyright (C) Golaem S.A.  All Rights Reserved.                         *
*                                                                          *
***************************************************************************/

#pragma once

#include "glmSDKAPI.h"
#include "glmMemory.h"
#include "glmVector3.h"
#include "glmSegment3.h"
#include "glmQuaternion.h"
#include "glmArray.h"
#include "glmPODArray.h"
#include "glmSingleton.h"
#include "glmMutex.h"
#include "glmScopedLock.h"
#include "glmSimuEntityHandle.h"
#include "glmTrafficPositionInRoadData.h"

namespace glm
{
	class TrafficEntityComponent;
	class TrafficContext;

	//-------------------------------------------------------------------------
	// this data is frozen at perception time
	class TrafficPerceptionData
	{
	public:
		GLM_CLASS_STAT_ID( glm::MemoryStatIds::Simulation);

		struct LaneOccupancy
		{
			float _laneRightNoise;
			float _minAbscissa; 
			float _maxAbscissa; 
			int32_t _roadIndex;
			uint8_t _laneDirection;
			uint8_t _laneIndexFromCenter;
			bool _switching;
		};

		TrafficPerceptionData();
		~TrafficPerceptionData();

		const LaneOccupancy* getLaneOccupancy(const PositionInRoadData& posInRoad, float carHalfWidth, float laneWidth) const;
		//bool getLaneOccupancy(const PositionInRoadData& posInRoad, int fromLaneIndexFromCenter, int toLaneIndexFromCenter, float& minAbscissa, float& maxAbscissa) const;
		const LaneOccupancy* getLaneOccupancyFromIndices(const PositionInRoadData& posInRoad, int fromLaneIndexFromCenter, int toLaneIndexFromCenter) const;
		void addLaneOccupancy(PositionInRoadData& inRoad, float carHalfWidth, float laneWidth, float& minAbscissa, float& maxAbscissa); // extend a road occupancy
		void clearLaneOccupancy(); // clear all road occupancies (when updating the component)

		void addCrossingVehicleAbsicssa(int crossingIndex, float crossingLocalAbscissa ); // extend a road occupancy

	public:
		// data for perception, freezed at perception time, ___based on the first box center (trailer)___
		float _speed;
		float _speedRatio; // speed modulation, for example when seeing pedestrians around or crossing. default to 1

		struct VehicleAbscissaInCrossing
		{
			int _crossingIndex;
			float _crossingLocalAbscissa; 
		};

		PODArray< VehicleAbscissaInCrossing > _crossingLocalVehicleAbscissa; // match the trajectories present in crossing requests = trafficElement.startsegmentDistanceToTrailer
		
	protected:

		glm::PODArray< LaneOccupancy > _laneOccupancy; // list of roads + laneDirection/laneIndex occupied by the component, with currently occupied abscissa. updated in monothread to be used with perception
	};
}
