#include "delegate.h"
#include "constants.h"
#include "tools.h"

#include <QComboBox>
#include <QDateEdit>


using namespace Constants;
using namespace ToolsSpace;

ComboDelegate::ComboDelegate(QObject * parent)
 {
     Q_UNUSED(parent);
     Tools t(this);
     m_hashConditions=t.hashOfConditionDelegateCombo();
     m_hashTypesOfDocuments = t.hashIntAndTypeStringsOfHeads();
     m_hashConjonctions = t.hashOfConjonctions();
 }

ComboDelegate::~ComboDelegate(){}

QSize ComboDelegate::sizeHint(const QStyleOptionViewItem &  option ,
                              const QModelIndex & index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return QSize(40,25);
}

QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
{
   if ( index.column() == CONDITIONOFRESEARCH || index.column() == TYPEOFDOC || index.column() == CONJONCTION )
    {
   	  QComboBox * combo = new QComboBox(parent);
    	  return combo;
        }
   return QStyledItemDelegate::createEditor(parent, option, index);
}

 void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 {
     QComboBox * combo = static_cast<QComboBox*>(editor);
     combo->setEditable(false);
     for (int row = 0; row < ConditionsOfResearchMaxParam; ++row )
     {
     	  if (index.column() == CONDITIONOFRESEARCH)
     	  {
     	        combo->addItem(m_hashConditions.value(row));
     	      }
     	  if (index.column() == TYPEOFDOC)
     	  {
     	        combo->addItem(m_hashTypesOfDocuments.value(row));
     	      }
         }
     if (index.column() == CONJONCTION)
     	  {
     	        for (int i = 0; i < Conjonctions_MaxParam; ++i)
     	        {
     	              combo->addItem(m_hashConjonctions.value(i));
     	            }
     	      }
     QVariant cond = index.model()->data(index,Qt::DisplayRole);
     if (index.column() == CONDITIONOFRESEARCH)
     {
           combo->setCurrentIndex(m_hashConditions.key(cond.toString()));
         }
     if (index.column() == TYPEOFDOC)
     {
            combo->setCurrentIndex(m_hashTypesOfDocuments.key(cond.toString()));
         }
     if (index.column() == CONJONCTION)
     {
           combo->setCurrentIndex(m_hashConjonctions.key(cond.toString()));
         }
     
     
}
 
 void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                      const QModelIndex &index) const
 {
     QComboBox * combo = static_cast<QComboBox*>(editor);
     int row = combo->currentIndex();
     QVariant value ;
     if (index.column() == CONDITIONOFRESEARCH)
     {
        switch(row){
           case CONTAINS :
             value = m_hashConditions.value(CONTAINS);
             break;
           case INF :
             value = m_hashConditions.value(INF);
             break;
           case SUP :
             value = m_hashConditions.value(SUP);
             break;
           default :
             break;    
         } 
         }
      if (index.column() == TYPEOFDOC)
      {
            switch(row){
                case OBSERVATION :
                    value = m_hashTypesOfDocuments.value(OBSERVATION);
                    break;
                case DOCUMENTS :
                    value = m_hashTypesOfDocuments.value(DOCUMENTS);
                    break;
                case ORDONNANCE_AJUSTABLE :
                    value = m_hashTypesOfDocuments.value(ORDONNANCE_AJUSTABLE);
                    break;
                case TERRAIN :
                    value = m_hashTypesOfDocuments.value(TERRAIN);
                default :
                    break;    
                }          
            }
        if (index.column() == CONJONCTION)
        {
              switch(row){
                  case AND :
                      value = m_hashConjonctions.value(AND);
                      break;
                  case OR :
                      value = m_hashConjonctions.value(OR);
                      break;
                  default :
                      break;    
                  }             
                  
            }
            

      model->setData(index, value, Qt::EditRole);      
 }
 
 //////////////////////////////////////////////////////////
 //////////////DATE EDIT//////////////
 //////////////////////////////////////////
 
 DateDelegate::DateDelegate(QObject * parent)
 {
     Q_UNUSED(parent);
 }

DateDelegate::~DateDelegate(){}

QSize DateDelegate::sizeHint(const QStyleOptionViewItem &  option ,
                              const QModelIndex & index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);
    return QSize(40,25);
}

QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const
{
   if (  index.column() == DATEBEGIN || index.column() == DATEEND)
    {
    	  QDateEdit * dateEdit = new QDateEdit(parent);
    	  dateEdit->setDisplayFormat("yyyy-MM-dd");
    	  return dateEdit;
        }
   return QStyledItemDelegate::createEditor(parent, option, index);
}

 void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
 {
     QDateEdit * dateEdit = static_cast<QDateEdit*>(editor);
     QDate date;
     date = index.model()->data(index,Qt::DisplayRole).toDate();
     dateEdit->setDate(date);
}
 
 void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                      const QModelIndex &index) const
 {
     QDateEdit * dateEdit = static_cast<QDateEdit*>(editor);
     QString value = dateEdit->date().toString("yyyy-MM-dd");

     model->setData(index, value, Qt::EditRole);
 }
