//MIT License
//
//Copyright(c) 2021 HaiLiang Feng
//
//QQ : 632865163
//Blog : https ://www.cnblogs.com/LynnVon/
//
//    Permission is hereby granted, free of charge, to any person obtaining a copy
//    of this softwareand associated documentation files(the "Software"), to deal
//    in the Software without restriction, including without limitation the rights
//    to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
//    copies of the Software, and to permit persons to whom the Software is
//    furnished to do so, subject to the following conditions :
//
//The above copyright noticeand this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

#include "FlightPropertySystem.h"
#include "Engine.h"
#include "IFlightSubsystemInterface.h"
#include "FlightControlSystem.h"
#include "FlightBlueprintFunctionLibrary.h"

UFlightPropertySystem::UFlightPropertySystem(const FObjectInitializer& ObjectInitializer) :Super(ObjectInitializer)
{

   SetTickSystemEnable(true);
   TickRate = 0.01;
   SetSystemName(FName("FlightPropertySystem"));
   ConsumeAmountPerSec = 0;
   bAutoStart = true;

}

void UFlightPropertySystem::StartUp()
{
   Super::StartUp();
   if (GetOwner())
   {
      transformFlightProperty.FlightName = FName(GetOwner()->GetName());
      transformFlightProperty.ID = GetOwner()->GetUniqueID();
      transformFlightProperty.TimeStamp = UFlightBlueprintFunctionLibrary::GetTimeStamp();
      transformFlightProperty.ActorType = EActorType::Flight;
   }

}

void UFlightPropertySystem::ShutDown()
{
   Super::ShutDown();
}

void UFlightPropertySystem::TickSystem()
{
   Super::TickSystem();

#if WITH_EDITORONLY_DATA
   DebugPrint();
#endif

   transformFlightProperty.Position = GetOwner()->GetActorLocation();
   transformFlightProperty.Velocity = GetOwner()->GetVelocity();

}


void UFlightPropertySystem::SetCurrentWeaponMode(EWeaponMode mode)
{
   CurrentWeaponMode = mode;
}

EWeaponMode UFlightPropertySystem::GetCurrentWeaponMode() const
{
   return CurrentWeaponMode;
}

void UFlightPropertySystem::move_engine_rpm(float val)
{
   engine_rpm += val;
   FS_CLAMP_RANGE<float>(engine_rpm, 0, engine_max_rpm);
}

void UFlightPropertySystem::set_engine_rpm(float val)
{
   engine_rpm = val;
   FS_CLAMP_RANGE<float>(engine_rpm, 0, engine_max_rpm);

}

void UFlightPropertySystem::set_engine_max_rpm(float val)
{
   engine_max_rpm = val;
}

void UFlightPropertySystem::move_engine_tep(float val)
{
   engine_temp += val;
   FS_CLAMP_RANGE<float>(engine_temp, 0, engine_max_temp);
}

void UFlightPropertySystem::set_engine_tep(float val)
{
   engine_temp = val;
   FS_CLAMP_RANGE<float>(engine_temp, 0, engine_max_temp);

}

void UFlightPropertySystem::set_engine_max_tep(float val)
{
   engine_max_temp = val;
}

void UFlightPropertySystem::move_engine_pressure(float val)
{
   engine_pressure += val;
   FS_CLAMP_RANGE<float>(engine_pressure, 0, engine_max_pressure);
}

void UFlightPropertySystem::set_engine_pressure(float val)
{
   engine_pressure = val;
   FS_CLAMP_RANGE<float>(engine_pressure, 0, engine_max_pressure);

}

void UFlightPropertySystem::set_engine_max_pressure(float val)
{
   engine_max_pressure = val;
}

void UFlightPropertySystem::DebugPrint()
{
   if (GFlightGlobals && GFlightGlobals->bDrawDebug)
   {

      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("d_cg_rp_body_v: ") + d_cg_rp_body_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_dot_local_v: ") + v_dot_local_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_dot_body_v: ") + v_dot_body_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("a_cg_body_v: ") + a_cg_body_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("a_pilot_body_v: ") + a_pilot_body_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("n_cg_body_v: ") + n_cg_body_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_local_v: ") + v_local_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_local_rel_ground_v: ") + v_local_rel_ground_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_local_airmass_v: ") + v_local_airmass_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_body_v: ") + v_body_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("omega_body_v: ") + omega_body_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("euler_rates_v: ") + euler_rates_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("geocentric_rates_v: ") + geocentric_rates_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("geocentric_position_v: ") + geocentric_position_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("geodetic_position_v: ") + geodetic_position_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("euler_angles_v: ") + euler_angles_v.ToString());
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("nlf: ") + FString::SanitizeFloat(nlf));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_rel_wind: ") + FString::SanitizeFloat(v_rel_wind));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_true_kts: ") + FString::SanitizeFloat(v_true_kts));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_ground_speed: ") + FString::SanitizeFloat(v_ground_speed));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_equiv_kts: ") + FString::SanitizeFloat(v_equiv_kts));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("v_calibrated_kts: ") + FString::SanitizeFloat(v_calibrated_kts));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("alpha: ") + FString::SanitizeFloat(alpha));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("beta: ") + FString::SanitizeFloat(beta));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("gamma_vert_rad: ") + FString::SanitizeFloat(gamma_vert_rad));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("density: ") + FString::SanitizeFloat(density));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("mach_number: ") + FString::SanitizeFloat(mach_number));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("static_pressure: ") + FString::SanitizeFloat(static_pressure));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("total_pressure: ") + FString::SanitizeFloat(total_pressure));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("dynamic_pressure: ") + FString::SanitizeFloat(dynamic_pressure));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("static_temperature: ") + FString::SanitizeFloat(static_temperature));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("total_temperature: ") + FString::SanitizeFloat(total_temperature));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("sea_level_radius: ") + FString::SanitizeFloat(sea_level_radius));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("earth_position_angle: ") + FString::SanitizeFloat(earth_position_angle));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("runway_altitude: ") + FString::SanitizeFloat(runway_altitude));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("climb_rate: ") + FString::SanitizeFloat(climb_rate));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("altitude_agl: ") + FString::SanitizeFloat(altitude_agl));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("throttle_pos: ") + FString::SanitizeFloat(throttle_pos));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("engine rpm: ") + FString::SanitizeFloat(engine_rpm));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("engine temp: ") + FString::SanitizeFloat(engine_temp));
      GEngine->AddOnScreenDebugMessage(-1, 0, FColor::White, FString("engine pressure: ") + FString::SanitizeFloat(engine_pressure));
   }
}
