#include "MiniStoreStatusForm.h"
#include "ui_MiniStoreStatusForm.h"
#include "DTools/configManger.h"
#include "QsLog.h"

extern QThread  workThread;


MiniStoreStatusForm::MiniStoreStatusForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MiniStoreStatusForm)
{
    ui->setupUi(this);
    m_timer =new QTimer(this);
    m_timer->setSingleShot(true);
    connect(m_timer,&QTimer::timeout,this,&MiniStoreStatusForm::hideForm);
    _Init();
}


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


void MiniStoreStatusForm::mouseMoveEvent(QMouseEvent *event)
{
    if ((event->buttons()==Qt::LeftButton) && m_bMousePress)
    {
        QPoint moveAmount = event->globalPos() - m_lastMousePos;
        m_absMove += moveAmount;
        move( pos() + moveAmount );
        m_lastMousePos = event->globalPos();
        m_bMouseMove = true;

        // 当移动子集门店营业状态表单时, 主悬浮框门店营业状态表单也同时移动;
        // QPoint mSSFPoint = pos();
        // mSSFPoint.setX( mSSFPoint.x() - MINI_STORE_STATUS_FORM_LEFT_OFFSET );
        // m_miniStoreStatusForm->move( mSSFPoint + moveAmount );
    }
}


void MiniStoreStatusForm::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_bMousePress = true;
        m_lastMousePos = event->globalPos();
        m_absMove = QPoint(0,0);
    }
}


void MiniStoreStatusForm::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_bMousePress = false;
    }
    m_bMouseMove = false;
    QPoint point = this->pos();
    QLOG_INFO()<<QString::fromLocal8Bit("MiniStoreStatusForm current Form position(x:%1,y:%2)").arg(point.x()).arg(point.y());
    int nWidth = GetSystemMetrics(SM_CXSCREEN) - 200;
    int nHeight = GetSystemMetrics(SM_CYSCREEN) - 220;
    QLOG_INFO()<<QString::fromLocal8Bit("MiniStoreStatusForm active_Range(x:%1,y:%2)").arg(nWidth).arg(nHeight);
    if( (point.x()<nWidth && -80<point.x()) && (point.y()<nHeight && -50<point.y()) ){
        // ConfigManger::GetInstance().setFloatInitPostion(this->pos());
    }else{
        point.setX(nWidth);
        point.setY(nHeight);
        this->move(QPoint(nWidth, nHeight));
        // ConfigManger::GetInstance().setFloatInitPostion(point);
        // 同步移动 主悬浮框营业状态位置
       // m_miniStoreStatusForm->move( QPoint(nWidth  - MINI_STORE_STATUS_FORM_LEFT_OFFSET, nHeight) );
    }
}


void MiniStoreStatusForm::_Init()
{
    setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
    setAttribute(Qt::WA_TranslucentBackground);
    this->setModal(Qt::NonModal);
    this->ui->MiniStoreStatusCloseBtn->setIcon( QIcon(":CloseButtonIcon.png") );
    this->ui->MiniStoreStatusCloseBtn->setIconSize( QSize( 25, 25) );
    setStyleSheet("#miniStoreStatusWdg{ border-image: url(:MiniStoreBusinessStatus.png); }");
}


void MiniStoreStatusForm::hideForm()
{
    this->hide();
}


void MiniStoreStatusForm::onShow(int modStoreStatus, int eleStoreStatus, int mtStoreStatus)
{
    QLOG_INFO()<<"MiniStoreStatusForm::show() -----------------> ";
    QPoint point = ConfigManger::GetInstance().GetFloatInitPostion();
    QLOG_INFO()<<QString::fromLocal8Bit("MiniStoreStatusForm Init Postion(x:%1,y:%2)").arg(point.x()).arg(point.y());
    point.setX(point.x() - MINI_STORE_STATUS_FORM_LEFT_OFFSET );
    this->move(point);
    RefreshStoreStatus(modStoreStatus, eleStoreStatus, mtStoreStatus);
    show();
}


void MiniStoreStatusForm::RefreshStoreStatus(int modStoreStatus, int eleStoreStatus, int mtStoreStatus) {
    QLOG_INFO()<<QString("[<<<----MiniStoreStatusForm::RefreshStoreStatus:  modStoreStatus:%1 , eleStoreStatus: %2 , mtStoreStatus: %3--->>>>]")
                 .arg(modStoreStatus).arg(eleStoreStatus).arg(mtStoreStatus);

    int titleLab_w = 22, titleLab_h = 14,  statusLab_w = 20, statusLab_h = 12;
    int first_x = 4, first_y = 35 ;  // int first_x = 4, first_y = 30 ;
    this->ui->stackedWidget->move(first_x, first_y);
    //准备MOD门店营业状态Label;
    this->ui->Mod1Lable->setPixmap( QPixmap(":ModTitle.png").scaled(titleLab_w, titleLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    if(1 == modStoreStatus){
        this->ui->Mod2Lable->setPixmap( QPixmap(":StoreOn.png").scaled(statusLab_w, statusLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    }else if(2 == modStoreStatus){
        this->ui->Mod2Lable->setPixmap( QPixmap(":StoreOff.png").scaled(statusLab_w, statusLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    }else {
        this->ui->Mod2Lable->setPixmap( QPixmap(":StoreDef.png").scaled(statusLab_w, statusLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    }

    //准备Eleme门店营业状态Label;
    this->ui->Eleme1Lable->setPixmap( QPixmap(":ElemeTitle.png").scaled(titleLab_w, titleLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    if(1 == eleStoreStatus){
        this->ui->Eleme2Lable->setPixmap( QPixmap(":StoreOn.png").scaled(statusLab_w, statusLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    }else if(2 == eleStoreStatus){
        this->ui->Eleme2Lable->setPixmap( QPixmap(":StoreOff.png").scaled(statusLab_w, statusLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    } else {
        this->ui->Eleme2Lable->setPixmap( QPixmap(":StoreDef.png").scaled(statusLab_w, statusLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    }

    //准备美团外卖 门店营业状态Label;
    this->ui->MtMOD1Lable->setPixmap( QPixmap(":MtTitle.png").scaled(titleLab_w, titleLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    if(1 == mtStoreStatus){
        this->ui->MtMOD2Lable->setPixmap( QPixmap(":StoreOn.png").scaled(statusLab_w, statusLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    } else if(2 == mtStoreStatus) {
        this->ui->MtMOD2Lable->setPixmap( QPixmap(":StoreOff.png").scaled(statusLab_w, statusLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    } else {
        this->ui->MtMOD2Lable->setPixmap( QPixmap(":StoreDef.png").scaled(statusLab_w, statusLab_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation) );
    }
}



void MiniStoreStatusForm::on_MiniStoreStatusCloseBtn_clicked()
{
    QLOG_INFO()<<"MiniStoreStatusForm::on_MiniStoreStatusCloseBtn_clicked -----------------> ";
    this->hide();
    return;
}
