#include "sdcmworklist.h"

#include "mainwindow.h"

#include <QDebug>

#include "charconvert.h"
#include <QTextCodec>

static void progressCallback(void* callbackData, T_DIMSE_C_FindRQ* request, int responseCount, T_DIMSE_C_FindRSP* rsp, DcmDataset* responseIdentifiers)
{

}

SDCMWorklist::SDCMWorklist()
{
    m_pOverrideKeys = new OFList<OFString>;


    //m_strIP = "10.10.16.28";
    //m_nPort = 104;
    m_strIP = "10.20.236.27";
    //m_nPort = 127;
    m_nPort = 128;

    m_strAETitle = "GI_TEST";
    m_strServerAETitle = "TY_WORK_2727";
    m_pAbstractSyntax = UID_FINDModalityWorklistInformationModel;
    m_NetworkTransferSyntax = EXS_LittleEndianExplicit; //EXS_Unknown
    //m_NetworkTransferSyntax = EXS_LittleEndianImplicit; //EXS_Unknown
    m_BlockMode = DIMSE_BLOCKING;

    m_DimseTimeout = 20;
    //m_nMaxReceivePDULength = ASC_DEFAULTMAXPDU;
    m_nMaxReceivePDULength = ASC_MAXIMUMPDUSIZE;
    m_bSecure = false;
    m_bAbortAssociation = false;
    m_nRetryCount = 5;
    //m_ExtractMode = FEM_dicomFile;
    m_ExtractMode = FEM_none;
    m_CancelAfterNResponses = -1;
    m_ACSETimeout = 20;
}

SDCMWorklist::~SDCMWorklist()
{
    if(m_pOverrideKeys!=NULL)
    {
        m_pOverrideKeys->clear();
        delete m_pOverrideKeys;
        m_pOverrideKeys = NULL;
    }
}

void SDCMWorklist::Init()
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    SEARCH_ITEM* pSearchItem = pCommonData->GetSearchItemWorklist();

    QTextCodec* codecKR = QTextCodec::codecForName("eucKR");

    QString strModality = pCommonData->GetModality();
    m_pOverrideKeys->clear();

    QString strDICOMCharacter = pCommonData->GetDICOMCharacterSet();
    if(strDICOMCharacter=="EUC-KR")
    {
        m_pOverrideKeys->push_back("0008,0005=ISO 2022 IR 149"); //DCM_SpecificCharacterSet
    }
    else if(strDICOMCharacter=="UTF-8")
    {
        m_pOverrideKeys->push_back("0008,0005=ISO_IR 192"); //DCM_SpecificCharacterSet
    }
    else if(strDICOMCharacter=="ASCII")
    {
        m_pOverrideKeys->push_back("0008,0005=ISO_IR 100"); //DCM_SpecificCharacterSet
    }


    m_pOverrideKeys->push_back("0008,0012"); //DCM_InstanceCreationDate
    m_pOverrideKeys->push_back("0008,0013"); //DCM_InstanceCreationTime
    m_pOverrideKeys->push_back("0008,0014"); //DCM_InstanceCreatorUID
    m_pOverrideKeys->push_back("0008,0016"); //DCM_SOPClassUID
    m_pOverrideKeys->push_back("0008,0018"); //DCM_SOPInstanceUID
    m_pOverrideKeys->push_back("0008,0050"); //DCM_AccessionNumber
    //m_pOverrideKeys->push_back("0008,0080"); //DCM_InstitutionName
    //m_pOverrideKeys->push_back("0008,0081"); //DCM_InstitutionAddress
    {
        //DCM_InstitutionCodeSequence
        //m_pOverrideKeys->push_back("(0008,0082)[0].(0008,0100)"); //DCM_CodeValue
        //m_pOverrideKeys->push_back("(0008,0082)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
        //m_pOverrideKeys->push_back("(0008,0082)[0].(0008,0103)"); //DCM_CodingSchemeVersion
        //m_pOverrideKeys->push_back("(0008,0082)[0].(0008,0104)"); //DCM_CodeMeaning
    }

    m_pOverrideKeys->push_back("0008,0090"); //DCM_ReferringPhysicianName
    //m_pOverrideKeys->push_back("0008,0092"); //DCM_ReferringPhysicianAddress
    //m_pOverrideKeys->push_back("0008,0094"); //DCM_ReferringPhysicianTelephoneNumbers

    {
        {
            //DCM_ReferringPhysicianIdentificationSequence --> DCM_InstitutionCodeSequence
            //m_pOverrideKeys->push_back("(0008,0096)[0].(0008,0082)[0].(0008,0100)"); //DCM_CodeValue
            //m_pOverrideKeys->push_back("(0008,0096)[0].(0008,0082)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
            //m_pOverrideKeys->push_back("(0008,0096)[0].(0008,0082)[0].(0008,0103)"); //DCM_CodingSchemeVersion
            //m_pOverrideKeys->push_back("(0008,0096)[0].(0008,0082)[0].(0008,0104)"); //DCM_CodeMeaning
        }

        {
            //DCM_ReferringPhysicianIdentificationSequence --> DCM_PersonIdentificationCodeSequence
            //m_pOverrideKeys->push_back("(0008,0096)[0].(0040,1101)[0].(0008,0100)"); //DCM_CodeValue
            //m_pOverrideKeys->push_back("(0008,0096)[0].(0040,1101)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
            //m_pOverrideKeys->push_back("(0008,0096)[0].(0040,1101)[0].(0008,0103)"); //DCM_CodingSchemeVersion
            //m_pOverrideKeys->push_back("(0008,0096)[0].(0040,1101)[0].(0008,0104)"); //DCM_CodeMeaning
        }

        //m_pOverrideKeys->push_back("(0008,0096)[0].(0040,1102)"); //DCM_PersonAddress
        //m_pOverrideKeys->push_back("(0008,0096)[0].(0040,1103)"); //DCM_PersonTelephoneNumbers
    }

    {
        //DCM_CodingSchemeIdentificationSequence
        //m_pOverrideKeys->push_back("(0008,0110)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
        //m_pOverrideKeys->push_back("(0008,0110)[0].(0008,0103)"); //DCM_CodingSchemeVersion
    }

    //m_pOverrideKeys->push_back("0008,0201"); //DCM_TimezoneOffsetFromUTC
    //m_pOverrideKeys->push_back("0008,1040"); //DCM_InstitutionalDepartmentName
    //m_pOverrideKeys->push_back("0008,1080"); //DCM_AdmittingDiagnosesDescription

    {
        //DCM_AdmittingDiagnosesCodeSequence
        //m_pOverrideKeys->push_back("(0008,1084)[0].(0008,0100)"); //DCM_CodeValue
        //m_pOverrideKeys->push_back("(0008,1084)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
        //m_pOverrideKeys->push_back("(0008,1084)[0].(0008,0103)"); //DCM_CodingSchemeVersion
        //m_pOverrideKeys->push_back("(0008,1084)[0].(0008,0104)"); //DCM_CodeMeaning
    }

    {
        //DCM_ReferencedStudySequence
        //m_pOverrideKeys->push_back("(0008,1110)[0].(0008,1150)"); //DCM_ReferencedSOPClassUID
        //m_pOverrideKeys->push_back("(0008,1110)[0].(0008,1155)"); //DCM_ReferencedSOPInstanceUID
    }

    {
        //DCM_ReferencedPatientSequence
        //m_pOverrideKeys->push_back("(0008,1120)[0].(0008,1150)"); //DCM_ReferencedSOPClassUID
        //m_pOverrideKeys->push_back("(0008,1120)[0].(0008,1155)"); //DCM_ReferencedSOPInstanceUID
    }

    {
        //DCM_ReferencedVisitSequence
        //m_pOverrideKeys->push_back("(0008,1125)[0].(0008,1150)"); //DCM_ReferencedSOPClassUID
        //m_pOverrideKeys->push_back("(0008,1125)[0].(0008,1155)"); //DCM_ReferencedSOPInstanceUID
    }

    {
        //m_pOverrideKeys->push_back("0010,0010"); //DCM_PatientName
        QString strPatientName = QString("0010,0010");
        if(pSearchItem->m_strPatientName.size()>0)
        {
            QString strPatientName = QString("0010,0010=%1").arg(pSearchItem->m_strPatientName);
            QByteArray strPatientNameKor = codecKR->fromUnicode(strPatientName);

            m_pOverrideKeys->push_back(strPatientNameKor.data());
        }
        else
        {
            m_pOverrideKeys->push_back(strPatientName.toStdString().c_str());
        }

    }

    {
        //m_pOverrideKeys->push_back("0010,0020"); //DCM_PatientID
        QString strPatientID = QString("0010,0020");
        if(pSearchItem->m_strPatientID.size()>0)
        {
            QString strPatientID = QString("0010,0020=%1").arg(pSearchItem->m_strPatientID);
            QByteArray strPatientIDKor = codecKR->fromUnicode(strPatientID);

            m_pOverrideKeys->push_back(strPatientIDKor.data());

        }
        else
        {
            m_pOverrideKeys->push_back(strPatientID.toStdString().c_str());
        }

    }


    m_pOverrideKeys->push_back("0010,0021"); //DCM_IssuerOfPatientID
    m_pOverrideKeys->push_back("0010,0030"); //DCM_PatientBirthDate
    //m_pOverrideKeys->push_back("0010,0032"); //DCM_PatientBirthTime
    m_pOverrideKeys->push_back("0010,0040"); //DCM_PatientSex
    {
        //DCM_PatientInsurancePlanCodeSequence
        //m_pOverrideKeys->push_back("(0010,0050)[0].(0008,0100)"); //DCM_CodeValue
        //m_pOverrideKeys->push_back("(0010,0050)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
        //m_pOverrideKeys->push_back("(0010,0050)[0].(0008,0103)"); //DCM_CodingSchemeVersion
        //m_pOverrideKeys->push_back("(0010,0050)[0].(0008,0104)"); //DCM_CodeMeaning
    }
    {
        //DCM_PatientPrimaryLanguageCodeSequence
        //m_pOverrideKeys->push_back("(0010,0101)[0].(0008,0100)"); //DCM_CodeValue
        //m_pOverrideKeys->push_back("(0010,0101)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
        //m_pOverrideKeys->push_back("(0010,0101)[0].(0008,0103)"); //DCM_CodingSchemeVersion
        //m_pOverrideKeys->push_back("(0010,0101)[0].(0008,0104)"); //DCM_CodeMeaning
        {
            //DCM_PatientPrimaryLanguageCodeSequence --> DCM_PatientPrimaryLanguageModifierCodeSequence
            //m_pOverrideKeys->push_back("(0010,0101)[0].(0010,0102)[0].(0008,0100)"); //DCM_CodeValue
            //m_pOverrideKeys->push_back("(0010,0101)[0].(0010,0102)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
            //m_pOverrideKeys->push_back("(0010,0101)[0].(0010,0102)[0].(0008,0103)"); //DCM_CodingSchemeVersion
            //m_pOverrideKeys->push_back("(0010,0101)[0].(0010,0102)[0].(0008,0104)"); //DCM_CodeMeaning
        }
    }

    //m_pOverrideKeys->push_back("0010,1000"); //DCM_RETIRED_OtherPatientIDs
    //m_pOverrideKeys->push_back("0010,1001"); //DCM_OtherPatientNames
    //m_pOverrideKeys->push_back("0010,1005"); //DCM_PatientBirthName
    m_pOverrideKeys->push_back("0010,1010"); //DCM_PatientAge
    m_pOverrideKeys->push_back("0010,1020"); //DCM_PatientSize
    m_pOverrideKeys->push_back("0010,1030"); //DCM_PatientWeight
    m_pOverrideKeys->push_back("0010,1040"); //DCM_PatientAddress
    //m_pOverrideKeys->push_back("0010,1060"); //DCM_PatientMotherBirthName
    //m_pOverrideKeys->push_back("0010,1080"); //DCM_MilitaryRank
    //m_pOverrideKeys->push_back("0010,1081"); //DCM_BranchOfService
    //m_pOverrideKeys->push_back("0010,1090"); //DCM_RETIRED_MedicalRecordLocator
    m_pOverrideKeys->push_back("0010,2000"); //DCM_MedicalAlerts
    m_pOverrideKeys->push_back("0010,2110"); //DCM_Allergies
    //m_pOverrideKeys->push_back("0010,2150"); //DCM_CountryOfResidence
    //m_pOverrideKeys->push_back("0010,2152"); //DCM_RegionOfResidence
    //m_pOverrideKeys->push_back("0010,2154"); //DCM_PatientTelephoneNumbers
    //m_pOverrideKeys->push_back("0010,2160"); //DCM_EthnicGroup
    //m_pOverrideKeys->push_back("0010,2180"); //DCM_Occupation
    m_pOverrideKeys->push_back("0010,21a0"); //DCM_SmokingStatus
    //m_pOverrideKeys->push_back("0010,21b0"); //DCM_AdditionalPatientHistory
    m_pOverrideKeys->push_back("0010,21c0"); //DCM_PregnancyStatus
    //m_pOverrideKeys->push_back("0010,21d0"); //DCM_LastMenstrualDate
    //m_pOverrideKeys->push_back("0010,21f0"); //DCM_PatientReligiousPreference
    m_pOverrideKeys->push_back("0010,4000"); //DCM_PatientComments

    m_pOverrideKeys->push_back("0020,000d"); //DCM_StudyInstanceUID

    m_pOverrideKeys->push_back("0038,0300"); //DCM_CurrentPatientLocation

    /*
    {
        //DCM_ContributingEquipmentSequence
        m_pOverrideKeys->push_back("(0018,a001)[0].(0008,0070)"); //DCM_Manufacturer
        m_pOverrideKeys->push_back("(0018,a001)[0].(0008,0080)"); //DCM_InstitutionName
        m_pOverrideKeys->push_back("(0018,a001)[0].(0008,0081)"); //DCM_InstitutionAddress
        m_pOverrideKeys->push_back("(0018,a001)[0].(0008,1010)"); //DCM_StationName
        m_pOverrideKeys->push_back("(0018,a001)[0].(0008,1040)"); //DCM_InstitutionalDepartmentName
        m_pOverrideKeys->push_back("(0018,a001)[0].(0008,1090)"); //DCM_ManufacturerModelName
        m_pOverrideKeys->push_back("(0018,a001)[0].(0018,1000)"); //DCM_DeviceSerialNumber
        m_pOverrideKeys->push_back("(0018,a001)[0].(0018,1020)"); //DCM_SoftwareVersions
        m_pOverrideKeys->push_back("(0018,a001)[0].(0018,1050)"); //DCM_SpatialResolution
        m_pOverrideKeys->push_back("(0018,a001)[0].(0018,1090)"); //DCM_CardiacNumberOfImages
        m_pOverrideKeys->push_back("(0018,a001)[0].(0018,1200)"); //DCM_DateOfLastCalibration
        m_pOverrideKeys->push_back("(0018,a001)[0].(0018,1201)"); //DCM_TimeOfLastCalibration
        m_pOverrideKeys->push_back("(0018,a001)[0].(0018,a002)"); //DCM_ContributionDateTime
        m_pOverrideKeys->push_back("(0018,a001)[0].(0018,a003)"); //DCM_ContributionDescription

        {
            //DCM_ContributingEquipmentSequence --> DCM_PurposeOfReferenceCodeSequence
            m_pOverrideKeys->push_back("(0018,a001)[0].(0040,a170)[0].(0008,0100)"); //DCM_CodeValue
            m_pOverrideKeys->push_back("(0018,a001)[0].(0040,a170)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
            m_pOverrideKeys->push_back("(0018,a001)[0].(0040,a170)[0].(0008,0103)"); //DCM_CodingSchemeVersion
            m_pOverrideKeys->push_back("(0018,a001)[0].(0040,a170)[0].(0008,0104)"); //DCM_CodeMeaning
        }
    }

    m_pOverrideKeys->push_back("0020,000d"); //DCM_StudyInstanceUID
    m_pOverrideKeys->push_back("0020,0013"); //DCM_InstanceNumber


    {
        //DCM_RequestingPhysicianIdentificationSequence
        m_pOverrideKeys->push_back("(0032,1031)[0].(0008,0080)"); //DCM_InstitutionName
        m_pOverrideKeys->push_back("(0032,1031)[0].(0008,0081)"); //DCM_InstitutionAddress

        {
            //DCM_RequestingPhysicianIdentificationSequence --> DCM_InstitutionCodeSequence
            m_pOverrideKeys->push_back("(0032,1031)[0].(0008,0082)[0].(0008,0100)"); //DCM_CodeValue
            m_pOverrideKeys->push_back("(0032,1031)[0].(0008,0082)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
            m_pOverrideKeys->push_back("(0032,1031)[0].(0008,0082)[0].(0008,0103)"); //DCM_CodingSchemeVersion
            m_pOverrideKeys->push_back("(0032,1031)[0].(0008,0082)[0].(0008,0104)"); //DCM_CodeMeaning
        }
        {
            //DCM_RequestingPhysicianIdentificationSequence --> DCM_PersonIdentificationCodeSequence
            m_pOverrideKeys->push_back("(0032,1031)[0].(0040,1101)[0].(0008,0100)"); //DCM_CodeValue
            m_pOverrideKeys->push_back("(0032,1031)[0].(0040,1101)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
            m_pOverrideKeys->push_back("(0032,1031)[0].(0040,1101)[0].(0008,0103)"); //DCM_CodingSchemeVersion
            m_pOverrideKeys->push_back("(0032,1031)[0].(0040,1101)[0].(0008,0104)"); //DCM_CodeMeaning
        }

        m_pOverrideKeys->push_back("(0032,1031)[0].(0040,1102)"); //DCM_PersonAddress
        m_pOverrideKeys->push_back("(0032,1031)[0].(0040,1103)"); //DCM_PersonTelephoneNumbers
    }


    m_pOverrideKeys->push_back("0032,1032"); //DCM_RequestingPhysician
    m_pOverrideKeys->push_back("0032,1033"); //DCM_RequestingService
    m_pOverrideKeys->push_back("0032,1060"); //DCM_RequestedProcedureDescription


    {
        //DCM_RequestedProcedureCodeSequence
        m_pOverrideKeys->push_back("(0032,1064)[0].(0008,0100)"); //DCM_CodeValue
        m_pOverrideKeys->push_back("(0032,1064)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
        m_pOverrideKeys->push_back("(0032,1064)[0].(0008,0103)"); //DCM_CodingSchemeVersion
        m_pOverrideKeys->push_back("(0032,1064)[0].(0008,0104)"); //DCM_CodeMeaning
    }

    {
        //DCM_ReferencedPatientAliasSequence
        m_pOverrideKeys->push_back("(0038,0004)[0].(0008,1150)"); //DCM_ReferencedSOPClassUID
        m_pOverrideKeys->push_back("(0038,0004)[0].(0008,1155)"); //DCM_ReferencedSOPInstanceUID
    }

    m_pOverrideKeys->push_back("0038,0008"); //DCM_VisitStatusID
    m_pOverrideKeys->push_back("0038,0010"); //DCM_AdmissionID
    m_pOverrideKeys->push_back("0038,0011"); //DCM_RETIRED_IssuerOfAdmissionID
    m_pOverrideKeys->push_back("0038,0016"); //DCM_RouteOfAdmissions
    m_pOverrideKeys->push_back("0038,0020"); //DCM_AdmittingDate
    m_pOverrideKeys->push_back("0038,0021"); //DCM_AdmittingTime
    m_pOverrideKeys->push_back("0038,0050"); //DCM_SpecialNeeds

    m_pOverrideKeys->push_back("0038,0400"); //DCM_PatientInstitutionResidence
    m_pOverrideKeys->push_back("0038,0500"); //DCM_PatientState
    m_pOverrideKeys->push_back("0038,4000"); //DCM_VisitComments
    */


    {
        //DCM_ScheduledProcedureStepSequence
        QString strSearch = QString("(0040,0100)[0].0008,0060=%1").arg(strModality);
        m_pOverrideKeys->push_back(strSearch.toStdString().c_str()); //DCM_Modality
        //m_pOverrideKeys->push_back("(0040,0100)[0].0008,0060"); //DCM_Modality
        m_pOverrideKeys->push_back("(0040,0100)[0].(0032,1070)"); //DCM_RequestedContrastAgent
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0011)"); //DCM_ScheduledProcedureStepLocation
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0001)"); //DCM_ScheduledStationAETitle
        {
            QString strDateStart;
            QString strDateEnd;



            if(pSearchItem->m_strStudyDate.size()==21)
            {
                QString strTmp = pSearchItem->m_strStudyDate;
                strDateStart = QString("%1%2%3").arg(strTmp.mid(0, 4)).
                        arg(strTmp.mid(5, 2)).
                        arg(strTmp.mid(8, 2));

                strDateEnd = QString("%1%2%3").arg(strTmp.mid(11, 4)).
                        arg(strTmp.mid(16, 2)).
                        arg(strTmp.mid(19, 2));
            }
            else
            {
                QDate date = QDate::currentDate();
                int nYear = date.year();
                int nMonth = date.month();
                int nDay = date.day();
                strDateStart = QString("%1%2%3").arg(QString::number(nYear).rightJustified(4, '0')).arg(QString::number(nMonth).rightJustified(2, '0')).arg(QString::number(nDay).rightJustified(2, '0'));
                strDateEnd = strDateStart;
            }

            //QString strSearchDateTmp = QString("0040,0002=%1-%2").arg(strDateStart).arg(strDateEnd);
            QString strSearchDateTmp = QString("(0040,0100)[0].(0040,0002)=%1-%2").arg(strDateStart).arg(strDateEnd);


            OFString strSearchDate = strSearchDateTmp.toStdString().c_str();

            m_pOverrideKeys->push_back(strSearchDate);

        }

        //m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0002)"); //DCM_ScheduledProcedureStepStartDate


        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0003)"); //DCM_ScheduledProcedureStepStartTime
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0004)"); //DCM_ScheduledProcedureStepEndDate
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0005)"); //DCM_ScheduledProcedureStepEndTime
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0006)"); //DCM_ScheduledPerformingPhysicianName
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0007)"); //DCM_ScheduledProcedureStepDescription
        {
            //DCM_ScheduledProcedureStepSequence --> DCM_ScheduledProtocolCodeSequence
            m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0008)[0].(0008,0100)"); //DCM_CodeValue
            //m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0008)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
            //m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0008)[0].(0008,0103)"); //DCM_CodingSchemeVersion
            //m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0008)[0].(0008,0104)"); //DCM_CodeMeaning
        }

        /*
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0009)"); //DCM_ScheduledProcedureStepID
        {

            //DCM_ScheduledProcedureStepSequence --> DCM_ScheduledPerformingPhysicianIdentificationSequence
            m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0008,0080)"); //DCM_InstitutionName
            m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0008,0081)"); //DCM_InstitutionAddress
            {
                //DCM_ScheduledProcedureStepSequence --> DCM_ScheduledPerformingPhysicianIdentificationSequence --> DCM_InstitutionCodeSequence
                m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0008,0082)[0].(0008,0100)"); //DCM_CodeValue
                m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0008,0082)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
                m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0008,0082)[0].(0008,0103)"); //DCM_CodingSchemeVersion
                m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0008,0082)[0].(0008,0104)"); //DCM_CodeMeaning
            }
            {
                //DCM_ScheduledProcedureStepSequence --> DCM_ScheduledPerformingPhysicianIdentificationSequence --> DCM_InstitutionCodeSequence
                m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0040,1101)[0].(0008,0100)"); //DCM_CodeValue
                m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0040,1101)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
                m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0040,1101)[0].(0008,0103)"); //DCM_CodingSchemeVersion
                m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0040,1101)[0].(0008,0104)"); //DCM_CodeMeaning
            }
            m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0040,1102)"); //DCM_PersonAddress
            m_pOverrideKeys->push_back("(0040,0100)[0].(0040,000b)[0].(0040,1103)"); //DCM_PersonTelephoneNumbers            
        }
        */

        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0010)"); //DCM_ScheduledStationName
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0011)"); //DCM_ScheduledProcedureStepLocation
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0012)"); //DCM_PreMedication
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0020)"); //DCM_ScheduledProcedureStepStatus
        m_pOverrideKeys->push_back("(0040,0100)[0].(0040,0400)"); //DCM_CommentsOnTheScheduledProcedureStep
    }


    m_pOverrideKeys->push_back("0040,1001"); //DCM_RequestedProcedureID
    m_pOverrideKeys->push_back("0040,1002"); //DCM_ReasonForTheRequestedProcedure
    m_pOverrideKeys->push_back("0040,1003"); //DCM_RequestedProcedurePriority
    m_pOverrideKeys->push_back("0040,1004"); //DCM_PatientTransportArrangements
    m_pOverrideKeys->push_back("0040,1005"); //DCM_RequestedProcedureLocation
    m_pOverrideKeys->push_back("0040,1008"); //DCM_ConfidentialityCode
    m_pOverrideKeys->push_back("0040,1009"); //DCM_ReportingPriority
    m_pOverrideKeys->push_back("0040,1010"); //DCM_NamesOfIntendedRecipientsOfResults


    /*
    {
        //DCM_IntendedRecipientsOfResultsIdentificationSequence
        m_pOverrideKeys->push_back("(0040,1011)[0].(0008,0080)"); //DCM_InstitutionName
        m_pOverrideKeys->push_back("(0040,1011)[0].(0008,0081)"); //DCM_InstitutionAddress
        {
            //DCM_IntendedRecipientsOfResultsIdentificationSequence --> DCM_InstitutionCodeSequence
            m_pOverrideKeys->push_back("(0040,1011)[0].(0008,0082)[0].(0008,0100)"); //DCM_CodeValue
            m_pOverrideKeys->push_back("(0040,1011)[0].(0008,0082)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
            m_pOverrideKeys->push_back("(0040,1011)[0].(0008,0082)[0].(0008,0103)"); //DCM_CodingSchemeVersion
            m_pOverrideKeys->push_back("(0040,1011)[0].(0008,0082)[0].(0008,0104)"); //DCM_CodeMeaning
        }
        {
            //DCM_IntendedRecipientsOfResultsIdentificationSequence --> DCM_PersonIdentificationCodeSequence
            m_pOverrideKeys->push_back("(0040,1011)[0].(0040,1101)[0].(0008,0100)"); //DCM_CodeValue
            m_pOverrideKeys->push_back("(0040,1011)[0].(0040,1101)[0].(0008,0102)"); //DCM_CodingSchemeDesignator
            m_pOverrideKeys->push_back("(0040,1011)[0].(0040,1101)[0].(0008,0103)"); //DCM_CodingSchemeVersion
            m_pOverrideKeys->push_back("(0040,1011)[0].(0040,1101)[0].(0008,0104)"); //DCM_CodeMeaning
        }
        m_pOverrideKeys->push_back("(0040,1011)[0].(0040,1102)"); //DCM_PersonAddress
        m_pOverrideKeys->push_back("(0040,1011)[0].(0040,1103)"); //DCM_PersonTelephoneNumbers
    }

    m_pOverrideKeys->push_back("0040,1400"); //DCM_RequestedProcedureComments
    m_pOverrideKeys->push_back("0040,2001"); //DCM_RETIRED_ReasonForTheImagingServiceRequest
    m_pOverrideKeys->push_back("0040,2004"); //DCM_IssueDateOfImagingServiceRequest
    m_pOverrideKeys->push_back("0040,2005"); //DCM_IssueTimeOfImagingServiceRequest
    m_pOverrideKeys->push_back("0040,2008"); //DCM_OrderEnteredBy
    m_pOverrideKeys->push_back("0040,2009"); //DCM_OrderEntererLocation
    m_pOverrideKeys->push_back("0040,2010"); //DCM_OrderCallbackPhoneNumber
    m_pOverrideKeys->push_back("0040,2016"); //DCM_PlacerOrderNumberImagingServiceRequest
    m_pOverrideKeys->push_back("0040,2017"); //DCM_FillerOrderNumberImagingServiceRequest
    m_pOverrideKeys->push_back("0040,2400"); //DCM_ImagingServiceRequestComments
    m_pOverrideKeys->push_back("0040,3001"); //DCM_ConfidentialityConstraintOnPatientDataDescription


    m_pOverrideKeys->push_back("0100,0410"); //DCM_SOPInstanceStatus
    m_pOverrideKeys->push_back("0100,0420"); //DCM_SOPAuthorizationDateTime
    m_pOverrideKeys->push_back("0100,0424"); //DCM_SOPAuthorizationComment
    m_pOverrideKeys->push_back("0100,0426"); //DCM_AuthorizationEquipmentCertificationNumber

    {
        //DCM_EncryptedAttributesSequence
        m_pOverrideKeys->push_back("(0400,0500)[0].(0400,0510)"); //DCM_EncryptedContentTransferSyntaxUID
        m_pOverrideKeys->push_back("(0400,0500)[0].(0400,0520)"); //DCM_EncryptedContent
    }

    {
        //DCM_MACParametersSequence
        m_pOverrideKeys->push_back("(4ffe,0001)[0].(0400,0005)"); //DCM_MACIDNumber
        m_pOverrideKeys->push_back("(4ffe,0001)[0].(0400,0010)"); //DCM_MACCalculationTransferSyntaxUID
        m_pOverrideKeys->push_back("(4ffe,0001)[0].(0400,0015)"); //DCM_MACAlgorithm
        m_pOverrideKeys->push_back("(4ffe,0001)[0].(0400,0020)"); //DCM_DataElementsSigned
    }

    {
        //DCM_Item
        m_pOverrideKeys->push_back("(fffa,fffa)[0].(0400,0005)"); //DCM_MACIDNumber
        m_pOverrideKeys->push_back("(fffa,fffa)[0].(0400,0100)"); //DCM_DigitalSignatureUID
        m_pOverrideKeys->push_back("(fffa,fffa)[0].(0400,0105)"); //DCM_DigitalSignatureDateTime
        m_pOverrideKeys->push_back("(fffa,fffa)[0].(0400,0110)"); //DCM_CertificateType
        m_pOverrideKeys->push_back("(fffa,fffa)[0].(0400,0115)"); //DCM_CertificateOfSigner
        m_pOverrideKeys->push_back("(fffa,fffa)[0].(0400,0120)"); //DCM_Signature
        m_pOverrideKeys->push_back("(fffa,fffa)[0].(0400,0305)"); //DCM_CertifiedTimestampType
        m_pOverrideKeys->push_back("(fffa,fffa)[0].(0400,0310)"); //DCM_CertifiedTimestamp

    }
    */


}

int SDCMWorklist::Search()
{
    Init();
    MainWindow* pMainWindow = MainWindow::GetMainWindow();

    CommonData* pCommonData = MainWindow::GetCommonData();


    /*
    QString strDateStart;
    QString strDateEnd;

    if(pSearchItem->m_strStudyDate.size()==21)
    {
        QString strTmp = pSearchItem->m_strStudyDate;
        strDateStart = QString("%1%2%3").arg(strTmp.mid(0, 4)).
                arg(strTmp.mid(5, 2)).
                arg(strTmp.mid(8, 2));

        strDateEnd = QString("%1%2%3").arg(strTmp.mid(11, 4)).
                arg(strTmp.mid(16, 2)).
                arg(strTmp.mid(19, 2));
    }
    else
    {
        QDate date = QDate::currentDate();
        int nYear = date.year();
        int nMonth = date.month();
        int nDay = date.day();
        strDateStart = QString("%1%2%3").arg(QString::number(nYear).rightJustified(4, '0')).arg(QString::number(nMonth).rightJustified(2, '0')).arg(QString::number(nDay).rightJustified(2, '0'));
        strDateEnd = strDateStart;
    }

    //QString strSearchDateTmp = QString("0040,0002=%1-%2").arg(strDateStart).arg(strDateEnd);
    QString strSearchDateTmp = QString("(0040,0100)[0].(0040,0002)=%1-%2").arg(strDateStart).arg(strDateEnd);


    OFString strSearchDate = strSearchDateTmp.toStdString().c_str();

    m_pOverrideKeys->push_back(strSearchDate);

    OFString strOFModality;
    strOFModality = "0008,0060=SC";
    m_pOverrideKeys->push_back(strOFModality);
    */

    DCM_dcmnetLogger.setLogLevel(dcmtk::log4cplus::FATAL_LOG_LEVEL);

    OFCondition cond = m_FindSCU.initializeNetwork(m_ACSETimeout);

    SDCMFindSCUDataCallback* pCallback = pCommonData->GetFindSCUDataCallback();


    //m_FindSCU.performQuery("192.168.1.125", m_nPort, "TEST1", "DCMSERVER", m_pAbstractSyntax, m_NetworkTransferSyntax, m_BlockMode, m_DimseTimeout, m_nMaxReceivePDULength, m_bSecure, m_bAbortAssociation, m_nRetryCount, m_ExtractMode, m_CancelAfterNResponses,m_pOverrideKeys, (DcmFindSCUCallback*)pCallback);// NULL, NULL, NULL, NULL);

    //DcmFindSCUDefaultCallback* pNewCallback = reinterpret_cast<DcmFindSCUDefaultCallback*>(pCallback);
    //m_FindSCU.performQuery(m_strIP.c_str(), m_nPort, m_strAETitle.c_str(), m_strServerAETitle.c_str(), m_pAbstractSyntax, m_NetworkTransferSyntax, m_BlockMode, m_DimseTimeout, m_nMaxReceivePDULength, m_bSecure, m_bAbortAssociation, m_nRetryCount, m_ExtractMode, m_CancelAfterNResponses, m_pOverrideKeys, pNewCallback);//, NULL, NULL, NULL);

    //cond = m_FindSCU.performQuery(m_strIP.c_str(), m_nPort, m_strAETitle.c_str(), m_strServerAETitle.c_str(), m_pAbstractSyntax, m_NetworkTransferSyntax, m_BlockMode, m_DimseTimeout, m_nMaxReceivePDULength, m_bSecure, m_bAbortAssociation, m_nRetryCount, m_ExtractMode, m_CancelAfterNResponses, m_pOverrideKeys, (DcmFindSCUCallback*)pCallback);//, NULL, NULL, NULL);

    bool bComplete = false;
    int nTry = 0;
    while(bComplete==false && nTry < m_nRetryCount)
    {
        pCallback->Clear();
        const char* pStrIP = m_strIP.toStdString().c_str();
        cond = m_FindSCU.performQuery(m_strIP.toStdString().c_str(), m_nPort, m_strAETitle.toStdString().c_str(), m_strServerAETitle.toStdString().c_str(), m_pAbstractSyntax, m_NetworkTransferSyntax, m_BlockMode, m_DimseTimeout, m_nMaxReceivePDULength, m_bSecure, m_bAbortAssociation, 1, m_ExtractMode, m_CancelAfterNResponses, m_pOverrideKeys, (DcmFindSCUCallback*)pCallback);//, NULL, NULL, NULL);
        if(cond.good()==true)
        {
            bComplete = true;
            break;
        }
        nTry++;
    }


    m_FindSCU.dropNetwork();

    if(cond.good()==true)
    {
        return 0;
    }

    return -1;

}

DcmFindSCUExtractMode SDCMWorklist::GetExtractMode()
{
    return m_ExtractMode;
}

int SDCMWorklist::GetCancelAfterNResponses()
{
    return m_CancelAfterNResponses;
}

void SDCMWorklist::SetWorklistInfo(QString strIP, QString strPort, QString strServerAETitle, QString strClientAETitle)
{
    m_strIP = strIP;
    m_nPort = strPort.toInt();

    m_strAETitle = strClientAETitle;
    m_strServerAETitle = strServerAETitle;
}
