#include "dialogsettingtable.h"
#include "ui_dialogsettingtable.h"

#include "mainwindow.h"

DialogSettingTable::DialogSettingTable(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSettingTable)
{
    //setAttribute(Qt::WA_TranslucentBackground);
    //setWindowFlags(Qt::CustomizeWindowHint);
    //setWindowFlags(Qt::FramelessWindowHint);

    //setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog | Qt::Popup);
    setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);

    ui->setupUi(this);

    CommonData* pCommonData = MainWindow::GetCommonData();


    {
        ui->listWidget_choose->SetActivate(false);
        ui->verticalScrollBar_Choose->setRange(0, 299);
        ui->verticalScrollBar_Choose->SetSlideType(SSlideBar::SLIDE_VERTICAL);

        connect(ui->verticalScrollBar_Choose, SIGNAL(valueChanged(int)), ui->listWidget_choose, SLOT(valueChangedScrollBarHorizontal(int)));

        QScrollBar* pScrollBarChoose = ui->listWidget_choose->verticalScrollBar();
        connect(pScrollBarChoose, SIGNAL(valueChanged(int)), this, SLOT(ScrollChangeChoose(int)));
    }

    {
        ui->listWidget_Selection->SetActivate(true);
        ui->verticalScrollBar_Selection->setRange(0, 299);
        ui->verticalScrollBar_Selection->SetSlideType(SSlideBar::SLIDE_VERTICAL);

        connect(ui->verticalScrollBar_Selection, SIGNAL(valueChanged(int)), ui->listWidget_Selection, SLOT(valueChangedScrollBarHorizontal(int)));

        QScrollBar* pScrollBarChoose = ui->listWidget_Selection->verticalScrollBar();
        connect(pScrollBarChoose, SIGNAL(valueChanged(int)), this, SLOT(ScrollChangeSelection(int)));
    }

    int nDisplayType = pCommonData->GetDisplayType();

    if(nDisplayType==SDISPLAY_1280X1024)
    {
        int nGabX = (1280-1000)/2;
        int nGabY = (1024-600)/2;
        setGeometry(nGabX, nGabY, 1000, 600);
        ui->frame_choose->setGeometry(30, 52, 380, 520);
        ui->label_choose->setGeometry(0, 0, 380, 50);
        ui->listWidget_choose->setGeometry(20, 60, 380-60, 520-80);
        ui->frame_verticalScrollBar_Choose->setGeometry(380-30, 50, 25, 520-60);
        ui->verticalScrollBar_Choose->setGeometry(0, 0, 25, 520-60);

        ui->pushButton_Active->setGeometry(430, 220, 60, 31);
        ui->pushButton_Inactive->setGeometry(430, 320, 60, 31);

        ui->frame_selection->setGeometry(520, 52, 380, 520);
        ui->label_selection->setGeometry(0, 0, 380, 50);
        ui->listWidget_Selection->setGeometry(20, 60, 380-60, 520-80);
        ui->frame_verticalScrollBar_Selection->setGeometry(380-30, 50, 25, 520-60);
        ui->verticalScrollBar_Selection->setGeometry(0, 0, 25, 520-60);

        ui->pushButton_Up->setGeometry(920, 60, 60, 36);
        ui->pushButton_Down->setGeometry(920, 100, 60, 36);
        ui->pushButton_Save->setGeometry(920, 540, 60, 36);
    }
    else if(nDisplayType==SDISPLAY_1920X1080)
    {
        setGeometry(0, 0, 1583, 818);
        ui->frame_choose->setGeometry(52, 52, 616, 724);
        ui->label_choose->setGeometry(0, 0, 616, 50);
        ui->listWidget_choose->setGeometry(50, 60, 501, 651);
        ui->frame_verticalScrollBar_Choose->setGeometry(570, 20, 25, 671);
        ui->verticalScrollBar_Choose->setGeometry(0, 0, 25, 671);

        ui->pushButton_Active->setGeometry(700, 290, 89, 51);
        ui->pushButton_Inactive->setGeometry(700, 390, 89, 51);

        ui->frame_selection->setGeometry(814, 52, 616, 724);
        ui->label_selection->setGeometry(0, 0, 616, 50);
        ui->listWidget_Selection->setGeometry(50, 60, 501, 651);
        ui->frame_verticalScrollBar_Selection->setGeometry(570, 20, 25, 671);
        ui->verticalScrollBar_Selection->setGeometry(0, 0, 25, 671);

        ui->pushButton_Up->setGeometry(1470, 60, 89, 51);
        ui->pushButton_Down->setGeometry(1470, 120, 89, 51);
        ui->pushButton_Save->setGeometry(1470, 720, 89, 51);
    }
}

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

void DialogSettingTable::SetListType(int nType)
{
    m_nListType = nType;

    CommonData* pCommonData = MainWindow::GetCommonData();
    STableHeader* pTableHeader = NULL;

    if(m_nListType==SWIDGET_WORKLIST)
    {
        pTableHeader = pCommonData->GetWorklistTableHeader();
        m_TableHeader.CopyWorklistFrom(pTableHeader);
    }
    else if(m_nListType==SWIDGET_HISTORY)
    {
        pTableHeader = pCommonData->GetHistoryTableHeader();
        m_TableHeader.CopyHistoryFrom(pTableHeader);
    }

    ui->listWidget_choose->SetListType(nType);
    ui->listWidget_Selection->SetListType(nType);

    ui->listWidget_choose->SetData(&m_TableHeader);
    ui->listWidget_Selection->SetData(&m_TableHeader);

    CheckVerticalScrollBar();

}

void DialogSettingTable::CheckVerticalScrollBar()
{
    int nMaxShowItem = 8;
    int nDisplayType = MainWindow::GetCommonData()->GetDisplayType();
    if(nDisplayType==SDISPLAY_1280X1024)
    {
        nMaxShowItem = 11;
    }
    if(ui->listWidget_choose->count() > nMaxShowItem)
    {
        ui->verticalScrollBar_Choose->show();
    }
    else
    {
        ui->verticalScrollBar_Choose->hide();
    }

    if(ui->listWidget_Selection->count() > nMaxShowItem)
    {
        ui->verticalScrollBar_Selection->show();
    }
    else
    {
        ui->verticalScrollBar_Selection->hide();
    }
}

void DialogSettingTable::on_pushButton_Save_clicked()
{
    MainWindow* pMainWindow = MainWindow::GetMainWindow();
    CommonData* pCommonData = MainWindow::GetCommonData();

    STableHeader* pTableHeader = NULL;
    if(m_nListType==SWIDGET_WORKLIST)
    {
        pTableHeader = pCommonData->GetWorklistTableHeader();
        pTableHeader->CopyWorklistFrom(&m_TableHeader);
        pCommonData->UpdateWorklistIndex();
    }
    else if(m_nListType==SWIDGET_HISTORY)
    {
        pMainWindow->ClearHistoryView();
        pTableHeader = pCommonData->GetHistoryTableHeader();
        pTableHeader->CopyHistoryFrom(&m_TableHeader);
        pCommonData->UpdateHistoryIndex();
    }


    done(QDialog::Accepted);
}



void DialogSettingTable::ScrollChangeChoose(int nValue)
{
    QScrollBar* pScrollBar = ui->listWidget_choose->verticalScrollBar();
    int nMax = pScrollBar->maximum();
    float fRatio = (float)nValue / (float)nMax;

    int nPos = fRatio * 299.0f + 0.5f;
    if(nPos>299)
    {
        nPos = 299;
    }

    ui->verticalScrollBar_Choose->setValue(nPos);
}

void DialogSettingTable::ScrollChangeSelection(int nValue)
{
    QScrollBar* pScrollBar = ui->listWidget_Selection->verticalScrollBar();
    int nMax = pScrollBar->maximum();
    float fRatio = (float)nValue / (float)nMax;

    int nPos = fRatio * 299.0f + 0.5f;
    if(nPos>299)
    {
        nPos = 299;
    }

    ui->verticalScrollBar_Selection->setValue(nPos);
}

void DialogSettingTable::on_pushButton_Active_clicked()
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    STableHeader* pTableHeader = &m_TableHeader;
    vector<TableHeader*> listMove;

    vector<TableHeader*>* pListHeader = NULL;
    {
        pListHeader = pTableHeader->GetListInactive();
        vector<TableHeader*>::iterator it;
        for(it=pListHeader->begin() ; it!=pListHeader->end() ; )
        {
            TableHeader* pHeader = *it;
            if(pHeader->m_bSelect==true)
            {
                listMove.push_back(pHeader);
                pListHeader->erase(it);
            }
            else
            {
                ++it;
            }
        }

        if(listMove.size()>0)
        {
            ui->listWidget_choose->SetData(&m_TableHeader);
        }
    }

    if(listMove.size()>0)
    {
        pListHeader = pTableHeader->GetListActive();
        vector<TableHeader*>::iterator it;
        for(it=listMove.begin() ; it!=listMove.end() ; ++it)
        {
            TableHeader* pHeader = *it;
            pHeader->m_bSelect = false;

            pListHeader->push_back(pHeader);
        }

        if(listMove.size()>0)
        {
            listMove.clear();
        }

        ui->listWidget_Selection->SetData(&m_TableHeader);
    }

    CheckVerticalScrollBar();

}

void DialogSettingTable::on_pushButton_Inactive_clicked()
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    STableHeader* pTableHeader = &m_TableHeader;
    vector<TableHeader*> listMove;

    vector<TableHeader*>* pListHeader = NULL;
    {
        pListHeader = pTableHeader->GetListActive();
        vector<TableHeader*>::iterator it;
        for(it=pListHeader->begin() ; it!=pListHeader->end() ; )
        {
            TableHeader* pHeader = *it;
            if(pHeader->m_bSelect==true)
            {
                listMove.push_back(pHeader);
                pListHeader->erase(it);
            }
            else
            {
                ++it;
            }
        }

        if(listMove.size()>0)
        {
            ui->listWidget_Selection->SetData(&m_TableHeader);
        }
    }

    if(listMove.size()>0)
    {
        pListHeader = pTableHeader->GetListInactive();
        vector<TableHeader*>::iterator it;
        for(it=listMove.begin() ; it!=listMove.end() ; ++it)
        {
            TableHeader* pHeader = *it;
            pHeader->m_bSelect = false;

            pListHeader->push_back(pHeader);
        }

        if(listMove.size()>0)
        {
            listMove.clear();
        }

        ui->listWidget_choose->SetData(&m_TableHeader);
    }

    CheckVerticalScrollBar();
}

void DialogSettingTable::on_pushButton_Up_clicked()
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    STableHeader* pTableHeader = &m_TableHeader;
    vector<TableHeader*> listMove;

    vector<TableHeader*>* pListHeader = NULL;
    {
        pListHeader = pTableHeader->GetListActive();
        int nCheckCount = 0;
        int nSwapCount = 0;
        int i=0;
        for(i=0 ; i<pListHeader->size() ; i++)
        {
            TableHeader* pHeader = (*pListHeader)[i];
            if(pHeader->m_bSelect==true)
            {
                nCheckCount++;
            }
        }

        //for(i=0 ; i<nCheckCount ; i++)
        {
            vector<TableHeader*>::iterator it;
            vector<TableHeader*>::iterator itNext;
            int nIndex = 0;
            for(it=pListHeader->begin() ; it!=pListHeader->end() ; ++it, nIndex++)
            {
                TableHeader* pHeader = *it;
                if(pHeader->m_bSelect==true)
                {
                    if(nIndex==0)
                    {
                        break;
                    }
                    itNext = it;
                    --itNext;
                    TableHeader* pHeaderNext = *itNext;

                    if(pHeaderNext!=NULL && itNext!=pListHeader->end())
                    {

                        TableHeader* pHeaderTmp = pHeaderNext;

                        *itNext = pHeader;
                        *it = pHeaderTmp;
                        nSwapCount++;
                        //break;
                    }
                }
            }
        }

        if(nSwapCount>0)
        {
            ui->listWidget_Selection->SetData(&m_TableHeader);
        }


    }
}

void DialogSettingTable::on_pushButton_Down_clicked()
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    STableHeader* pTableHeader = &m_TableHeader;
    vector<TableHeader*> listMove;

    vector<TableHeader*>* pListHeader = NULL;
    {
        pListHeader = pTableHeader->GetListActive();
        int nCheckCount = 0;
        int nSwapCount = 0;
        int i=0;
        for(i=0 ; i<pListHeader->size() ; i++)
        {
            TableHeader* pHeader = (*pListHeader)[i];
            if(pHeader->m_bSelect==true)
            {
                nCheckCount++;
            }
        }

        //for(i=0 ; i<nCheckCount ; i++)
        {
            vector<TableHeader*>::reverse_iterator it;
            vector<TableHeader*>::reverse_iterator itNext;
            int nIndex = 0;
            for(it=pListHeader->rbegin() ; it!=pListHeader->rend() ; ++it, nIndex++)
            {
                TableHeader* pHeader = *it;
                if(pHeader->m_bSelect==true)
                {
                    if(nIndex==0)
                    {
                        break;
                    }
                    itNext = it;
                    --itNext;
                    TableHeader* pHeaderNext = *itNext;

                    if(pHeaderNext!=NULL)
                    {

                        TableHeader* pHeaderTmp = pHeaderNext;

                        *itNext = pHeader;
                        *it = pHeaderTmp;
                        nSwapCount++;
                        //break;
                    }
                }
            }
        }

        if(nSwapCount>0)
        {
            ui->listWidget_Selection->SetData(&m_TableHeader);
        }


    }
}
