#include "SweepFrequencyWidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
#include <QDebug>
#include "../component/FrequentShower.h"
#include "../component/ProgressBar.h"
#include "../component/ConsoleWidget.h"
#include "../component/ShowTime.h"
#include "../Global.h"
#include "../menu/BaseWidgetMenu.h"
#include "Demo_Gallery/base/Tool.h"
QWidget* Sweep_Frequency_Widget::create_spectrogram_power_plot() {
    class Sweep_Frequent_Plot : public renderive::Latency_Eager_Plot {
    public:
        std::shared_ptr<renderive::Axis> x_axis{};
        std::shared_ptr<renderive::Axis> y_axis{};
        std::shared_ptr<renderive::Multi_Select_Rect> msr{};
        std::shared_ptr<renderive::Sweep_Frequency> sf{};
        QMenu* menu{};
        void init() override {
            Latency_Eager_Plot::init();
            object_name = "Axis_Test_Plot";
            auto data = create_renderable_node(get_root_renderable(), "Data_Renderable");
            auto x_axis_cache = create_renderable_node(get_root_renderable(), "X_Axis_Cache");
            auto y_axis_cache = create_renderable_node(get_root_renderable(), "Y_Axis_Cache");
            auto overlay = create_renderable_node(get_root_renderable(), "Overlay_Renderable");
            x_axis_cache->set_cache_mode(renderive::Renderable_Cache_Mode::Local_Pixel);
            y_axis_cache->set_cache_mode(renderive::Renderable_Cache_Mode::Local_Pixel);
            x_axis = renderive::Axis::Builder(x_axis_cache, Qt::Horizontal)
                    .set_coord_range({0, 100})
                    .set_tick_length(-10).build();
            y_axis = renderive::Axis::Builder(y_axis_cache, Qt::Vertical)
                    .set_coord_range({100, 0})
                    .set_tick_length(-10).build();
            sf = renderive::Sweep_Frequency::Builder(data, x_axis, y_axis)
                 .set_block_num(200)
                 .set_block_frequency_point_size(20)
                 .set_frequency_range({0, 100})
                 .build();
            msr = renderive::Multi_Select_Rect::Builder(overlay, x_axis, y_axis).build();
        }
    protected:
        void resizeEvent(QResizeEvent* event) override {
            x_axis->set_x(0);
            x_axis->set_y(height() - 1);
            x_axis->set_pixel_size(width());
            y_axis->set_x(0);
            y_axis->set_y(0);
            y_axis->set_pixel_size(height());
        }
        void contextMenuEvent(QContextMenuEvent* event) override {
            if (!menu) {
                auto ws = concat_widgets(
                bw(cw(sf.get(), -1), "扫频图"),
                bw(cw(x_axis.get()), "x轴"),
                bw(cw(y_axis.get()), "y轴"),
                bw(cw(msr.get()), "多选框"));
                menu = create_menu(this, ws);
            }
            menu->exec(event->globalPos());
        }
    };
    auto w = new Sweep_Frequent_Plot;
    w->init();
    sweep_frequency_line = w->sf;
    return w;
}
//am, atu, vsql, nb, nr, dnf, pre, att, spl, fil1, agc_a
Sweep_Frequency_Widget::Sweep_Frequency_Widget(QWidget* parent) : Base_Widget(parent) {
    setObjectName("扫频界面");
    int font_size = 20;
    m1 = create_normal("RF Gain\n62", font_size);
    m2 = create_normal("FFT SPAN\n100K", font_size);
    m3 = create_normal("频率表\n（12）89.6MHZ", font_size);
    m4 = create_normal("MODEM\nRTTY", font_size);
    select_value_widget = new Frequent_Shower(this);
    progress_bar1 = new Progress_Bar({
                                        {"S", Qt::black},
                                        {"3", Qt::black},
                                        {"5", Qt::black},
                                        {"7", Qt::black},
                                        {"9", Qt::black},
                                        {"+20", Qt::yellow},
                                        {"+40", Qt::yellow},
                                        {"+60", Qt::red},
                                    }, this);
    progress_bar2 = new Progress_Bar({
                                        {"1", Qt::black},
                                        {"2", Qt::yellow},
                                        {"3", Qt::red},
                                        {">5", Qt::red},
                                    }, this);
    auto main = create_vbox_layout({}, this);
    main->addStretch(1);
    {
        // 中心Layout
        auto center = create_hbox_layout();
        main->addLayout(center, 7);
        auto front = create_vbox_layout();
        center->addLayout(front, 1);
        front->addWidget(m1, 3);
        front->addStretch(1);
        front->addWidget(m2, 3);
        center->addSpacing(4);
        auto middle = create_vbox_layout();
        center->addLayout(middle, 9);
        middle->addStretch(1);
        middle->addWidget(select_value_widget, 4);
        auto middle_h = create_hbox_layout();
        middle->addLayout(middle_h, 2);
        middle_h->addWidget(progress_bar1, 5);
        middle_h->addStretch(1);
        middle_h->addWidget(progress_bar2, 3);
        center->addStretch(5);
        center->addSpacing(4);
        auto back1 = create_vbox_layout();
        center->addLayout(back1, 1);
        back1->addWidget(m3, 3);
        back1->addStretch(1);
        back1->addWidget(m4, 3);
    }
    main->addStretch(1);
    main->addSpacing(8);
    //main->addWidget(new TestWidget, 24);
    main->addWidget(create_spectrogram_power_plot(), 24);
    init_menu();
}
Sweep_Frequency_Widget::~Sweep_Frequency_Widget() {}
void Sweep_Frequency_Widget::focusInEvent(QFocusEvent* event) {
    QWidget::focusInEvent(event);
    select_value_widget->setFocus();
}
void Sweep_Frequency_Widget::init_menu() {
    int font_size = 30;
    auto start_freq = new Value_Menu_Button("起始频率：%1 Hz", font_size, [&](Value_Menu_Button* that, bool add) {
        that->setValue(that->value + (double)(add ? 1 : -1));
    }, -1);
    start_freq->setValue(sweep_frequency_line->frequency_range().origin);
    auto end_freq = new Value_Menu_Button("终止频率：%1 Hz", font_size, [&](Value_Menu_Button* that, bool add) {
        that->setValue(that->value + (double)(add ? 1 : -1));
    }, -1);
    end_freq->setValue(sweep_frequency_line->frequency_range().target);
    auto btn3 = new Menu_Button("设置频率", font_size, [&, start_freq, end_freq]() {
        sweep_frequency_line->set_frequency_range({start_freq->value, end_freq->value});
    });
    base_widget_menu->add_menu_button_on_layout1({start_freq, end_freq, btn3}, 2);
}
