#include "navprogress.h"

NavProgress::NavProgress(QWidget *parent) : QWidget(parent), navStyle(NavStyle_JD), maxStep(3), state(0)  {

    background = Qt::gray;
    foreground = Qt::white;
    firstCircleGreen = false;
    firstCircleBlinking =false;
    secondCircleGreen = false;
    secondCircleBlinking = false;
    thridCircleGreen = false;
    thridCircleBlinking = false;
    topInfo = {"组网", "充电", "起爆"};
    blinkTimer = new QTimer(this);
    connect(blinkTimer, &QTimer::timeout, this, &NavProgress::toggleBlinkColors);
    blinkColor = Qt::red;
    secondBlinkColor = Qt::red;
    qDebug()<<"width"<<this->width();
}
void NavProgress::setNavStyle(NavStyle style) {
    navStyle = style;
    update();
}

void NavProgress::setMaxStep(int step) {
    maxStep = step;
    update();
}


void NavProgress::setTopInfo(const QVector<QString>& info) {
    topInfo = info;
    update();
}


void NavProgress::setFirstCircleGreen(bool green) {
    firstCircleGreen = green;
    update();
}

void NavProgress::setFirstCircleBlinking(bool blinking) {
    firstCircleBlinking = blinking;
    if (blinking) {
        blinkTimer->start(500);
    } else {
        blinkTimer->stop();
        update();
    }
}



QVector<QColor> NavProgress::getCircleColors() {
    QVector<QColor> colors;
    for (int i = 0; i < maxStep; i++) {
        if (i == 0 && firstCircleGreen) {
            colors.append(Qt::green);
        } else {
            colors.append(background);
        }
    }
    return colors;
}

void NavProgress::toggleBlinkColors() {

    blinkColor = (blinkColor == Qt::red)? Qt::gray : Qt::red;
    if (secondCircleBlinking) {

        secondBlinkColor = (secondBlinkColor == Qt::red)? Qt::gray : Qt::red;
    }
    if (thridCircleBlinking) {

        secondBlinkColor = (secondBlinkColor == Qt::red)? Qt::gray : Qt::red;
    }
    update();
}


void NavProgress::setState(int newState) {
    state = newState;
    switch (state) {
        case 0:
            firstCircleGreen = false;
            firstCircleBlinking = false;
            secondCircleBlinking = false;
            secondCircleGreen = false;
            thridCircleBlinking = false;
            thridCircleGreen = false;
            blinkTimer->stop();
            break;
        case 1:
            firstCircleGreen = false;
            firstCircleBlinking = true;
            secondCircleBlinking = false;
            secondCircleGreen = false;
            thridCircleBlinking = false;
            thridCircleGreen = false;
            blinkTimer->start(500);
            break;
        case 2:
            firstCircleGreen = true;
            firstCircleBlinking = false;
            secondCircleBlinking = false;
            secondCircleGreen = false;
            thridCircleBlinking = false;
            thridCircleGreen = false;
            break;
        case 3:
            firstCircleGreen = true;
            firstCircleBlinking = false;
            secondCircleBlinking = true;
            secondCircleGreen = false;
            thridCircleBlinking = false;
            thridCircleGreen = false;
            blinkTimer->start(500);
            break;
        case 4:
            firstCircleGreen = true;
            firstCircleBlinking = false;
            secondCircleBlinking = false;
            secondCircleGreen = true;
            thridCircleBlinking = false;
            thridCircleGreen = false;
            break;
        case 5:
            firstCircleGreen = true;
            firstCircleBlinking = false;
            secondCircleBlinking = false;
            secondCircleGreen = true;
            thridCircleBlinking = true;
            thridCircleGreen = false;
            blinkTimer->start(500);
            break;
        case 6:
            firstCircleGreen = true;
            firstCircleBlinking = false;
            secondCircleBlinking = false;
            secondCircleGreen = true;
            thridCircleBlinking = false;
            thridCircleGreen = true;
            break;
        default:
            break;
    }
    update();
}

void NavProgress::paintEvent(QPaintEvent *) {

    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);


    if (navStyle == NavStyle_JD) {

        drawBg_JD(&painter);

        drawText_JD(&painter);

    }
}

void NavProgress::drawBg_JD(QPainter *painter) {
    painter->save();


    int width = this->width() / maxStep;
    int height = this->height() / 2;
    int radius = height / 4;
    int initX = 0 + radius;
    int initY = height / 2 + radius / 5;

    initX = width / 2;
    QPen pen;
    pen.setWidthF((double)radius / 4);
    pen.setCapStyle(Qt::RoundCap);

    for (int i = 0; i < maxStep - 1; i++) {
        if (i == 0 && firstCircleGreen) {
            pen.setColor(Qt::green);
        }else if(i == 1 && secondCircleGreen){
            pen.setColor(Qt::green);
        }else {
            pen.setColor(background);
        }
        painter->setPen(pen);
        painter->setBrush(Qt::NoBrush);
        painter->drawLine(QPoint(initX+ radius + radius , initY), QPoint(initX + width - (radius + radius), initY));
        initX += width;
    }


    initX = width / 2;
    painter->setPen(Qt::NoPen);

    for (int i = 0; i < maxStep; i++) {

        if (i == 0 && firstCircleGreen) {
            painter->setBrush(Qt::green);
        } else if (i == 0 && firstCircleBlinking) {
            painter->setBrush(blinkColor);
        } else if (i == 1 && secondCircleBlinking) {
            painter->setBrush(secondBlinkColor);
        }  else if (i == 1 && secondCircleGreen) {
            painter->setBrush(Qt::green);
        }else if (i == 2 && thridCircleBlinking) {
            painter->setBrush(secondBlinkColor);
        }else if (i == 2 && thridCircleGreen) {
            painter->setBrush(Qt::green);
        }
        else {
            painter->setBrush(background);
        }
        painter->drawEllipse(QPoint(initX, initY), radius, radius);
        initX += width;
    }

    initX = width / 2;
    QPen outerPen;
    outerPen.setWidth(5);
    for (int i = 0; i < maxStep; i++) {
        if (i == 0 && firstCircleBlinking) {
            outerPen.setColor(blinkColor);
        } else if (i == 0 && firstCircleGreen) {
            outerPen.setColor(Qt::green); // 第一个空心圆常亮绿色
        } else if (i == 1 && secondCircleBlinking) {
            outerPen.setColor(secondBlinkColor);
        } else if (i == 1 && secondCircleGreen) {
            outerPen.setColor(Qt::green);
        }
        else if (i == 2 && thridCircleBlinking) {
            outerPen.setColor(secondBlinkColor);
        }else if (i == 2 && thridCircleGreen) {
            outerPen.setColor(Qt::green);
        }
        else {
            outerPen.setColor(background);
        }
        painter->setPen(outerPen);
        painter->setBrush(Qt::NoBrush);
        int outerRadius = radius + 5; // 外层圆半径比内层圆大 5，可按需调整
        painter->drawEllipse(QPoint(initX, initY), outerRadius, outerRadius);
        initX += width;
    }

    initX = width / 2;
    QFont font;
    font.setPixelSize(radius);
    painter->setFont(font);
    painter->setPen(foreground);
    painter->setBrush(Qt::NoBrush);

    for (int i = 0; i < maxStep; i++) {
        QRect textRect(initX - radius, initY - radius, radius * 2, radius * 2);
        painter->drawText(textRect, Qt::AlignCenter, QString::number(i + 1));
        initX += width;
    }

    painter->restore();
}

void NavProgress::drawText_JD(QPainter *painter) {
    int width = this->width() / maxStep;
    int height = this->height() / 2;
    int initX = 0;
    int initY = height * 1 /2 +10;
    painter->save();
    QFont font;
    font.setPixelSize(height / 4);
    painter->setFont(font);
    for (int i = 0; i < maxStep; i++) {
        if (i == 0 && firstCircleGreen) {
            painter->setPen(Qt::green); // 第一个文字为绿色
        } else {
            painter->setPen(background);
        }
        painter->setBrush(Qt::NoBrush);
        QRect textRect(initX, initY, width, height);
        painter->drawText(textRect, Qt::AlignCenter, topInfo.at(i));
        initX += width;
    }
    painter->restore();
}

