ecsDataTable(UKCoreTruckModel* TruckModel) const
{
	if (TruckModel)
	{
		static const FString Context(TEXT("UKTruckModelDataSubsystem_FillFromChassisSpecsDataTable"));
		if (auto ChassisSpecs = ChassisSpecsDT->FindRow<FChassisSpecsRow>(TruckModel->GetChassisID(), Context, false))
		{
			FTruckChassisInfo& info = TruckModel->GetChassisInfo();
			info.Weight = ChassisSpecs->Weight;
			info.FullBodyHP = ChassisSpecs->BodyHP;
			info.CurrentBodyHP = info.FullBodyHP;
			info.FullCabinHP = ChassisSpecs->CabinHP;
			info.CurrentCabinHP = info.FullCabinHP;
			info.FullFrameHP = ChassisSpecs->FrameHP;
			info.CurrentFrameHP = info.FullFrameHP;

			return true;
		}
	}
	return false;
}

bool UKTruckModelDataSubsystem::FillFromSuspensionSpecsDataTable(UKCoreTruckModel* TruckModel) const
{
	if (TruckModel)
	{
		static const FString Context(TEXT("UKTruckModelDataSubsystem_FillFromSuspensionSpecsDataTable"));
		if (auto SuspensionSpecs = SuspensionSpecsDT->FindRow<FSuspensionSpecsRow>(TruckModel->GetSuspensionID(), Context, false))
		{
			FTruckSuspensionInfo& info = TruckModel->GetSuspensionInfo();
			info.Weight = SuspensionSpecs->Weight;
			info.Stiffness = SuspensionSpecs->Stiffness;
			info.Travel = SuspensionSpecs->Travel;
			info.TireGrip = SuspensionSpecs->TireGrip;
			info.TireOffroadness = SuspensionSpecs->TireOffroadness;
			info.BrakesEfficiency = SuspensionSpecs->BrakesEfficiency;
			info.BrakesControllability = SuspensionSpecs->BrakesControllability;
			info.BrakesHeatResistance = SuspensionSpecs->BrakesHeatResistance;
			info.WheelTurnSpeed = SuspensionSpecs->WheelTurnSpeed;

			info.FullWheelHP = SuspensionSpecs->WheelHP;
			info.FullBrakesHP = SuspensionSpecs->BrakesHP;
			info.CurrentBrakesHP = info.FullBrakesHP;
			info.FullSteeringHP = SuspensionSpecs->SteeringHP;
			info.CurrentSteeringHP = info.FullSteeringHP;

			return true;
		}
	}
	return false;
}

bool UKTruckModelDataSubsystem::FillFromTransmissionSpecsDataTable(UKCoreTruckModel* TruckModel) const
{
	if (TruckModel)
	{
		static const FString Context(TEXT("UKTruckModelDataSubsystem_FillFromTransmissionSpecsDataTable"));
		if (auto TransmissionSpecs = TransmissionSpecsDT->FindRow<FTransmissionSpecsRow>(TruckModel->GetTransmissionID(), Context, false))
		{
			FTruckTransmissionInfo& info = TruckModel->GetTransmissionInfo();
			info.Weight = TransmissionSpecs->Weight;
			info.CapacitySlots = TransmissionSpecs->CapacitySlots;
			info.GearboxAssembly = TransmissionSpecs->GearboxAssembly;
			info.Durability = TransmissionSpecs->Durability;
			info.ForwardGears = TransmissionSpecs->ForwardGears;
			info.Axle1 = TransmissionSpecs->Axle1;
			info.TurnAngle1 = TransmissionSpecs->TurnAngle1;
			info.Axle2 = TransmissionSpecs->Axle2;
			info.TurnAngle2 = TransmissionSpecs->TurnAngle2;
			info.Axle3 = TransmissionSpecs->Axle3;
			info.TurnAngle3 = TransmissionSpecs->TurnAngle3;
			info.Axle4 = TransmissionSpecs->Axle4;
			info.TurnAngle4 = TransmissionSpecs->TurnAngle4;

			info.FullGearboxHP = TransmissionSpecs->GearboxHP;
			info.CurrentGearboxHP = info.FullGearboxHP;

			info.FullTransmissionHP = TransmissionSpecs->TransmissionHP;
			info.CurrentTransmissionHP = info.FullTransmissionHP;

			info.FullAxleHP = TransmissionSpecs->AxleHP;

			return true;
		}
	}
	return false;
}

bool UKTruckModelDataSubsystem::FillFromTruckDataAsset(const FName& TruckModelID, UKCoreTruckModel* TruckModel) const
{
	if (TruckModelDescs && TruckModel)
	{
		if (auto assetEntry = TruckModelDescs->Find(TruckModelID))
		{
			if (UKTruckModelDesc* asset = *assetEntry)
			{
				TruckModel->SetTruckModelID(TruckModelID);
				TruckModel->SetName(asset->Name);
				TruckModel->SetAxleConfig(asset->AxleConfig);

				FTruckModelPartMeshes& PartMeshes = TruckModel->GetPartMeshes();
				PartMeshes.ChassisMesh = asset->Meshes.ChassisMesh;
				PartMeshes.SuspensionAnimBP = asset->Meshes.SuspensionAnimBP;
				PartMeshes.BodyMesh = asset->Meshes.BodyMesh;

				PartMeshes.WheelFrontMesh = asset->Meshes.WheelFrontMesh;
				PartMeshes.WheelRearMesh = asset->Meshes.WheelRearMesh;

				PartMeshes.BodyLightTopRearMiddle = asset->Meshes.BodyLightTopRearMiddle;
				PartMeshes.BodyLightTopRearLeft = asset->Meshes.BodyLightTopRearLeft;
				PartMeshes.BodyLightTopRearRight = asset->Meshes.BodyLightTopRearRight;
				PartMeshes.BodyLightBottomRearLeft = asset->Meshes.BodyLightBottomRearLeft;
				PartMeshes.BodyLightBottomRearRight = asset->Meshes.BodyLightBottomRearRight;

				PartMeshes.TrunkMesh = asset->Meshes.TrunkMesh;
				PartMeshes.BodyDoorRightMesh = asset->Meshes.BodyDoorRightMesh;
				PartMeshes.BodyDoorLeftMesh = asset->Meshes.BodyDoorLeftMesh;
				PartMeshes.SpareWheelMesh = asset->Meshes.SpareWheelMesh;

				PartMeshes.ShellClothMesh = asset->Meshes.ShellClothMesh;

				PartMeshes.CabinMesh = asset->Meshes.CabinMesh;

				PartMeshes.InteriorMesh = asset->Meshes.InteriorMesh;
				PartMeshes.SteeringWheelMesh = asset->Meshes.SteeringWheelMesh;
				PartMeshes.CabinDoorLeftMesh = asset->Meshes.CabinDoorLeftMesh;
				PartMeshes.CabinDoorRightMesh = asset->Meshes.CabinDoorRightMesh;
				PartMeshes.CabinHoodMesh = asset->Meshes.CabinHoodMesh;
				PartMeshes.CabinTurnLightLeftMesh = asset->Meshes.CabinTurnLightLeftMesh;
				PartMeshes.CabinTurnLightRightMesh = asset->Meshes.CabinTurnLightRightMesh;

				PartMeshes.CabinLightBottomFrontLeft = asset->Meshes.FrontLightsMeshes.CabinLightBottomFrontLeft;
				PartMeshes.CabinLightBottomFrontRight = asset->Meshes.FrontLightsMeshes.CabinLightBottomFrontRight;
				PartMeshes.CabinLightShieldBottomFrontLeft = asset->Meshes.FrontLightsMeshes.CabinLightShieldBottomFrontLeft;
				PartMeshes.CabinLightShieldBottomFrontRight = asset->Meshes.FrontLightsMeshes.CabinLightShieldBottomFrontRight;

				PartMeshes.CabinLightCenterFrontMiddle = asset->Meshes.FrontLightsMeshes.CabinLightCenterFrontMiddle;
				PartMeshes.CabinLightBottomFrontRightClass = asset->Meshes.FrontLightsMeshes.CabinLightBottomFrontRightClass;

				TruckModel->SetBackCameraDesc(asset->BackCameraDesc);
				TruckModel->SetFrontCameraDesc(asset->FrontCameraDesc);

				TruckModel->SetCabinLights(asset->CabinLights);
				TruckModel->SetBodyLights(asset->BodyLights);
				TruckModel->SetChassisLights(asset->ChassisLights);

				return true;
			}
		}
	}
	return false;
}

bool UKTruckModelDataSubsystem::FillFromEngineDataAsset(UKCoreTruckModel* TruckModel) const
{
	if (EngineModelDescs && TruckModel)
	{
		if (auto assetEntry = EngineModelDescs->Find(TruckModel->GetEngineID()))
		{
			if (UKEngineModelDesc* asset = *assetEntry)
			{
				FTruckEngineInfo& info = TruckModel->GetEngineInfo();
				info.Name = asset->Name;
				info.Mesh = asset->Mesh;
				info.TorqueCurve = asset->TorqueCurve;
				info.MaxTorque = asset->MaxTorque;
				info.MaxRPM = asset->MaxRPM;
				info.EngineIdleRPM = asset->EngineIdleRPM;
				info.EngineBrakeEffect = asset->EngineBrakeEffect;
				info.EngineRevUpMOI = asset->EngineRevUpMOI;
				info.EngineRevDownRate = asset->EngineRevDownRate;

				return true;
			}
		}
	}
	return false;
}

bool UKTruckModelDataSubsystem::FillFromChassisDataAsset(UKCoreTruckModel* TruckModel) const
{
	if (ChassisModelDescs && TruckModel)
	{
		if (auto assetEntry = ChassisModelDescs->Find(TruckModel->GetChassisID()))
		{
			if (UKChassisModelDesc* asset = *assetEntry)
			{
				FTruckChassisInfo& info = TruckModel->GetChassisInfo();
				info.Name = asset->Name;
				info.CenterOfMassOverride = asset->CenterOfMassOverride;

				info.ChassisWidth = asset->ChassisWidth;
				info.ChassisHeight = asset->ChassisHeight;
				info.DragCoefficient = asset->DragCoefficient;
				info.DownforceCoefficient = asset->DownforceCoefficient;
				info.SleepThreshold = asset->SleepThreshold;
				info.SleepSlopeLimit = asset->SleepSlopeLimit;

				return true;
			}
		}
	}
	return false;
}

bool UKTruckModelDataSubsystem::FillFromSuspensionDataAsset(UKCoreTruckModel* TruckModel) const
{
	if (SuspensionModelDescs && TruckModel)
	{
		if (auto assetEntry = SuspensionModelDescs->Find(TruckModel->GetSuspensionID()))
		{
			if (UKSuspensionModelDesc* asset = *assetEntry)
			{
				FTruckSuspensionInfo& info = TruckModel->GetSuspensionInfo();
				info.Name = asset->Name;
				info.SteeringCurve = asset->SteeringCurve;

				return true;
			}
		}
	}
	return false;
}

bool UKTruckModelDataSubsystem::FillFromTransmissionDataAsset(UKCoreTruckModel* TruckModel) const
{
	if (TransmissionModelDescs && TruckModel)
	{
		if (auto assetEntry = TransmissionModelDescs->Find(TruckModel->GetTransmissionID()))
		{
			if (UKTransmissionModelDesc* asset = *assetEntry)
			{
				FTruckTransmissionInfo& info = TruckModel->GetTransmissionInfo();
				info.Name = asset->Name;
				info.PhysWheelAxle1Class = asset->PhysWheelAxle1Class;
				info.PhysWheelAxle2Class = asset->PhysWheelAxle2Class;
				info.PhysWheelAxle3Class = asset->PhysWheelAxle3Class;
				info.PhysWheelAxle4Class = asset->PhysWheelAxle4Class;

				info.FinalRatio = asset->FinalRatio;
				info.ForwardGearRatios = asset->ForwardGearRatios;
				info.RearGearRatios = asset->RearGearRatios;
				info.ChangeUpRPM = asset->ChangeUpRPM;
				info.ChangeDownRPM = asset->ChangeDownRPM;
				info.GearChangeTime = asset->GearChangeTime;
				info.TransmissionEfficiency = asset->TransmissionEfficiency;

				return true;
			}
		}
	}
	return false;
}

void UKTruckModelDataSubsystem::TryOverrideByCSV()
{
	if (UDataTable* table = OverrideTableByCSV(TEXT("DT_TruckSpecs.csv"), FTruckSpecsRow::StaticStruct()))
	{
		TruckSpecsDT = table;
	}

	if (UDataTable* table = OverrideTableByCSV(TEXT("DT_EngineSpecs.csv"), FEngineSpecsRow::StaticStruct()))
	{
		EngineSpecsDT = table;
	}

	if (UDataTable* table = OverrideTableByCSV(TEXT("DT_ChassisSpecs.csv"), FChassisSpecsRow::StaticStruct()))
	{
		ChassisSpecsDT = table;
	}

	if (UDataTable* table = OverrideTableByCSV(TEXT("DT_SuspensionSpecs.csv"), FSuspensionSpecsRow::StaticStruct()))
	{
		SuspensionSpecsDT = table;
	}

	if (UDataTable* table = OverrideTableByCSV(TEXT("DT_TransmissionSpecs.csv"), FTransmissionSpecsRow::StaticStruct()))
	{
		TransmissionSpecsDT = table;
	}
}

UDataTable* UKTruckModelDataSubsystem::OverrideTableByCSV(const FString& TableName, UScriptStruct* Struct)
{
	FString CSVPath = FPaths::ProjectContentDir();
	CSVPath += TEXT("CSV/");
	CSVPath += TableName;
	if (Struct && FPaths::FileExists(CSVPath))
	{
		UE_LOG(LogUKTruckModelDataSubsystem, Log, TEXT("Table CSV file \'%s\' is found, overriding packaged datatable data"), *TableName);

		FString CSVContent;
		FFileHelper::LoadFileToString(CSVContent, *CSVPath);
		UDataTable* Table = NewObject<UDataTable>(this);
		Table->RowStruct = Struct;
		Table->CreateTableFromCSVString(CSVContent);
		return Table;
	}

	return nullptr;
}
