#include "FrequentShower.h"
#include <QPainter>
#include <QDebug>
#include <QKeyEvent>
#include "../Global.h"




Frequent_Shower::Frequent_Shower(QWidget *parent) : QWidget(parent) {
    setObjectName("设置频率值控件");
    set_frequent(Back_End::instance()->YsGetFreq());
}

void Frequent_Shower::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    QFont font;
    font.setPointSize(calculate_optimal_font_size(frequent, {(int)((double)width() * 0.9), height()}, font));
    painter.setFont(font);
    painter.setPen(Qt::black);
    painter.drawText(rect(), Qt::AlignCenter, frequent);
    QFontMetrics fontMetrics(font);
    if (select_index >= 0 && select_index < frequent.length()) {
        int a = (QWidget::width() - fontMetrics.horizontalAdvance(frequent)) / 2;
        int start_x = a + fontMetrics.horizontalAdvance(frequent.mid(0, select_index));
        int w = fontMetrics.horizontalAdvance(frequent[select_index]);
        painter.fillRect(start_x, 0, w, QWidget::height(), Qt::black);
        painter.setPen(Qt::white);
        painter.drawText(QRect(start_x, 0, w, QWidget::height()),  Qt::AlignCenter, QString(frequent[select_index]));
    }

}

void Frequent_Shower::keyPressEvent(QKeyEvent *event) {
    Back_End* g = Back_End::instance();

    QString key_text = event->text();
    if (key_text >= "0" && key_text <= "9") {
        frequent[select_index] = key_text.at(0).toLatin1();
        update();
        return;
    }
    if (g->match(event, "左移")) {
        move(-1);
    } else if (g->match(event, "右移")) {
        move(1);
    } else if (g->match(event, "增加")) {
        change(1);
    } else if (g->match(event, "减小")) {
        change(-1);
    }
    event->ignore();

}

void Frequent_Shower::move(int num) {
    select_index += num;
    if(select_index < 0) {
        select_index = 0;
    } else if(select_index >= frequent.length()) {
        select_index = frequent.length() - 1;
    }
    if(frequent[select_index] == '.') {
        if(num > 0) {
            select_index++;
        } else {
            select_index--;
        }
    }
    update();
}

void Frequent_Shower::change(int num) {
    if (select_index >= 0 && select_index < frequent.length()) {
        int n = frequent[select_index].digitValue();
        n += num;
        if (n < 0) {
            n = 9;
        } else if (n > 9) {
            n = 0;
        }
        frequent[select_index] = QString::number(n)[0];
        update();
        Back_End::instance()->YsSetFreq(QString(frequent).replace('.', "").toULongLong());
    }
}

void Frequent_Shower::set_frequent(quint64 frequent) {
    QString str_number = QString::number(frequent, 10);
    int padding = 11 - str_number.length();
    if (padding > 0) {
        str_number = QString(padding, '0') + str_number;
    }
    this->frequent = QString("%1.%2.%3.%4").arg(
                str_number.mid(0, 2),
                str_number.mid(2, 3),
                str_number.mid(5, 3),
                str_number.mid(8, 3)
            );
//    qDebug() << str_number << "   " << frequent;
    update();
}

