// Copyright (C) 2025 - zsliu98
// This file is part of ZLEqualizer
//
// ZLEqualizer is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 as published by the Free Software Foundation.
//
// ZLEqualizer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with ZLEqualizer. If not, see <https://www.gnu.org/licenses/>.

#pragma once

#include "multiple_fft_base.hpp"

namespace zldsp::analyzer {
    /**
     * a fft analyzer that makes sure that multiple FFTs are synchronized in time
     * and can create corresponding analyzer paths
     * @tparam FloatType the float type of input audio buffers
     * @tparam kFFTNum the number of FFTs
     * @tparam kPointNum the number of output points
     */
    template <typename FloatType, size_t kFFTNum, size_t kPointNum>
    class MultipleFFTAnalyzer final : public MultipleFFTBase<FloatType, kFFTNum, kPointNum> {
    public:
        explicit MultipleFFTAnalyzer(const size_t fft_order = 12) :
            MultipleFFTBase<FloatType, kFFTNum, kPointNum>(fft_order) {
        }

        ~MultipleFFTAnalyzer() = default;

        /**
         * create path x coordinate
         * @param xs
         * @param width
         */
        void createPathXs(std::span<float> xs, const float width) {
            for (size_t idx = 0; idx < this->interplot_freqs_p_.size(); ++idx) {
                xs[idx] = this->interplot_freqs_p_[idx] * width;
            }
        }

        /**
         * create path y coordinate
         * @param ys
         * @param height
         * @param min_db
         * @param max_db
         */
        void createPathYs(std::array<std::span<float>, kFFTNum> ys, const float height,
                          const float min_db = -72.f, const float max_db = 0.f) {
            std::vector<size_t> is_on_vector{};
            for (size_t i = 0; i < kFFTNum; ++i) {
                if (this->is_on_[i].load(std::memory_order::relaxed)) {
                    is_on_vector.push_back(i);
                }
            }
            const auto scale = height / min_db;
            for (const auto& i : is_on_vector) {
                if (!ys[i].empty()) {
                    auto db = kfr::make_univector(this->result_dbs_[i]);
                    auto y = kfr::make_univector(ys[i]);
                    if (std::abs(max_db) > 0.01f) {
                        y = (db - max_db) * scale;
                    } else {
                        y = db * scale;
                    }
                }
            }
        }
    };
}
