#include "dialogprogress.h"
#include "ui_dialogprogress.h"

#include "mainwindow.h"

DialogProgress::DialogProgress(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogProgress)
{
    ui->setupUi(this);

    m_bAutoRetryDICOMSend = false;

    //setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    //setAttribute(Qt::WA_TranslucentBackground);
    //setWindowFlags(Qt::CustomizeWindowHint);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog | Qt::Popup);

    ui->pushButton_Cancel->hide();
    ui->pushButton_Send->hide();

    m_bError = false;

    connect(&m_TimerDisplay, SIGNAL(timeout()), this, SLOT(OnTimerCalled()));
}

DialogProgress::~DialogProgress()
{
    disconnect(&m_TimerDisplay, SIGNAL(timeout()), this, SLOT(OnTimerCalled()));
    delete ui;
}


int DialogProgress::exec()
{
    int nRet = 0;

    //MainWindow* pMainWindow = MainWindow::GetMainWindow();

    //int nCheck = pMainWindow->CheckAliveWorklist();

    QString strText;

    //if(nCheck<1)
    {
        strText = ui->progressBar->text();
        //ui->label_Info->hide();

        //ChangeProgress(0);
    }

    //ui->pushButton_Cancel->hide();

    nRet = QDialog::exec();




    return nRet;
}




void DialogProgress::ChangeProgress(int nProgress)
{
    ui->progressBar->hide();
    ui->label_Info->show();
    ui->label_Info->setText("4 Images Send Complete!");
    //ui->label_Info->setText("4 Images Delete!");
    ui->pushButton_Cancel->setText("OK");
}

void DialogProgress::on_pushButton_Cancel_clicked()
{
    if(m_nType&0x10)
    {
        CommonData* pCommonData = MainWindow::GetCommonData();
        SThreadSendDICOM* pThreadSendDICOM = pCommonData->GetThreadSendDICOM();

        if(pThreadSendDICOM->GetCurrentState()&0x40)
        {
            pThreadSendDICOM->SetExitSendImageList();

            while(pThreadSendDICOM->IsExitSendImageList()==true)
            {
                usleep(10*1000);
            }

        }

    }
    if(m_TimerDisplay.isActive()==true)
    {
        m_TimerDisplay.stop();
    }
    done(QDialog::Rejected);
}


void DialogProgress::OnWorklistComplete()
{
    if(m_TimerDisplay.isActive()==true)
    {
        m_TimerDisplay.stop();
    }

    done(QDialog::Accepted);
}

void DialogProgress::OnWorklistError()
{
    DisplayInfo(-1);
    //if(m_TimerDisplay.isActive()==true)
    {
        //m_TimerDisplay.stop();
    }

    //done(QDialog::Accepted);
}

void DialogProgress::SendComplete()
{
    m_strSendInfo = QString("Send Complete!!");
    ui->pushButton_Cancel->show();
    ui->pushButton_Cancel->setText("OK");
    update();

    //QThread::msleep(1000);
    m_bSendComplete = true;
}

void DialogProgress::DisplayInfo(int nValue)
{
    if(nValue<0)
    {

        //m_TimerDisplay.stop();

        QString strError = "";

        if(nValue==-11)
        {
            strError = QString("Error: Patient Info");
            if(m_TimerDisplay.isActive()==true)
            {
                m_TimerDisplay.stop();
            }
        }
        else if(nValue==-12)
        {
            strError = QString("Error: Network Connection");
            if(m_TimerDisplay.isActive()==true)
            {
                m_TimerDisplay.stop();
            }
        }
        else
        {
            strError = QString("Check Server Info!! (%1)").arg(nValue);
        }

        ui->label_Info->setText(strError);
        ui->label_Info->show();
        ui->progressBar->setTextVisible(false);
        ui->pushButton_Cancel->setText("OK");
        ui->progressBar->setValue(0);

        ui->pushButton_Cancel->show();
        ui->pushButton_Cancel->update();

        QString strStyle = QString("QLabel \n"
        "{ \n"
        "   color: black; \n"
        "    font-family: 'Roboto'; \n"
        "   font-size: 18px; \n"
        "   font-weight: light; \n"
        "   padding-left: 0px; \n"
        "   padding-top: 0px; \n"
        //"   qproperty-backgroundVisible: false; \n"
        "   border: none; \n"
        //"   qproperty-verticalScrollBarPolicy: 'ScrollBarAlwaysOff'; \n"
        "   background: transparent; \n"
        "    qproperty-alignment: 'AlignCenter'; \n"
        "} \n");

        ui->label_Info->setStyleSheet(strStyle);


        m_bError = true;
    }
    else
    {
        QString strInfo = QString("Search...");
        ui->label_Info->setText(strInfo);
    }

}

void DialogProgress::DisplayInfo(QString strInfo)
{
    m_strSendInfo = strInfo;
}

void DialogProgress::DisplayInfo(int nValue, int nTotal)
{
    m_nSendCount = nValue;
    m_nSendTotal = nTotal;
}


void DialogProgress::Init(int nType)
{
    m_nSendCount = 0;
    m_nSendTotal = 0;
    m_bSendComplete = false;
    m_nType = nType;
    m_nCount = 0;
    //ui->label_Info->hide();
    ui->label_Info->setText("Connect to Server");

    ui->progressBar->show();
    ui->pushButton_Cancel->setText("Cancel");

    ui->progressBar->setTextVisible(false);

    QString strStyle = QString("QLabel \n"
    "{ \n"
    "    font-family: 'Roboto'; \n"
    "   font-size: 18px; \n"
    "   font-weight: light; \n"
    "   padding-left: 0px; \n"
    "   padding-top: 0px; \n"
    //"   qproperty-backgroundVisible: false; \n"
    "   border: none; \n"
    //"   qproperty-verticalScrollBarPolicy: 'ScrollBarAlwaysOff'; \n"
    "   background: transparent; \n"
    "    qproperty-alignment: 'AlignCenter'; \n"
    "} \n");

    ui->label_Info->setStyleSheet(strStyle);

    //if(nType==0)
    {
        m_TimerDisplay.setInterval(30);
        if(m_TimerDisplay.isActive()==false)
        {
            m_TimerDisplay.start(30);
        }

        if(nType&0x01)
        {
            ui->progressBar->setRange(0, 1);
            ui->progressBar->setValue(-1);
        }
        else
        {
            ui->progressBar->setRange(0, 100);
            ui->progressBar->setValue(100);
        }

        if(nType&0x02)
        {
            ui->label_Info->setText("Storage Clean");
        }

        if(nType&0x10)
        {
            //m_nType = 1;
            ui->pushButton_Cancel->show();
        }
        else
        {
            ui->pushButton_Cancel->hide();
        }

        if(nType&0x40)
        {
            ui->pushButton_Cancel->show();
            ui->pushButton_Cancel->setText("OK");

            if(nType&0x01)
            {
                ui->label_Info->setText("Error: Database");
            }
            else if(nType&0x02)
            {
                ui->label_Info->setText("Error: Database Lock");
            }
            else if(nType&0x04)
            {
                ui->label_Info->setText("Error: Image File");
            }
        }
    }

}

void DialogProgress::SetAutoRetryDICOMSend(bool bSet)
{
    m_bAutoRetryDICOMSend = bSet;
}


void DialogProgress::OnTimerCalled()
{
    if(m_nType==0)
    {
        m_nCount++;
        ui->progressBar->setValue(m_nCount%100);
    }
    else if(m_nType&0x01)
    {
        //DICOM Worklist, Storage
        if(m_nSendTotal>0)
        {
            ui->progressBar->setRange(0, m_nSendTotal);
        }

        if(m_nSendCount>0)
        {
            ui->progressBar->setValue(m_nSendCount);
            ui->label_Info->setText(m_strSendInfo);
        }
        else if(m_nSendCount<0)
        {
            int nValue = ui->progressBar->value();
            ui->progressBar->setValue(0);
            ui->label_Info->setText("Send Complete");
            m_bSendComplete = true;
        }

        if(m_bError==true)
        {
            ui->label_Info->setText("Failed: Send DICOM");
            m_TimerDisplay.stop();

            ui->pushButton_Cancel->setText("Close");

            if(m_bAutoRetryDICOMSend==true)
            {
                done(QDialog::Accepted);
            }
            else
            {
                ui->pushButton_Send->show();

                ui->pushButton_Cancel->setGeometry(360, 70, 130, 40);
                ui->pushButton_Send->setGeometry(180, 70, 130, 40);
            }

        }
        else
        {
            if(m_bSendComplete==true)
            {
                m_TimerDisplay.stop();

                CommonData* pCommonData = MainWindow::GetCommonData();
                if(pCommonData->IsAutoCloseSendFinish()==true)
                {
                    done(QDialog::Accepted);
                }
                else
                {
                    ui->pushButton_Cancel->setText("Close");
                    ui->pushButton_Cancel->setVisible(true);
                }
            }
        }
    }
    else if(m_nType&0x02)
    {
        ui->label_Info->setText("Storage Clean");
        m_nCount++;
        ui->progressBar->setValue(m_nCount%100);
    }

    /*
    else if(m_nType==3)
    {
        m_TimerDisplay.stop();

        ui->label_Info->setText("Error: Check Network Share Folder Setting!!!!");
        m_nCount++;
        ui->progressBar->setValue(0);
        ui->pushButton_Cancel->setText("Done");
        ui->pushButton_Cancel->show();
    }
    */

}


void DialogProgress::SetTypeMakeDICOM()
{
    m_nDICOMProgressType = 1;
}

void DialogProgress::SetTypeSendDICOM()
{
    m_nDICOMProgressType = 2;
}

void DialogProgress::keyReleaseEvent(QKeyEvent *event)
{
    int nKey = event->key();

    SThreadImageSave* pThread = MainWindow::GetCommonData()->GetThreadCaptureImage();

    if(nKey==Qt::Key_F10)
    {
        pThread->MissCapture();
    }
    else if(nKey==Qt::Key_F11)
    {
        pThread->MissCapture();
    }

}


void DialogProgress::reject()
{

}

void DialogProgress::on_pushButton_Send_clicked()
{
    done(QDialog::Accepted);
}


bool DialogProgress::IsError()
{
    return m_bError;
}
