/*
 * Copyright (C) 2012-2026 MotionSystems
 *
 * This file is part of ForceSeatMI SDK.
 *
 * www.motionsystems.eu
 *
 */

#include "ForceSeatMIPhysXVehicle_Controller.h"
#include "ForceSeatMIPhysXVehicle_ControllerCore.h"
#include "IForceSeatMI.h"

#include "WheeledVehicleMovementComponent.h"
#include "WheeledVehicle.h"

UForceSeatMIPhysXVehicle_Controller::UForceSeatMIPhysXVehicle_Controller()
	: m_controller(nullptr)
	, m_source(nullptr)
{
	PrimaryComponentTick.bCanEverTick = true;
	PrimaryComponentTick.bStartWithTickEnabled = true;
	PrimaryComponentTick.bAllowTickOnDedicatedServer = true;
}

UForceSeatMIPhysXVehicle_Controller::~UForceSeatMIPhysXVehicle_Controller()
{
	delete m_controller;
	m_controller = nullptr;
}

void UForceSeatMIPhysXVehicle_Controller::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	if (m_controller)
	{
		m_controller->Update(DeltaTime);
	}
}

void UForceSeatMIPhysXVehicle_Controller::SetVehicleObject(AWheeledVehicle* Input)
{
	if (!m_controller || m_source != Input)
	{
		m_source = Input;

		delete m_controller;

		m_controller = new ForceSeatMIPhysXVehicle_ControllerCore(Input);
		m_controller->Begin();
	}
}

void UForceSeatMIPhysXVehicle_Controller::SetAdditionalProperties(float SideSlip, float RoadHarshness, float G)
{
	if (m_controller)
	{
		m_controller->SetAdditionalProperties(SideSlip, RoadHarshness, G);
	}
}

void UForceSeatMIPhysXVehicle_Controller::SetUserAux(float aux1, float aux2, float aux3, float aux4, float aux5, float aux6, float aux7, float aux8)
{
	if (m_controller)
	{
		m_controller->SetUserAux(aux1, aux2, aux3, aux4, aux5, aux6, aux7, aux8);
	}
}

void UForceSeatMIPhysXVehicle_Controller::Pause(bool paused)
{
	if (m_controller)
	{
		m_controller->Pause(paused);
	}
}

void UForceSeatMIPhysXVehicle_Controller::BeginPlay()
{
	Super::BeginPlay();

	if (m_controller)
	{
		m_controller->Begin();
	}
}

void UForceSeatMIPhysXVehicle_Controller::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	Super::EndPlay(EndPlayReason);

	if (m_controller)
	{
		m_controller->End();
	}
}
