#include "ConfigPage.h"
#include "ui_ConfigPage.h"
#include "LogsSorterConfigData.h"

#include "LogsSorter.h"
#include "LogsSorterConfigData.h"

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

//    FILTRE="ZT1" #test de commentaire

//    TYPE="DÉPLACER"
//    DESCRIPTION="Zone Test 1 Terminus Beaugrand"
//    PREFIXE="HBGR"
//    DESTINATION="D:\Main\Fichiers\JOB\LogsDump\ZT Beaugrand"

    setAutoFillBackground(true);

    ui->mConfigTable->setAutoFillBackground(true);
    ui->mConfigTable->setColumnCount(5);
    ui->mConfigTable->setHorizontalHeaderLabels(QStringList() << "Filtre" << "Type" << "Préfixe" << "Description" << "Destination");
    ui->mConfigTable->setSelectionBehavior(QAbstractItemView::SelectRows);

    ui->mConfigTable->setRowCount(0);

    connect(ui->mAddFilterBtn,&QPushButton::pressed,this,&CConfigPage::AddFilterBtnPressed);
    connect(ui->mDeleteFilterBtn,&QPushButton::pressed,this,&CConfigPage::DeleteFilterBtnPressed);
    connect(ui->mSaveBtn,&QPushButton::pressed,this,&CConfigPage::SaveConfigBtnPressed);
    connect(ui->mCancelBtn,&QPushButton::pressed,this,&CConfigPage::CancelBtnPressed);
}

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

void CConfigPage::AddFilterBtnPressed()
{
    ui->mConfigTable->insertRow(0);
    QTableWidgetItem *NewItem;

    NewItem = new QTableWidgetItem("");
    ui->mConfigTable->setItem(0,0,NewItem);
    NewItem = new QTableWidgetItem("DÉPLACER");
    ui->mConfigTable->setItem(0,1,NewItem);
    NewItem = new QTableWidgetItem("");
    ui->mConfigTable->setItem(0,2,NewItem);
    NewItem = new QTableWidgetItem("");
    ui->mConfigTable->setItem(0,3,NewItem);
    NewItem = new QTableWidgetItem("");
    ui->mConfigTable->setItem(0,4,NewItem);
}

void CConfigPage::DeleteFilterBtnPressed()
{
    ui->mConfigTable->removeRow(ui->mConfigTable->currentRow());
}

void CConfigPage::CancelBtnPressed()
{
    InitializeConfigData(mProgramPtr->GetSorterConfigRequest());
}


void CConfigPage::SaveConfigBtnPressed()
{
    CLogsSorterConfigData CurrentConfig;

    //TODO: Confirmer la validité des données avant tout

    CurrentConfig.mLogsSourceDirectory = ui->mSrcDirLineEdit->text();

    for(int i = 0; i < ui->mConfigTable->rowCount(); i++)
    {
        //"Filtre" << "Type" << "Préfixe" << "Description" << "Destination");
        CLogsFilterData FilterData;

        FilterData.mLogIdentificator = ui->mConfigTable->item(i,0)->text();
        FilterData.mLogPrefix = ui->mConfigTable->item(i,2)->text();
        FilterData.mFilterName = ui->mConfigTable->item(i,3)->text();
        FilterData.mLogDestination = ui->mConfigTable->item(i,4)->text();

        if(ui->mConfigTable->item(i,1)->text() == "DÉPLACER")
        {
            FilterData.mFilterOperation = CLogsFilterData::LOG_FILTER_MOVE_OPERATION_TYPE;
        }
        else if(ui->mConfigTable->item(i,1)->text() == "EFFACER")
        {
            FilterData.mFilterOperation = CLogsFilterData::LOG_FILTER_DELETE_OPERATION_TYPE;
        }
        else
        {
            //TODO: Ceci est une erreur... à gérer
        }

        CurrentConfig.mLogsFiltersList.append(FilterData);
    }

    mProgramPtr->SaveSorterConfigRequest(CurrentConfig);


}



void CConfigPage::InitializeConfigData(const CLogsSorterConfigData *DataStruct)
{
     ui->mConfigTable->clearContents();
    ui->mSrcDirLineEdit->setText(DataStruct->mLogsSourceDirectory);

    ui->mConfigTable->setRowCount(DataStruct->mLogsFiltersList.size());
    for(int i = 0; i < DataStruct->mLogsFiltersList.size(); i++)
    {
        QTableWidgetItem *NewItem;


        //"Filtre" << "Type" << "Préfixe" << "Description" << "Destination");

        NewItem = new QTableWidgetItem(DataStruct->mLogsFiltersList.at(i).mLogIdentificator);
        ui->mConfigTable->setItem(i,0,NewItem);
        if(DataStruct->mLogsFiltersList.at(i).mFilterOperation == CLogsFilterData::LOG_FILTER_MOVE_OPERATION_TYPE)
        {
            NewItem = new QTableWidgetItem("DÉPLACER");
        }
        if(DataStruct->mLogsFiltersList.at(i).mFilterOperation == CLogsFilterData::LOG_FILTER_DELETE_OPERATION_TYPE)
        {
            NewItem = new QTableWidgetItem("EFFACER");
        }
        ui->mConfigTable->setItem(i,1,NewItem);
        NewItem = new QTableWidgetItem(DataStruct->mLogsFiltersList.at(i).mLogPrefix);
        ui->mConfigTable->setItem(i,2,NewItem);
        NewItem = new QTableWidgetItem(DataStruct->mLogsFiltersList.at(i).mFilterName);
        ui->mConfigTable->setItem(i,3,NewItem);
        NewItem = new QTableWidgetItem(DataStruct->mLogsFiltersList.at(i).mLogDestination);
        ui->mConfigTable->setItem(i,4,NewItem);
    }

    ui->mConfigTable->resizeColumnsToContents();
}

