/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
	Page affichant la liste des passages de train.

*/

/* ************************************************************************** */
/* Revision:
### 20131021 JFM
    Verision d'origine.

### YYYMMDD Description du besoin ou du bug
    Description du changement.
 */

/* ************************************************************************** */


#include "NetworkZTListPage.h"
#include <QGraphicsProxyWidget>
#include "OutilZT.h"
#include <QString>
#include <QTextCodec>
#include <QHeaderView>
//#include <NetworkManager.h>
#include <QDebug>


CNetworkZTListPage::~CNetworkZTListPage()
{
    ClearTable();
}

CNetworkZTListPage::CNetworkZTListPage(QGraphicsWidget *Parent)
{
    //QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
	setParentItem(Parent);
	mProgramHandle = 0;


    mZTListTable = new QTableWidget(0,3);
	QGraphicsProxyWidget *TableProxy = new QGraphicsProxyWidget(this);
    TableProxy->setWidget(mZTListTable);
    TableProxy->setPos(3,20);
    TableProxy->resize(320,300);
    TableProxy->setZValue(3);
    mZTListTable->resize(320,300);
    mZTListTable->horizontalHeader()->setStretchLastSection(true);
    mZTListTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    mZTListTable->setSelectionMode(QAbstractItemView::SingleSelection);
    mZTListTable->clearContents();
    mZTListTable->setRowCount(NB_ZT_ID);
    mZTListTable->setHorizontalHeaderLabels(QStringList() << "Zone Test" << "Addr. IP" << "Connection");
//	connect(mLogsTable,SIGNAL(cellClicked(int,int)),this,SLOT(LogsTableCellClicked(int,int)));
    connect(mZTListTable,SIGNAL(currentCellChanged(int,int,int,int)),this,SLOT(ZTListTableCellSelected(int,int,int,int)));
    connect(mZTListTable,SIGNAL(cellDoubleClicked(int,int)),SLOT(TableDoubleClicked(int,int)));


    for(int i = 0; i < NB_ZT_ID; i++)
    {
        QTableWidgetItem * NewItem = new QTableWidgetItem(COutilZT::GetStationName(i));
        NewItem->setData(Qt::UserRole,QVariant(i));
        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
        mZTListTable->setItem(i,0,NewItem);
        NewItem = new QTableWidgetItem("192.168.100.100");
        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
        mZTListTable->setItem(i,1,NewItem);
        NewItem = new QTableWidgetItem("Inconnu");
        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
        mZTListTable->setItem(i,2,NewItem);
    }

    mZTListTable->resizeColumnsToContents();

    mZTListTable->setCurrentCell(0,0);
}



void CNetworkZTListPage::resizeEvent(QGraphicsSceneResizeEvent *event)
{
	Q_UNUSED(event)
//	mBackgroundRect->setRect(boundingRect());
}

void CNetworkZTListPage::ButtonClicked(CTextButtonWidget *BtnPtr)
{

}

unsigned int CNetworkZTListPage::RefreshList()
{

//    ClearTable();

//    bool ShowZT1Items = (mShowZT1ItemsChkBx->checkState() == Qt::Checked);
//    bool ShowZT2Items = (mShowZT2ItemsChkBx->checkState() == Qt::Checked);
//    bool ShowNoDetections = (mShowNoDetectionsChkBx->checkState() == Qt::Checked);

//    bool ShowPPDetections = (mShowPPChkBx->checkState() == Qt::Checked);
//    bool ShowPGDetections = (mShowPGChkBx->checkState() == Qt::Checked);
//    bool ShowCountDetections = (mShowCountChkBx->checkState() == Qt::Checked);
//    bool ShowFNDetections = (mShowFNChkBx->checkState() == Qt::Checked);


//    mLogsTable->setRowCount(mLogMgrHandle->GetLogsCount());

//    if(mLogMgrHandle->GetLogsCount() == 0)
//        return RET_OK;

//    int CurRow = 0;

//    QList<CLogElement*> *LogsList = mLogMgrHandle->GetLogsList();
//    for(unsigned int i = 0; i < mLogMgrHandle->GetLogsCount(); i++)
//    {
//        if(LogsList->at(i)->mZTLogType == ZT1_LOG_TYPE && ShowZT1Items == true)
//        {
//            CZT1LogElement *ZT1LogElement = (CZT1LogElement*)LogsList->at(i);
//            QString stationtext = mStationsFilterComboBox->currentText();
//            if(ZT1LogElement->mStationName == mStationsFilterComboBox->currentText() || mStationsFilterComboBox->currentText() == "Toutes")
//            {
//                int NbDetections = ZT1LogElement->mZTDetections.size();
//                if(ShowNoDetections == true && NbDetections == 0)
//                {
//                    QTableWidgetItem * NewItem = new QTableWidgetItem(ZT1LogElement->mPassageDateTime.toString("yyyy/MM/dd - hh:mm:ss"));
//                    NewItem->setData(Qt::UserRole,QVariant(i));
//                    NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                    mLogsTable->setItem(CurRow,0,NewItem);

//                    NewItem = new QTableWidgetItem(ZT1LogElement->mStationName);
//                    NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                    mLogsTable->setItem(CurRow,1,NewItem);

//                    NewItem = new QTableWidgetItem(QString().sprintf("%d",NbDetections));
//                    NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                    mLogsTable->setItem(CurRow,2,NewItem);

//                    NewItem = new QTableWidgetItem(QString().sprintf("ZT1"));
//                    NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                    mLogsTable->setItem(CurRow,3,NewItem);

//                    CurRow++;
//                }
//                else if(ShowNoDetections == false && NbDetections == 0)
//                {
//                }
//                else
//                {
//                    bool FilterItem = true;

//                    for(int trig = 0; trig < NbDetections; trig++)
//                    {
//                        quint32 DetectionID = ZT1LogElement->mZTDetections.at(trig)->mDetectionID;

//                        if((ShowCountDetections == true && DetectionID == DETECTION_MAGNETIC_SENSOR_COUNT) ||
//                           (ShowFNDetections == true && DetectionID == DETECTION_FN_DETECTION) ||
//                           (ShowPGDetections == true && DetectionID == DETECTION_PG_DETECTION) ||
//                           (ShowPPDetections == true && (DetectionID == DETECTION_PPI_DETECTION || DetectionID == DETECTION_PPE_DETECTION)))
//                        {
//                            FilterItem = false;
//                            break;
//                        }
//                    }

//                    if(FilterItem == false)
//                    {
//                        QTableWidgetItem * NewItem = new QTableWidgetItem(ZT1LogElement->mPassageDateTime.toString("yyyy/MM/dd - hh:mm:ss"));
//                        NewItem->setData(Qt::UserRole,QVariant(i));
//                        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                        mLogsTable->setItem(CurRow,0,NewItem);

//                        NewItem = new QTableWidgetItem(ZT1LogElement->mStationName);
//                        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                        mLogsTable->setItem(CurRow,1,NewItem);

//                        NewItem = new QTableWidgetItem(QString().sprintf("%d",NbDetections));
//                        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                        mLogsTable->setItem(CurRow,2,NewItem);

//                        NewItem = new QTableWidgetItem(QString().sprintf("ZT1"));
//                        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                        mLogsTable->setItem(CurRow,3,NewItem);

//                        CurRow++;
//                    }
//                }
//            }



//        }
//        else if(LogsList->at(i)->mZTLogType == ZT2_LOG_TYPE && ShowZT2Items == true)
//        {
//            CZT2LogElement *ZT2LogElement = (CZT2LogElement*)LogsList->at(i);

//            if(ZT2LogElement->mStationName == mStationsFilterComboBox->currentText() || mStationsFilterComboBox->currentText() == "Toutes")
//            {
//                int NbDetections = ZT2LogElement->mZTDetections.size();
//                if(ShowNoDetections == true && NbDetections == 0)
//                {
//                    QTableWidgetItem * NewItem = new QTableWidgetItem(ZT2LogElement->mPassageDateTime.toString("yyyy/MM/dd - hh:mm:ss"));
//                    NewItem->setData(Qt::UserRole,QVariant(i));
//                    NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                    mLogsTable->setItem(CurRow,0,NewItem);

//                    NewItem = new QTableWidgetItem(ZT2LogElement->mStationName);
//                    NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                    mLogsTable->setItem(CurRow,1,NewItem);

//                    NewItem = new QTableWidgetItem(QString().sprintf("%d",ZT2LogElement->mZTDetections.size()));
//                    NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                    mLogsTable->setItem(CurRow,2,NewItem);

//                    NewItem = new QTableWidgetItem(QString().sprintf("ZT2"));
//                    NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                    mLogsTable->setItem(CurRow,32,NewItem);

//                    CurRow++;
//                }
//                else if(ShowNoDetections == false && NbDetections == 0)
//                {
//                }
//                else
//                {
//                    bool FilterItem = true;

//                    for(int trig = 0; trig < NbDetections; trig++)
//                    {
//                        quint32 DetectionID = ZT2LogElement->mZTDetections.at(trig)->mDetectionID;

//                        if((ShowCountDetections == true && DetectionID == DETECTION_ZT2_MAGNETIC_SENCOR_COUNT) ||
//                           (ShowPPDetections == true && (DetectionID == DETECTION_ZT2_PPI_DETECTION || DetectionID == DETECTION_ZT2_PPE_DETECTION)))
//                        {
//                            FilterItem = false;
//                            break;
//                        }
//                    }

//                    if(FilterItem == false)
//                    {
//                        QTableWidgetItem * NewItem = new QTableWidgetItem(ZT2LogElement->mPassageDateTime.toString("yyyy/MM/dd - hh:mm:ss"));
//                        NewItem->setData(Qt::UserRole,QVariant(i));
//                        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                        mLogsTable->setItem(CurRow,0,NewItem);

//                        NewItem = new QTableWidgetItem(ZT2LogElement->mStationName);
//                        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                        mLogsTable->setItem(CurRow,1,NewItem);

//                        NewItem = new QTableWidgetItem(QString().sprintf("%d",ZT2LogElement->mZTDetections.size()));
//                        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                        mLogsTable->setItem(CurRow,2,NewItem);

//                        NewItem = new QTableWidgetItem(QString().sprintf("ZT2"));
//                        NewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
//                        mLogsTable->setItem(CurRow,32,NewItem);

//                        CurRow++;
//                    }
//                }
//            }
//        }
//    }

//    mLogsTable->setRowCount(CurRow);

//    mLogsTable->setColumnWidth(0,150);
//    mLogsTable->setColumnWidth(1,75);
//    mLogsTable->setColumnWidth(2,50);
//    mLogsTable->setColumnWidth(3,50);

//    mLogsTable->sortItems(0,Qt::DescendingOrder);

//    mLogsTable->setCurrentCell(0,0);
//    LogsTableCellClicked(0,0);


	return RET_OK;

}

void CNetworkZTListPage::ClearTable()
{
    for(int Col = 0; Col < mZTListTable->columnCount(); Col++)
    {
        for(int Row = 0; Row < mZTListTable->rowCount(); Row++)
        {
            delete mZTListTable->item(Row,Col);
        }
    }
    mZTListTable->setRowCount(0);
    mZTListTable->clearContents();

}
unsigned int CNetworkZTListPage::SetConfig(CSettingsData *SettingsData)
{
    if(SettingsData->mNetworkCfgList.size() != NB_ZT_ID)
        return RET_ERROR;

    for(int i = 0; i < NB_ZT_ID; i++)
    {
        mZTListTable->item(i,1)->setText(SettingsData->mNetworkCfgList.at(i).mIPAdress);
    }

    return RET_OK;
}

void CNetworkZTListPage::ZTListTableCellSelected( int row, int col, int prevrow, int prevcol)
{
    Q_UNUSED(prevrow)
    Q_UNUSED(prevcol)


//    if(row < 0 || col < 0)
//        return;

//    LogsTableCellClicked(row,col);
}

int CNetworkZTListPage::GetSelectedZT()
{
    return mZTListTable->selectedItems().at(0)->data(Qt::UserRole).toInt();
}

void CNetworkZTListPage::ZTListTableCellClicked(int row, int column)
{
	Q_UNUSED(column);

//    int rowcount = mLogsTable->rowCount();
//    if(rowcount == 0)
//        return;

////	qDebug("Selection : %d",row);
//	QList<CLogElement*> *LogsList = mLogMgrHandle->GetLogsList();

//	float MeanSpeed = 0.0;
//    QString Summary;

//    if(mLogsTable->item(row,0) == 0)
//        return;

//    int ItemIndex = mLogsTable->item(row,0)->data(Qt::UserRole).toInt();
//    //if(LogsList->at(row)->mZTLogType == ZT1_LOG_TYPE)
//    if(LogsList->at(ItemIndex)->mZTLogType == ZT1_LOG_TYPE)
//    {
////        //CZT1LogElement *ZT1LogElement = (CZT1LogElement*) LogsList->at(row);
//		CZT1LogElement *ZT1LogElement = (CZT1LogElement*) LogsList->at(ItemIndex);

//		MeanSpeed = ZT1LogElement->mMeanSpeed;
//        Summary.sprintf("Passage:\n\nDate: %s\nHeure: %s\nZone Test : ZT1\nTrain : %s\nNombre d'éléments: %d\n\n\nDéclenchements: \n",
//                        ZT1LogElement->mPassageDateTime.toString("yyyy/MM/dd").toAscii().data(),
//                        ZT1LogElement->mPassageDateTime.toString("hh:mm:ss").toAscii().data(),
//                        CZTData::GetTrainTypeString(ZT1LogElement->mTrainType),
//                        ZT1LogElement->mNbElements);

//        if(ZT1LogElement->mZTDetections.size() >  0)
//        {
//            for(int i = 0; i < ZT1LogElement->mZTDetections.size(); i++)
//            {
//                QString temp;
//				if(ZT1LogElement->mZTDetections.at(i)->mDetectionID == DETECTION_FN_DETECTION)
//				{
//					temp.sprintf("%s au bogie %d\n",CZTData::GetErrorString(ZT1LogElement->mZTDetections.at(i)->mDetectionID),ZT1LogElement->mZTDetections.at(i)->mRank);
//				}
//				else
//				{
//					temp.sprintf("%s au rang %d\n",CZTData::GetErrorString(ZT1LogElement->mZTDetections.at(i)->mDetectionID),ZT1LogElement->mZTDetections.at(i)->mRank);
//				}
//                Summary += temp;
//				if(i >= MAX_DETECTION_LIST_NBR - 1)
//                {
//                    temp = " - Autres déclenchements non affichés -";
//                    Summary += temp;
//                    break;
//                }
//            }
//        }
//        else
//            Summary += "Aucun déclenchement";
//    }
//    //else if(LogsList->at(row)->mZTLogType == ZT2_LOG_TYPE)
//    else if(LogsList->at(ItemIndex)->mZTLogType == ZT2_LOG_TYPE)
//    {
//        //CZT2LogElement *ZT2LogElement = (CZT2LogElement*) LogsList->at(row);
//        CZT2LogElement *ZT2LogElement = (CZT2LogElement*) LogsList->at(ItemIndex);

//        Summary.sprintf("Statistiques du passage:\n\nDate: %s\nHeure: %s\nZone Test: ZT2\nNombre d'éléments: %d\n\n\nDéclenchements: \n",
//                        ZT2LogElement->mPassageDateTime.toString("yyyy/MM/dd").toAscii().data(),
//                        ZT2LogElement->mPassageDateTime.toString("hh:mm:ss").toAscii().data(),
//                        ZT2LogElement->mNbElements);

    //        if(ZT2LogElement->mZTDetections.size() >  0)
    //        {
    //            for(int i = 0; i < ZT2LogElement->mZTDetections.size(); i++)
    //            {
    //                QString temp;
    //                temp.sprintf("%s au rang %d\n",CZTData::GetErrorString(ZT2LogElement->mZTDetections.at(i)->mDetectionID),ZT2LogElement->mZTDetections.at(i)->mRank);
    //                Summary += temp;
    //				if(i >= MAX_DETECTION_LIST_NBR-1)
    //                {
    //                    temp = "- Autres déclenchements non affichés -";
    //                    Summary += temp;
    //                    break;
    //                }
    //            }
    //        }
    //        else
    //            Summary += "Aucun déclenchement";

    //    }
    //    mSummaryText->setPlainText(Summary);

}


//Grab the mouse if the user clicks outside buttons
void CNetworkZTListPage::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
//    qDebug("CNetworkZTListPage");
	Q_UNUSED(event)
}
void CNetworkZTListPage::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	Q_UNUSED(event)
}

void CNetworkZTListPage::TableDoubleClicked(int row, int col)
{
    Q_UNUSED(col)
	Q_UNUSED(row)
  //  ButtonClicked(mViewLogDataButton);
}

void CNetworkZTListPage::ZTNetworkStatusChanged(int ZTID,int Status)
{
    if(ZTID >= NB_ZT_ID)
        return;

//    qDebug() << "ZT Network status changed.  ID: " << ZTID << "Status : " << Status;

    QString ZTStatus;
    QColor Color;
//    switch(Status)
//    {
//        case CNetworkManager::ZT_NETWORK_OFFLINE_STATUS:
//        {
//            ZTStatus = "Hors Ligne";
//            Color = Qt::red;
//            break;
//        }
//        case CNetworkManager::ZT_NETWORK_ONLINE_STATUS:
//        {
//            ZTStatus = "En Ligne";
//            Color = Qt::darkGreen;
//            break;
//        }
//        case CNetworkManager::ZT_NETWORK_SEARCHING_STATUS:
//        {
//            ZTStatus = "Recherche...";
//            Color = Qt::black;
//            break;
//        }
//        case CNetworkManager::ZT_NETWORK_UNKNOWN_STATUS:
//        {
//             ZTStatus = "Inconnu";
//             Color = Qt::black;
//            break;
//        }
//        case CNetworkManager::ZT_NETWORK_CONNECTED_STATUS:
//        {
//            ZTStatus = "Connecté";
//            Color = Qt::darkBlue;
//            break;
//        }
//    }

    mZTListTable->item(ZTID,2)->setText(ZTStatus);
    mZTListTable->item(ZTID,2)->setForeground(QBrush(Color));

}
