#include "passthrough_processor.hpp"

#include <cmath>

namespace radar::processing {
namespace {

constexpr float kPi = 3.14159265358979323846F;

}  // namespace

auto PassThroughProcessor::name() const -> std::string {
    return "pass_through";
}

auto PassThroughProcessor::process(const ipc::SweepTraceBlock& trace, const ProcessingLiveConfig& live_config)
    -> ipc::ResultPayload {
    ipc::ResultPayload payload{};
    payload.processing_name = name();
    payload.kind = ipc::ResultKind::TraceComplex;
    payload.frequency_hz = trace.frequency_hz;
    payload.trace = trace.s21;

    const float linear_gain = std::pow(10.0F, live_config.gain_db / 20.0F);
    const float phase_rad = live_config.phase_deg * (kPi / 180.0F);
    const float cos_phase = std::cos(phase_rad);
    const float sin_phase = std::sin(phase_rad);

    for (auto& sample : payload.trace) {
        const float re = sample.re;
        const float im = sample.im;
        sample.re = linear_gain * ((re * cos_phase) - (im * sin_phase));
        sample.im = linear_gain * ((re * sin_phase) + (im * cos_phase));
    }

    return payload;
}

}  // namespace radar::processing
