#include "Sweep_Spectrum.h"
#include "Curve_Sampling.h"
#include "Plottable_Real_Time_Data.h"
#include "../render/Blend2D_Cache.h"
#include "../renderable/Renderable_p.h"
#include <algorithm>
#include <cmath>
#include <deque>
#include <optional>
namespace renderive {
namespace detail {
namespace {
using Sweep_Spectrum_History = Plottable_History_Real_Time_Data<std::vector<double>, std::deque<std::vector<double>>>;
std::vector<double> flatten(const std::deque<std::vector<double>>& blocks) {
    std::vector<double> values;
    for(const auto& block : blocks)
        values.insert(values.end(), block.begin(), block.end());
    return values;
}
}
struct Sweep_Spectrum_Control::Impl : Renderable::Impl {
    struct Prepare_Buffer {
        std::vector<PointF> points;
        PointF current_first;
        PointF current_second;
        bool valid{};
    };
    Impl(renderive_Owner<Axis> frequency, renderive_Owner<Axis> power)
        : frequency_axis(std::move(frequency)), power_axis(std::move(power)) {}
    renderive_Owner<Axis> frequency_axis;
    renderive_Owner<Axis> power_axis;
    std::optional<Sweep_Spectrum_History> blocks;
    Prepare_Buffer prepare_buffer;
};
Sweep_Spectrum_Control::Sweep_Spectrum_Control(const Sweep_Spectrum_Properties& properties, renderive_Owner<Axis> frequency_axis, renderive_Owner<Axis> power_axis)
    : Plottable_State(properties, std::make_unique<Impl>(
          std::move(frequency_axis), std::move(power_axis))) {
    d_func<Impl>().blocks.emplace(*this);
}
Sweep_Spectrum_Control::~Sweep_Spectrum_Control() = default;
void Sweep_Spectrum_Control::append_block(std::span<const double> values) {
    if(get<&Sweep_Spectrum_Properties::bins_per_block>() <= 0)
        set<&Sweep_Spectrum_Properties::bins_per_block>(static_cast<int>(values.size()));
    const int limit = get<&Sweep_Spectrum_Properties::block_count>();
    d_func<Impl>().blocks->update({values.begin(), values.end()}, static_cast<std::size_t>(limit));
    changed();
}
void Sweep_Spectrum_Control::append_block(std::pmr::vector<double>&& values) {
    append_block(std::span<const double>(values.data(), values.size()));
}
std::size_t Sweep_Spectrum_Control::stored_block_count() const {
    return d_func<Impl>().blocks->size();
}
std::size_t Sweep_Spectrum_Control::stored_point_count() const {
    const auto blocks = d_func<Impl>().blocks->snapshot();
    std::size_t count{};
    for(const auto& block : blocks)
        count += block.size();
    return count;
}
std::size_t Sweep_Spectrum_Control::rendered_point_count() const {
    const auto state = properties();
    const auto values = flatten(d_func<Impl>().blocks->snapshot());
    return curve_points(values, state.frequency_range, d_func<Impl>().frequency_axis->transform(), d_func<Impl>().power_axis->transform(), state.visible_range_only, state.interpolation_mode).size();
}
void Sweep_Spectrum_Control::publish() {
    publish_properties();
    const int limit = get<&Sweep_Spectrum_Properties::block_count>();
    d_func<Impl>().blocks->retain_latest(static_cast<std::size_t>(limit));
}
void Sweep_Spectrum_Control::prepare_frame(const Prepare_Render_Context& context) {
    const auto& view = context.frame.render_state;
    const auto& state = render_properties(view);
    const auto& blocks = view.get(*d_func<Impl>().blocks);
    const auto values = flatten(blocks);
    auto& output = d_func<Impl>().prepare_buffer;
    output = {};
    if (values.size() < 2)
        return;
    const Axis_Transform x = d_func<Impl>().frequency_axis->transform(view);
    const Axis_Transform y = d_func<Impl>().power_axis->transform(view);
    output.points = curve_points(values, state.frequency_range, x, y,
                                 state.visible_range_only, state.interpolation_mode);
    const double completed = std::min(1.0, static_cast<double>(blocks.size()) / state.block_count.get());
    const double frequency = state.frequency_range.origin + state.frequency_range.length() * completed;
    output.current_first = mapped_point(x, frequency, y, y.coordinate_range.origin);
    output.current_second = mapped_point(x, frequency, y, y.coordinate_range.target);
    output.valid = true;
}
void Sweep_Spectrum_Control::paint(Painter& painter,
                                   const Paint_Render_Context& context) {
    const auto& output = d_func<Impl>().prepare_buffer;
    if (!output.valid)
        return;
    const auto& view = context.frame.render_state;
    const auto& state = render_properties(view);
    painter.polyline(output.points, state.pen);
    painter.line(output.current_first, output.current_second,
                 state.current_frequency_pen);
}
}
}
