#include "widget.h"
#include "ui_widget.h"
#include "tchttpservice.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    initWidgetForm();
    initWidget();
    initSignalSlot();
    // this->btnPageUpload->setChecked(true);
    // this->btnPageUpload->setStyleSheet("{border: none;border-right: 4px solid #409EFF;color: #409EFF;}");
}

Widget::~Widget()
{
    delete fileConfig;
    delete ui;
}

bool Widget::eventFilter(QObject *watched, QEvent *event)
{
    static QPoint mousePoint;
    static bool mousePressed = false;


    if (watched == btnPageUpload) {
        if (event->type() == QEvent::Enter) {
            QIcon icon(":/qrc/image/upload_blue.png");
            btnPageUpload->setIcon(icon);
            return true;
        }
        else if (event->type() == QEvent::Leave && !btnPageUpload->isChecked()) {
            QIcon icon(":/qrc/image/upload_white.png");
            btnPageUpload->setIcon(icon);
            return true;
        }
        else {
            return false;
        }
    }

    if (watched == btnPageAlbum) {
        if (event->type() == QEvent::Enter) {
            QIcon icon(":/qrc/image/album_blue.png");
            btnPageAlbum->setIcon(icon);
            return true;
        }
        else if (event->type() == QEvent::Leave && !btnPageAlbum->isChecked()) {
            QIcon icon(":/qrc/image/album_white.png");
            btnPageAlbum->setIcon(icon);
            return true;
        }
        else {
            return false;
        }
    }

    if (watched == btnPageSetting) {
        if (event->type() == QEvent::Enter) {
            QIcon icon(":/qrc/image/setting_blue.png");
            btnPageSetting->setIcon(icon);
            return true;
        }
        else if (event->type() == QEvent::Leave && !btnPageSetting->isChecked()) {
            QIcon icon(":/qrc/image/setting_white.png");
            btnPageSetting->setIcon(icon);
            return true;
        }
        else {
            return false;
        }
    }

    if (watched == labAbout) {
        if (event->type() == QEvent::Enter) {
            labAbout->setPixmap(QPixmap(":/qrc/image/info_blue.png"));
            return true;
        }
        else if (event->type() == QEvent::Leave) {
            labAbout->setPixmap(QPixmap(":/qrc/image/info_grey.png"));
            return true;
        }

        static bool labAboutPress = false;
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if (mouseEvent->type() == QMouseEvent::MouseButtonPress && mouseEvent->button() == Qt::LeftButton) {
            labAboutPress = true;
            return true;
        }
        else if (mouseEvent->type() == QMouseEvent::MouseButtonRelease && labAboutPress && mouseEvent->button() == Qt::LeftButton) {
            QPoint pos = QCursor::pos();
            menuPop->popup(pos);
            return true;
        }
        else {
            return false;
        }

    }

    if (watched->objectName() == "frmTop") {
        QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
        if (mouseEvent->type() == QEvent::MouseButtonPress) {
            if (mouseEvent->button() == Qt::LeftButton) {
                mousePressed = true;
                mousePoint = mouseEvent->globalPos() - this->pos();
            }
        }
        else if (mouseEvent->type() == QEvent::MouseButtonRelease){
            mousePressed = false;
        }
        else if (mouseEvent->type() == QEvent::MouseMove) {
            if (mousePressed) {
                this->move(mouseEvent->globalPos() - mousePoint);
                return true;
            }
        }
    }

    return QWidget::eventFilter(watched, event);
}



void Widget::paintEvent(__attribute__((unused))QPaintEvent *event)
{

//    QPainter painter(this);
//    painter.setRenderHint(QPainter::Antialiasing);
//    painter.setPen(Qt::NoPen);
//    painter.setBrush(QColor("#3F3C37"));

//    painter.drawRoundedRect(rect(), 15, 15);

//    QWidget::paintEvent(event);
    QStyleOption opt;
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    opt.init(this);
#else
    opt.initFrom(this);
#endif
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
}

void Widget::initWidgetForm()
{
    // this->setFixedSize(QSize(1000, 550));
    this->setFixedSize(800, 450);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setWindowFlags(this->windowFlags() | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
    this->setWindowIcon(QIcon(QPixmap(":/qrc/image/icon.png")));

    this->setAttribute(Qt::WA_TranslucentBackground);

    initLeftMenu();

    ui->btnMin->setFlat(true);
    ui->btnClose->setFlat(true);

    IconHelper::setIconFontIndex(2);

    IconHelper::setIcon(ui->btnMin, QString("F068").toInt(nullptr, 16), 16);
    IconHelper::setIcon(ui->btnClose, QString("F00D").toInt(nullptr, 16), 16);

    ui->labTitle->setText("PicPanel");

}

void Widget::initWidget()
{
    menuPop = new QMenu;
    actAbout = new QAction;
    actRcode = new QAction;
    actPrivateLic = new QAction;

    actAbout->setText("关于");
    actRcode->setText("生成配置二维码");
    actPrivateLic->setText("隐私协议");

    menuPop->addAction(actAbout);
    menuPop->addAction(actRcode);
    menuPop->addAction(actPrivateLic);

    connect(actAbout, &QAction::triggered, this, &Widget::actAboutSlot);
    connect(actRcode, &QAction::triggered, this, &Widget::actRcodeSlot);
    connect(actPrivateLic, &QAction::triggered, this, &Widget::actPrivateLicSlot);

    ui->frmTop->installEventFilter(this);

    btnPageUpload->setChecked(true);
    ui->stackedWidget->setCurrentIndex(0);

    ui->horizontalLayout_2->setSizeConstraint(QLayout::SetFixedSize);
}

void Widget::initSignalSlot()
{
    QList<QPushButton*> btns = ui->frame->findChildren<QPushButton*>();
    foreach (QPushButton* b, btns) {
        b->setFlat(true);
        connect(b, &QPushButton::clicked, this, &Widget::pageChangeSlot);
        b->installEventFilter(this);
    }

}

void Widget::initLeftMenu()
{
    frmupload = new FrmUpload;
    frmalbum = new FrmAlbum;
    frmsetting = new FrmSetting(this);

    ui->frmTop->setPalette(QPalette(QColor(63, 60, 55)));

    btnPageUpload = new QPushButton(ui->frame);
    btnPageUpload->setObjectName("btnPageUpload");
    btnPageUpload->setFlat(true);
    btnPageUpload->setCursor(Qt::PointingHandCursor);
    btnPageUpload->setText("上传区");
    btnPageUpload->setMinimumWidth(160);
    btnPageUpload->setMinimumHeight(30);
    btnPageUpload->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
    QIcon iconUpload(QPixmap(":/qrc/image/upload_blue.png"));
    btnPageUpload->setIconSize(QSize(24, 24));
    btnPageUpload->setIcon(iconUpload);
    btnPageUpload->setCheckable(true);

    btnPageAlbum = new QPushButton(ui->frame);
    btnPageAlbum->setObjectName("btnPageAlbum");
    btnPageAlbum->setFlat(true);
    btnPageAlbum->setCursor(Qt::PointingHandCursor);
    btnPageAlbum->setText("相册");
    btnPageAlbum->setMinimumWidth(160);
    btnPageAlbum->setMinimumHeight(30);
    btnPageAlbum->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
    QIcon iconAlbum(QPixmap(":/qrc/image/album_white.png"));
    btnPageAlbum->setIconSize(QSize(24, 24));
    btnPageAlbum->setIcon(iconAlbum);
    btnPageAlbum->setCheckable(true);

    btnPageSetting = new QPushButton(ui->frame);
    btnPageSetting->setObjectName("btnPageSetting");
    btnPageSetting->setFlat(true);
    btnPageSetting->setCursor(Qt::PointingHandCursor);
    btnPageSetting->setText("PicPanel设置");
    btnPageSetting->setMinimumWidth(160);
    btnPageSetting->setMinimumHeight(30);
    btnPageSetting->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
    QIcon iconSetting(QPixmap(":/qrc/image/setting_white.png"));
    btnPageSetting->setIconSize(QSize(24, 24));
    btnPageSetting->setIcon(iconSetting);
    btnPageSetting->setCheckable(true);

    ui->stackedWidget->addWidget(frmupload);
    ui->stackedWidget->addWidget(frmalbum);
    ui->stackedWidget->addWidget(frmsetting);

    QVBoxLayout* frameLayout = new QVBoxLayout;
    frameLayout->setContentsMargins(0, 0, 0, 0);
    frameLayout->setSpacing(0);
    QSpacerItem* verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
    frameLayout->addWidget(btnPageUpload);
    frameLayout->addWidget(btnPageAlbum);
    frameLayout->addWidget(btnPageSetting);
    frameLayout->addSpacerItem(verticalSpacer);

    QHBoxLayout* buttomLayout = new QHBoxLayout;
    labAbout = new QLabel;
    QSpacerItem* horizenSpacer = new QSpacerItem(250, 20, QSizePolicy::Minimum, QSizePolicy::Minimum);
    labAbout->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    labAbout->setMaximumSize(32, 32);
    QPixmap pixInfo(":/qrc/image/info_grey.png");
    labAbout->setPixmap(pixInfo);
    labAbout->installEventFilter(this);
    buttomLayout->addWidget(labAbout);
    buttomLayout->addSpacerItem(horizenSpacer);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    buttomLayout->setMargin(0);
#else
    buttomLayout->setContentsMargins(0, 0, 0, 0);
#endif

    frameLayout->addItem(buttomLayout);
    frameLayout->setSpacing(40);
    ui->frame->setLayout(frameLayout);
}



void Widget::on_btnClose_clicked()
{
    close();
}

void Widget::on_btnMin_clicked()
{
    showMinimized();
}

void Widget::pageChangeSlot()
{
    QPushButton* btn = (QPushButton*)sender();

    QList<QPushButton*> btns = ui->frame->findChildren<QPushButton*>();
    foreach (QPushButton* b, btns) {
        b->setCheckable(true);
        if (b == btn) {
            b->setChecked(true);
        }
        else {
            b->setChecked(false);
        }
    }
    QIcon iconUploadWhite(":/qrc/image/upload_white.png");
    QIcon iconAlbumWhite(":/qrc/image/album_white.png");
    QIcon iconSettingWhite(":/qrc/image/setting_white.png");
    QIcon iconUploadBule(":/qrc/image/upload_blue.png");
    QIcon iconAlbumBlue(":/qrc/image/album_blue.png");
    QIcon iconSettingBlue(":/qrc/image/setting_blue.png");

    if (btn->objectName() == "btnPageUpload") {
        btn->setIcon(iconUploadBule);
        btnPageAlbum->setIcon(iconAlbumWhite);
        btnPageSetting->setIcon(iconSettingWhite);
        ui->stackedWidget->setCurrentWidget(frmupload);
    }
    else if (btn->text() == "相册") {
        btn->setIcon(iconAlbumBlue);
        btnPageUpload->setIcon(iconUploadWhite);
        btnPageSetting->setIcon(iconSettingWhite);
        ui->stackedWidget->setCurrentWidget(frmalbum);
    }
    else {
        btn->setIcon(iconSettingBlue);
        btnPageAlbum->setIcon(iconAlbumWhite);
        btnPageUpload->setIcon(iconUploadWhite);
        ui->stackedWidget->setCurrentWidget(frmsetting);
    }
}

void Widget::actAboutSlot()
{

}

void Widget::actRcodeSlot()
{

}

void Widget::actPrivateLicSlot()
{
    qDebug() << "prv";
    QMessageBox::information(this, "隐私协议", "本软件尊重并保护所有使用服务用户的个人隐私权，为了给您提供更准确、"
                                           "更优质的服务，本软件会按照本隐私权政策的规定使用和收集您的一些行为信息。您在同意本软件服务使用协议之时，即视"
                                           "为您已经同意本隐私权政策全部内容。本隐私权政策属于本软件服务使用协议不可分割的一部分，如果不同意将无法使用。本协议会定期更新。<br><br>"
                             "1.适用范围<br><br>"
                             "a)在您使用本软件时，本软件会记录的您对本软件的一些操作行为信息，包括但不限于您使用本软件进行文件上传的耗时、类型、数量等信息。<br><br>"
                             "2.信息的使用<br><br>"
                             "a)在获得悠的使用数据之后，本软件会将其上传至数据分析服务器，以便分析数据后，提供给您更好的服务<br><br>"
                             "3.信息披露<br><br>"
                             "a)本软件不会将您的信息披露给不受信任的第三方。<br><br>"
                             "b)根据法律的有关规定，或者行政或司法机构的要求，向第三方或者行政、司法机构披露<br><br>"
                             "c)如您出现违反中国有关法律、法规或者相关规则的情况，需要向第三方披露<br><br>"
                             "4.信息安全<br><br>"
                             "a)本软件不会收集您的个人信息、密钥信息等隐私信息，所收集的信息仅仅作为改善软件、优化体验、了解软件日活等用途。");
}
