#include "../include/Air_Ground_CEC/loginui.h"
#include "../include/Air_Ground_CEC/mainwindow.hpp"
#include <QMovie>
#include <QWidget>
#include <QMouseEvent>
#include <QApplication>

LoginUi::LoginUi(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LoginUi)
{
    ui->setupUi(this);
    initWindow();

}

LoginUi::~LoginUi()
{
    delete ui;
}

void LoginUi::initWindow(){
    setWindowState();
    setBtnAndLabelState();
}

void LoginUi::setWindowState(){
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlags(Qt::FramelessWindowHint);//隐藏边框
}



void LoginUi::setBtnAndLabelState(){
    QMovie* movie = new QMovie("://images/background.gif");
    ui->label_video->setMovie(movie);
    movie->start();

    ui->btnWinMin->setIcon(QIcon("://images/min.png"));
    ui->btnWinMin_2->setIcon(QIcon("://images/min.png"));
    ui->btnWinClose->setIcon(QIcon("://images/close.png"));
    ui->btnWinClose_2->setIcon(QIcon("://images/close.png"));

    connect(ui->btnWinMin, &QPushButton::clicked, this,
            [=]() { this->showMinimized(); });
    connect(ui->btnWinMin_2, &QPushButton::clicked, this,
            [=]() { this->showMinimized(); });

    connect(ui->btnWinClose, &QPushButton::clicked, this, [=]() { this->close(); });
    connect(ui->btnWinClose_2, &QPushButton::clicked, this, [=]() { this->close(); });

    connect(ui->btnLogin, &QPushButton::clicked, this, [=]() {
        changetoMainwindow();}
    );

}

void LoginUi::mousePressEvent(QMouseEvent *event) {
  m_lastPos = event->globalPos();
  isPressedWidget = true;  // 当前鼠标按下的即是QWidget而非界面上布局的其它控件
}

void LoginUi::mouseMoveEvent(QMouseEvent *event) {
  if (isPressedWidget) {
    this->move(this->x() + (event->globalX() - m_lastPos.x()),
               this->y() + (event->globalY() - m_lastPos.y()));
    m_lastPos = event->globalPos();
  }
}

void LoginUi::mouseReleaseEvent(QMouseEvent *event) {
  // 其实这里的mouseReleaseEvent函数可以不用重写
  m_lastPos = event->globalPos();
  isPressedWidget = false;  // 鼠标松开时，置为false
}

bool LoginUi::verifyAccount(string usr, string pwd){
    if (usr == "admin" && pwd == "admin"){
        return true;
    }
    return false;
}

void LoginUi::changetoMainwindow(){
    this->close();

    //MainWindow CEC(0,0,0);
    //CEC.show();


}









