//
// Created by Krad on 2021/12/16.
//

#include "RollingMessageWidget.h"
#include <QLabel>
#include <QThread>
#include <QDebug>


const int ROLL_DISTANCE = 32;
const int WAIT_TIMES = 30;

RollingMessageWidget::RollingMessageWidget(QWidget *parent) :QWidget(parent){
    this->setFixedHeight(36);
    this->setFixedWidth(500);
    label_current = new QLabel(this);
    label_next = new QLabel(this);
    QPoint p1 = this->mapToGlobal({5,2});
    label_current->setFixedSize(400, 30);
    label_current->show();
    label_current->move(p1);
    label_current->setText(tr("Message of warn!"));
    QPoint p2 = this->mapToGlobal({5,-30});
    label_next->setFixedSize(400, 30);
    label_next->show();
    label_next->move(p2);
    label_next->setText(tr("Message2 of warn!"));
    timerId = startTimer(100);
}

void RollingMessageWidget::timerEvent(QTimerEvent *e) {
    if (!this->isVisible()) return;
    //滚动
    if (rolling) {
        //end rolling, reset next
        if (rollStep == ROLL_DISTANCE){
            std::swap(label_current,label_next);
            rolling = false;
            QPoint p3 = this->mapToGlobal({5,-30});
            p3.setX(label_next->geometry().x());
            label_next->move(p3);
            return;
        }
        //rolling
        QPoint p1 = label_current->geometry().topLeft();
        p1.setY(p1.y()+1);
        label_current->move(p1);
        QPoint p2 = label_next->geometry().topLeft();
        p2.setY(p2.y()+1);
        label_next->move(p2);
        rollStep++;
        return ;
    }
    else{

        //有等待的高优先级消息时，不停顿
        if (waitStep == WAIT_TIMES || waitPriorityCount > 0){
            locker.lock();
            label_next->setText(getNextMessage());
            locker.unlock();
        } else{
            waitStep++;
        }
    }
}

void RollingMessageWidget::setMessageList(const QStringList &message) {
    //empty, don't set
    if (message.isEmpty()) return;
    messages.clear();
    needRolling = false;
    // set default label
    label_current->setText(message[0]);
    messages.push_back({0,message[0],0});
    //add list
    if (message.length()>0)
    {

        for (int k = 1; k < message.length(); ++k) {
            messages.push_front({k, message[k],-1});
        }
        needRolling = true;
    }

}

void RollingMessageWidget::updateMessagePriority(int innerIndex, int priority) {
    //lock thread
    locker.lock();
    //高优先级，需要插入
    if (priority > 0)
    {
        int idx = -1, insert_idx = -1;
        for (int i = 0; i < messages.length(); ++i) {
            if (messages[i].innerId == innerIndex)
            {
                messages[i].priority = priority;
                idx = i;
            }
            else if (insert_idx < 0 && messages[i].priority<1)
            {
                insert_idx = i;
            }
            if (idx>=0 && insert_idx >=0) break;
        }
        insert_idx = insert_idx==-1?0:insert_idx;
        //move
        if(insert_idx<idx)messages.move(idx,insert_idx);
        waitPriorityCount++;
    }
    // 低优先级，更新状态即可
    else{
        for (WarnMessage & message : messages) {
            if (message.innerId == innerIndex) {
                message.priority = -1;
                break;
            }
        }
    }
    locker.unlock();
}

QString RollingMessageWidget::getNextMessage() {

    QString ret = "No message";
    int retryCount = 0;
    bool priorityFlag = false;
    while (retryCount<messages.length()){
        messages.move(0,messages.length()-1);
        retryCount++;
        if(messages.last().priority>=0)
        {
            qDebug()<<"get next:"<<messages.last().toString();
            priorityFlag = messages.last().priority>0;
            ret = messages.last().msg;
            messages.last().priority = 0;
            break;
        }
    }
    if (priorityFlag && waitPriorityCount > 0) waitPriorityCount--;
    rollStep = 0;
    rolling = true;
    waitStep = 0;
    return ret;
}
