#include "stableheader.h"

#include "SDCMCommonStructure.h"

map<int, TableHeader*> STableHeader::m_MapHeaderWorklist;
map<int, TableHeader*> STableHeader::m_MapHeaderHistory;

STableHeader::STableHeader()
{
    /*
    QStringList strList;
    strList << "ID" << "Name" << "Sex" << "Weight" << "BirthDate" << "Age" << "Location" << "Study Instance UID" << "Study Description" << "Study Date" << "Study Create Date" << "Refer Doctor" << "Series Instance UID" << "Operator" << "Modality" << "Bodypart" << "Instance UID" << "Instance Create Date" << "Instance Create Time" << "Scheduled Date" << "Scheduled Time" << "Procedure Step ID" << "Procedure ID" << "Requested Comments" << "Index" << "Image Count";

    int i=0;
    for(i=0 ; i<strList.size() ; i++)
    {
        TableHeader* pHeader = new TableHeader;
        pHeader->m_nIndex = i;
        pHeader->m_nIndexDefault = i;
        pHeader->m_strText = strList[i];
        pHeader->m_bSelect = false;
        pHeader->m_nWidth = 150;
        if(i==2 || i==3)
        {
            pHeader->m_nWidth = 80;
        }
        m_MapHeaderWorklist[i] = pHeader;

        //m_ListHeader.push_back(pHeader);
    }

    for(i=0 ; i<m_MapHeaderWorklist.size() ; i++)
    {
        TableHeader* pHeader = m_MapHeaderWorklist[i];

        if(i<8)
        {
            m_ListActive.push_back(pHeader);
        }
        else
        {
            m_ListInactive.push_back(pHeader);
        }

    }
    */
}

STableHeader::~STableHeader()
{
    //ClearMapData();
    m_ListActive.clear();
    m_ListInactive.clear();
}

void STableHeader::ClearMapData()
{
    ClearMapDataWorklist();
    ClearMapDataHistory();
}

void STableHeader::ClearMapDataWorklist()
{
    int i=0;
    for(i=0 ; i<m_MapHeaderWorklist.size() ; i++)
    {
        TableHeader* pHeader = m_MapHeaderWorklist[i];
        SAFE_DELETE(pHeader);
    }
    m_MapHeaderWorklist.clear();
}

void STableHeader::ClearMapDataHistory()
{
    int i=0;
    for(i=0 ; i<m_MapHeaderHistory.size() ; i++)
    {
        TableHeader* pHeader = m_MapHeaderHistory[i];
        SAFE_DELETE(pHeader);
    }
    m_MapHeaderHistory.clear();
}

vector<TableHeader*>* STableHeader::GetListActive()
{
    return &m_ListActive;
}

vector<TableHeader*>* STableHeader::GetListInactive()
{
    return &m_ListInactive;
}


void STableHeader::UpdateTableHeaderInfo(map<int, int>& nMapData)
{
    int i=0;

    m_ListActive.clear();
    m_ListInactive.clear();


    map<int, int> nTmp;
    for(i=0 ; i<m_MapHeaderWorklist.size() ; i++)
    {
        nTmp[i] = i;
    }

    for(i=0 ; i<nMapData.size() ; i++)
    {
        int nIndex = nMapData[i];
        TableHeader* pHeader = m_MapHeaderWorklist[nIndex];

        m_ListActive.push_back(pHeader);

        nTmp.erase(nIndex);
    }

    int nCountActive = m_ListActive.size();

    map<int, int>::iterator it;
    for(it=nTmp.begin() ; it!=nTmp.end() ; ++it)
    {
        int nIndex = it->second;
        TableHeader* pHeader = m_MapHeaderWorklist[nIndex];

        m_ListInactive.push_back(pHeader);
    }



}


void STableHeader::CopyWorklistFrom(const STableHeader* pTableHeader)
{
    int i=0;
    m_ListActive.clear();
    m_ListInactive.clear();

    for(i=0 ; i<pTableHeader->m_ListActive.size() ; i++)
    {
        TableHeader* pHeader =pTableHeader->m_ListActive[i];
        //int nIndex = pHeader->m_nIndex;
        int nIndex = pHeader->m_nIndexDefault;

        //pHeader = m_MapHeaderWorklist[nIndex];
        m_ListActive.push_back(pHeader);
    }

    for(i=0 ; i<pTableHeader->m_ListInactive.size() ; i++)
    {
        TableHeader* pHeader =pTableHeader->m_ListInactive[i];
        //int nIndex = pHeader->m_nIndex;
        int nIndex = pHeader->m_nIndexDefault;

        //pHeader = m_MapHeaderWorklist[nIndex];
        m_ListInactive.push_back(pHeader);
    }
}

void STableHeader::CopyHistoryFrom(const STableHeader* pTableHeader)
{
    int i=0;
    m_ListActive.clear();
    m_ListInactive.clear();

    for(i=0 ; i<pTableHeader->m_ListActive.size() ; i++)
    {
        TableHeader* pHeader =pTableHeader->m_ListActive[i];
        int nIndex = pHeader->m_nIndex;

        //pHeader = m_MapHeaderHistory[nIndex];
        m_ListActive.push_back(pHeader);
    }

    for(i=0 ; i<pTableHeader->m_ListInactive.size() ; i++)
    {
        TableHeader* pHeader =pTableHeader->m_ListInactive[i];
        int nIndex = pHeader->m_nIndex;

        //pHeader = m_MapHeaderHistory[nIndex];
        m_ListInactive.push_back(pHeader);
    }
}


void STableHeader::SetDataWorklist(const QList<QStringList>& resultQuery)
{
    ClearMapDataWorklist();
    int i=0;
    m_ListActive.clear();
    m_ListInactive.clear();
    for(i=0 ; i<resultQuery.size() ; i++)
    {
        const QStringList& listData = resultQuery[i];
        QString strDefaultIndex = listData[0];
        QString strDisplayIndex = listData[1];
        QString strValueString = listData[2];
        QString strVisible = listData[3];
        QString strDisplayWidth = listData[4];

        int nDefaultIndex = strDefaultIndex.toInt();
        int nDisplayIndex = strDisplayIndex.toInt();
        int nVisible = strVisible.toInt();
        int nDisplayWidth = strDisplayWidth.toInt();

        TableHeader* pHeader = new TableHeader;
        pHeader->m_nIndex = nDisplayIndex;
        pHeader->m_nIndexDefault = nDefaultIndex;
        pHeader->m_strText = strValueString;
        pHeader->m_bSelect = false;
        pHeader->m_nWidth = nDisplayWidth;

        m_MapHeaderWorklist[i] = pHeader;

        if(nVisible==1)
        {
            m_ListActive.push_back(pHeader);
        }
        else if(nVisible==0)
        {
            m_ListInactive.push_back(pHeader);
        }
    }
}

void STableHeader::SetDataHistory(const QList<QStringList>& resultQuery)
{
    ClearMapDataHistory();
    int i=0;
    m_ListActive.clear();
    m_ListInactive.clear();
    for(i=0 ; i<resultQuery.size() ; i++)
    {
        const QStringList& listData = resultQuery[i];
        QString strDefaultIndex = listData[0];
        QString strDisplayIndex = listData[1];
        QString strValueString = listData[2];
        QString strVisible = listData[3];
        QString strDisplayWidth = listData[4];

        int nDefaultIndex = strDefaultIndex.toInt();
        int nDisplayIndex = strDisplayIndex.toInt();
        int nVisible = strVisible.toInt();
        int nDisplayWidth = strDisplayWidth.toInt();

        TableHeader* pHeader = new TableHeader;
        pHeader->m_nIndex = nDisplayIndex;
        pHeader->m_nIndexDefault = nDefaultIndex;
        pHeader->m_strText = strValueString;
        pHeader->m_bSelect = false;
        pHeader->m_nWidth = nDisplayWidth;

        m_MapHeaderHistory[i] = pHeader;

        if(nVisible==1)
        {
            m_ListActive.push_back(pHeader);
        }
        else if(nVisible==0)
        {
            m_ListInactive.push_back(pHeader);
        }
    }
}
