#include "formsetting.h"
#include "ui_formsetting.h"

#include "dialogsettingtable.h"
#include "mainwindow.h"

#include "dialogsettingnetworkshare.h"

#include "dialogloginuseradd.h"

#include "dialogadvancedsetting.h"

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

    ui->comboBox_Modality->addItem("ES");
    ui->comboBox_Modality->addItem("SC");
    ui->comboBox_Modality->addItem("CR");
    ui->comboBox_Modality->addItem("DR");

    ui->comboBox_TransferSyntax->addItem("RAW");
    ui->comboBox_TransferSyntax->addItem("Lossless JPEG");

    ui->comboBox_Video->addItem("1080P");
    ui->comboBox_Video->addItem("720P");

    ui->comboBox_Modality->setEnabled(true);
    ui->comboBox_TransferSyntax->setEnabled(true);
}

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

void FormSetting::on_pushButton_Worklist_Setting_clicked()
{
    DialogSettingTable dlg(this);
    dlg.SetListType(SWIDGET_WORKLIST);

    MainWindow* pMainWindow = MainWindow::GetMainWindow();
    pMainWindow->StopScreenSaverTimer();
    dlg.exec();
    pMainWindow->ResetScreenSaverTimer();
    pMainWindow->StartScreenSaverTimer();
}

void FormSetting::on_pushButton_History_Setting_clicked()
{
    DialogSettingTable dlg(this);
    dlg.SetListType(SWIDGET_HISTORY);

    MainWindow* pMainWindow = MainWindow::GetMainWindow();
    pMainWindow->StopScreenSaverTimer();

    dlg.exec();

    pMainWindow->ResetScreenSaverTimer();
    pMainWindow->StartScreenSaverTimer();
}

void FormSetting::on_pushButton_Setting_Complete_clicked()
{
    QString strWorklistIP = ui->plainTextEdit_Worklist_IP->toPlainText();
    QString strWorklistPort = ui->plainTextEdit_Worklist_Port->toPlainText();
    QString strWorklistServerAETitle = ui->plainTextEdit_Worklist_ServerAETitle->toPlainText();
    QString strWorklistClientAETitle = ui->plainTextEdit_Worklist_ClientAETitle->toPlainText();

    QString strStorageIP = ui->plainTextEdit_Storage_IP->toPlainText();
    QString strStoragePort = ui->plainTextEdit_Storage_Port->toPlainText();
    QString strStorageServerAETitle = ui->plainTextEdit_Storage_ServerAETitle->toPlainText();
    QString strStorageClientAETitle = ui->plainTextEdit_Storage_ClientAETitle->toPlainText();

    QString strInstitutionName = ui->plainTextEdit_Hospital_Institution->toPlainText();
    QString strStationName = ui->plainTextEdit_Hospital_Station->toPlainText();

    int nTransferSyntax = ui->comboBox_TransferSyntax->currentIndex();
    int nModality = ui->comboBox_Modality->currentIndex();
    int nVideo = ui->comboBox_Video->currentIndex();
    nVideo = 0;



    int nWorklistPort = strWorklistPort.toInt();
    int nStoragePort = strStoragePort.toInt();

    CommonData* pCommon = MainWindow::GetCommonData();
    SDCMWorklist* pDCMWorklist = pCommon->GetWorklistClass();
    pDCMWorklist->SetWorklistInfo(strWorklistIP, strWorklistPort, strWorklistServerAETitle, strWorklistClientAETitle);





    {
        SDCM_SERVER_INFO* pInfoWorklist = pCommon->GetServerWorklistInfo();
        pInfoWorklist->m_nServer_Port = nWorklistPort;
        pInfoWorklist->m_strServer_IP = strWorklistIP;
        pInfoWorklist->m_strServer_AETitle = strWorklistServerAETitle;
        pInfoWorklist->m_strClient_AETitle = strWorklistClientAETitle;
        pCommon->ReleaseServerWorklistInfo();

        SDCM_SERVER_INFO* pInfoStorage = pCommon->GetServerStorageInfo();
        pInfoStorage->m_nServer_Port = nStoragePort;
        pInfoStorage->m_strServer_IP = strStorageIP;
        pInfoStorage->m_strServer_AETitle = strStorageServerAETitle;
        pInfoStorage->m_strClient_AETitle = strStorageClientAETitle;
        pCommon->ReleaseServerStorageInfo();
    }

    QString strModality;
    if(nModality == 0)
    {
        strModality="ES";
    }
    else if(nModality == 1)
    {
        strModality="SC";
    }
    else if(nModality == 2)
    {
        strModality="CR";
    }
    else if(nModality == 3)
    {
        strModality="DR";
    }

    pCommon->SetVideoMode(nVideo);

    pCommon->SetModality(strModality);
    pCommon->SetTransferSyntax(nTransferSyntax);

    pCommon->SetStationName(strStationName);
    pCommon->SetInstitutionName(strInstitutionName);


    pCommon->UpdateSetting();
    pCommon->UpdateAdvancedSetting();

}


void FormSetting::Init()
{
    CommonData* pCommon = MainWindow::GetCommonData();

    SDCM_SERVER_INFO* pInfoWorklist = pCommon->GetServerWorklistInfo();
    ui->plainTextEdit_Worklist_IP->setPlainText(pInfoWorklist->m_strServer_IP);
    if(pInfoWorklist->m_nServer_Port>0)
    {
        ui->plainTextEdit_Worklist_Port->setPlainText(QString::number(pInfoWorklist->m_nServer_Port));
    }

    ui->plainTextEdit_Worklist_ServerAETitle->setPlainText(pInfoWorklist->m_strServer_AETitle);
    ui->plainTextEdit_Worklist_ClientAETitle->setPlainText(pInfoWorklist->m_strClient_AETitle);
    pCommon->ReleaseServerWorklistInfo();

    SDCM_SERVER_INFO* pInfoStorage = pCommon->GetServerStorageInfo();
    ui->plainTextEdit_Storage_IP->setPlainText(pInfoStorage->m_strServer_IP);

    if(pInfoStorage->m_nServer_Port>0)
    {
        ui->plainTextEdit_Storage_Port->setPlainText(QString::number(pInfoStorage->m_nServer_Port));
    }

    ui->plainTextEdit_Storage_ServerAETitle->setPlainText(pInfoStorage->m_strServer_AETitle);
    ui->plainTextEdit_Storage_ClientAETitle->setPlainText(pInfoStorage->m_strClient_AETitle);
    pCommon->ReleaseServerStorageInfo();

    ui->plainTextEdit_Hospital_Institution->setPlainText(pCommon->GetInstitutionName());
    ui->plainTextEdit_Hospital_Station->setPlainText(pCommon->GetStationName());

    int nTransferSyntax = pCommon->GetTransferSyntax();
    QString strModality = pCommon->GetModality();
    int nVideoMode = pCommon->GetVideoMode();
    int nModality = 0;

    if(strModality=="ES")
    {
        nModality = 0;
    }
    else if(strModality=="SC")
    {
        nModality = 1;
    }
    else if(strModality=="CR")
    {
        nModality = 2;
    }
    else if(strModality=="DR")
    {
        nModality = 3;
    }

    nVideoMode = 0;

    ui->comboBox_TransferSyntax->setCurrentIndex(nTransferSyntax);
    ui->comboBox_Modality->setCurrentIndex(nModality);
    ui->comboBox_Video->setCurrentIndex(nVideoMode);

    {
        pCommon->ComputeStorageSize();
        int nStorageSize = pCommon->GetStorageSize();
        int nStorageFreeSize = pCommon->GetStorageFreeSize();

        double fStorageSize = (double)nStorageSize / 1000.0;
        double fStorageFreeSize = (double)nStorageFreeSize / 1000.0;


        QString strStorageSize = QString("Total: %1 GB").arg(fStorageSize, 3, 'f', 1);
        QString strStorageFreeSize = QString("Free: %1 GB").arg(fStorageFreeSize, 3, 'f', 1);
        QString strStorageUsedSize = QString("Used: %1 GB").arg(fStorageSize - fStorageFreeSize, 3, 'f', 1);

        ui->label_LocalStorage_Free->setText(strStorageFreeSize);
        ui->label_LocalStorage_Total->setText(strStorageSize);
        ui->label_LocalStorage_Used->setText(strStorageUsedSize);
    }
}

void FormSetting::on_pushButton_NetworkShareInfo_clicked()
{
    DialogSettingNetworkShare dlg(this);
    MainWindow* pMainWindow = MainWindow::GetMainWindow();
    pMainWindow->StopScreenSaverTimer();

    int nRet = dlg.exec();
    if(nRet==QDialog::Accepted)
    {
        QString strIP = dlg.GetIP();
        QString strID = dlg.GetID();
        QString strPassword = dlg.GetPassword();
        QString strShareFolder = dlg.GetShareFolder();
        QString strVersion = dlg.GetSMBVersion();


        bool bEnable = dlg.IsEnabledNetworkShare();
        bool bAutoSend = dlg.IsNetworkAutoSend();

        CommonData* pCommonData = MainWindow::GetCommonData();
        NetworkShareInfo* pNetworkShareInfo = pCommonData->GetNetworkShareInfo();

        pNetworkShareInfo->m_strIP = strIP;
        pNetworkShareInfo->m_strID = strID;
        pNetworkShareInfo->m_strPassword = strPassword;
        pNetworkShareInfo->m_strShareFolder = strShareFolder;
        pNetworkShareInfo->m_strVersion = strVersion;
        pNetworkShareInfo->m_bEnable = bEnable;
        pNetworkShareInfo->m_bAutoSend = bAutoSend;

        pCommonData->UpdateAdvancedSetting();
    }

    pMainWindow->ResetScreenSaverTimer();
    pMainWindow->StartScreenSaverTimer();

}

void FormSetting::on_pushButton_AddLoginUser_clicked()
{
    DialogLoginUserAdd dlg(this);

    MainWindow* pMainWindow = MainWindow::GetMainWindow();
    pMainWindow->StopScreenSaverTimer();

    dlg.exec();

    pMainWindow->ResetScreenSaverTimer();
    pMainWindow->StartScreenSaverTimer();
}

void FormSetting::on_pushButton_Advanced_clicked()
{
    MainWindow* pMainWindow = MainWindow::GetMainWindow();
    pMainWindow->StopScreenSaverTimer();

    DialogAdvancedSetting dlg(this);
    int nRet = dlg.exec();

    if(nRet==QDialog::Accepted)
    {
        bool bIsAutoVideoCapture = dlg.IsAutoVideoCapture();
        bool bIsAutoCaptureSendDICOM = dlg.IsAutoCaptureSendDICOM();
        bool bIsAutoExitCaptureSendDICOM = dlg.IsAutoExitCaptureSendDICOM();
        bool bIsAutoCloseSendFinish = dlg.IsAutoCloseSendFinish();
        bool bIsAutoLogin = dlg.IsAutoLogin();
        bool bIsCropMode = dlg.IsCropMode();
        bool bIsUseFixWidthLevel = dlg.IsUseFixWidthLevel();
        bool bIsUseLogLevel = dlg.IsUseLogLevel();
        int nScreenSaverTime = dlg.GetScreenSaverTime();
        bool bIsUseUSBBackup = dlg.IsUseUSBBackup();

        QString strDICOMCharacterSet = dlg.GetDICOM_CharacterSet();

        CommonData* pCommonData = MainWindow::GetCommonData();
        pCommonData->SetAutoVideoCapture(bIsAutoVideoCapture);
        pCommonData->SetAutoCaptureSendDICOM(bIsAutoCaptureSendDICOM);
        pCommonData->SetAutoExitCaptureSendDICOM(bIsAutoExitCaptureSendDICOM);
        pCommonData->SetAutoCloseSendComplete(bIsAutoCloseSendFinish);
        pCommonData->SetDICOMCharacterSet(strDICOMCharacterSet);
        pCommonData->SetAutoLogin(bIsAutoLogin);
        pCommonData->SetCropMode((int)bIsCropMode);
        pCommonData->SetUseFixWidthLevel((int)bIsUseFixWidthLevel);
        pCommonData->SetLogLevel((int)bIsUseLogLevel);
        pCommonData->SetScreenSaverTime(nScreenSaverTime);
        pCommonData->SetUseUSBBackup((int)bIsUseUSBBackup);



        pCommonData->UpdateAdvancedSetting();

    }

    pMainWindow->ResetScreenSaverTimer();
    pMainWindow->StartScreenSaverTimer();
}

