<?php

namespace Modules\Evaluation\Repository;

use Entity\Model\BibQstTh;
use Entity\Model\CampQstTh;
use Entity\Model\AoQstTh;
use Entity\Model\GrilTheme;

class Theme
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    
    public function reOrder($mode, $idMode, $idTheme, $oldParent, $parent, $oldPosition, $position)
    {
        $oldPosition = $oldPosition+1;
        $position = $position+1;
        
        $this->reOrderAnomalie($mode, $idMode, $oldParent);

        if ($oldParent != $parent) {
            $this->reOrderAnomalie($mode, $idMode, $parent);
            $this->reOrderOtherParent($mode, $idMode, $idTheme, $oldParent, $parent, $oldPosition, $position);
            
            $this->reorderTheme($mode, $idMode, $oldParent, $oldPosition);
        } else {
            $this->reOrderSameParent($mode, $idMode, $idTheme, $parent, $oldPosition, $position);
        }
    }
    
    public function orderIsError($mode, $idMode, $idParent)
    {
        $tab = '';
        $whereTab = '';
        if ($mode == 'question') {
            $tab = 'BIB_QST_TH';
        } elseif ($mode == 'campagne') {
            $tab = 'CAMP_QST_TH';
            $whereTab = ' and ID_CAMP = :idCamp';
            $arrayReq[':idCamp'] = $idMode;
        } elseif ($mode == 'AO') {
            $tab = 'AO_QST_TH';
            $whereTab = ' and NO_AO = :noAO';
            $arrayReq[':noAO'] = $idMode;
        } elseif ($mode == 'modele') {
            $tab = 'GRIL_THEME';
            $whereTab = ' AND ID_MODEL = :ID_MODEL';
            $arrayReq[':ID_MODEL'] = $idMode;
        }
        
        
        $req = "SELECT MAX(NM_ORDRE) maxNumOrdre, count(NM_ORDRE) countNumOrdre
                FROM $tab WITH (NOLOCK)
                WHERE ID_PERE = :idPere";

        $req .= $whereTab;
        $requete = $this->pdo->prepare($req);
        $arrayReq[':idPere'] = $idParent;
        
        $requete->execute($arrayReq);
        return $requete->fetch();
    }
    
    public function reOrderAnomalie($mode, $idMode, $idPere)
    {
        $erreur = $this->orderIsError($mode, $idMode, $idPere);

        /** @var BibQstTh|CampQstTh|AoQstTh|GrilTheme $entity */
        $entity = '';
        $filtre = [];
        if ($mode == 'question') {
            $entity = new BibQstTh($this->pdo);
            $filtre = ['filter_egal_ID_THEME' => $idPere, 'tri_NM_ORDRE' => 'asc'];
        } elseif ($mode == 'campagne') {
            $entity = new CampQstTh($this->pdo);
            $filtre = ['filter_egal_ID_THEME' => $idPere, 'filter_egal_ID_CAMP' => $idMode, 'tri_NM_ORDRE' => 'asc'];
        } elseif ($mode == 'AO') {
            $entity = new AoQstTh($this->pdo);
            $filtre = ['filter_egal_ID_THEME' => $idPere, 'filter_egal_NO_AO' => $idMode, 'tri_NM_ORDRE' => 'asc'];
        } elseif ($mode == 'modele') {
            $entity = new GrilTheme($this->pdo);
            $filtre = ['filter_egal_ID_THEME' => $idPere, 'filter_egal_ID_MODEL' => $idMode, 'tri_NM_ORDRE' => 'asc'];
        }
        
        if ($erreur['maxNumOrdre'] != $erreur['countNumOrdre'] && $entity != '') {
            $listTheme = [];
            $i = 1;
            
            $listTheme = $entity->fetchAll([], $filtre);
            
            foreach ($listTheme as $theme) {
                if ($mode=='question') {
                    $entity->update(['NM_ORDRE' => $i], $theme['ID_THEME']);
                } elseif ($mode == 'campagne') {
                    $entity->update(['NM_ORDRE' => $i], ['ID_CAMP' => $idMode, 'ID_THEME' => $theme['ID_THEME']]);
                } elseif ($mode == 'AO') {
                    $entity->update(['NM_ORDRE' => $i], ['NO_AO' => $idMode, 'ID_THEME' => $theme['ID_THEME']]);
                } elseif ($mode == 'modele') {
                    $entity->update(['NM_ORDRE' => $i], ['ID_MODEL' => $idMode, 'ID_THEME' => $theme['ID_THEME']]);
                }
                
                $i++;
            }
        }
    }
    
    public function reOrderOtherParent($mode, $idMode, $idTheme, $oldParent, $parent, $oldPosition, $position)
    {
        // init array pour requete en fonction de la table
        $tab = '';
        $whereTab = '';
        $arrayReqSave = [];
        if ($mode == 'question') {
            $tab = 'BIB_QST_TH';
        } elseif ($mode == 'campagne') {
            $tab = 'CAMP_QST_TH';
            $whereTab = ' and ID_CAMP = :idCamp';
            $arrayReqSave[':idCamp'] = $idMode;
        } elseif ($mode == 'AO') {
            $tab = 'AO_QST_TH';
            $whereTab = ' and NO_AO = :noAO';
            $arrayReqSave[':noAO'] = $idMode;
        } elseif ($mode == 'modele') {
            $tab = 'GRIL_THEME';
            $whereTab = ' AND ID_MODEL = :ID_MODEL';
            $arrayReqSave[':ID_MODEL'] = $idMode;
        }

        // update tout les themes du parent
        $req = "UPDATE $tab SET NM_ORDRE = NM_ORDRE +1 WHERE ID_PERE = :parent AND NM_ORDRE >= :position ".$whereTab;
        
        $arrayReq = $arrayReqSave;
        $arrayReq[':parent'] = $parent;
        $arrayReq[':position'] = $position;

        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);

        // place le theme a son nouvel order
        $req = "UPDATE $tab SET NM_ORDRE = :position, ID_PERE = :parent WHERE ID_THEME = :idTheme".$whereTab;
        
        $arrayReq = $arrayReqSave;
        $arrayReq[':parent'] = $parent;
        $arrayReq[':idTheme'] = $idTheme;
        $arrayReq[':position'] = $position;
        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
    }
    
    public function reorderTheme($mode, $idMode, $idParent, $numOrdre)
    {
        // init array pour requete en fonction de la table
        $tab = '';
        $whereTab = '';
        if ($mode == 'question') {
            $tab = 'BIB_QST_TH';
        } elseif ($mode == 'campagne') {
            $tab = 'CAMP_QST_TH';
            $whereTab = ' and ID_CAMP = :idCamp';
            $arrayReq[':idCamp'] = $idMode;
        } elseif ($mode == 'AO') {
            $tab = 'AO_QST_TH';
            $whereTab = ' and NO_AO = :noAO';
            $arrayReq[':noAO'] = $idMode;
        } elseif ($mode == 'modele') {
            $tab = 'GRIL_THEME';
            $whereTab = ' AND ID_MODEL = :ID_MODEL';
            $arrayReq[':ID_MODEL'] = $idMode;
        }
        
        $sql = "UPDATE $tab SET NM_ORDRE = NM_ORDRE-1 WHERE ID_PERE = :idParent AND NM_ORDRE > :numOrdre ".$whereTab;
        
        $arrayReq[':parent'] = $idParent;
        $arrayReq[':numOrdre'] = $numOrdre;
        
        $requete = $this->pdo->prepare($sql);
        
        return $requete->execute($arrayReq);
    }
    
    public function reOrderSameParent($mode, $idMode, $idTheme, $parent, $oldPosition, $position)
    {
        // init array pour requete en fonction de la table
        $tab = '';
        $whereTab = '';
        $arrayReqSave = [];
        if ($mode == 'question') {
            $tab = 'BIB_QST_TH';
        } elseif ($mode == 'campagne') {
            $tab = 'CAMP_QST_TH';
            $whereTab = ' and ID_CAMP = :idCamp';
            $arrayReqSave[':idCamp'] = $idMode;
        } elseif ($mode == 'AO') {
            $tab = 'AO_QST_TH';
            $whereTab = ' and NO_AO = :noAO';
            $arrayReqSave[':noAO'] = $idMode;
        } elseif ($mode == 'modele') {
            $tab = 'GRIL_THEME';
            $whereTab = ' AND ID_MODEL = :ID_MODEL';
            $arrayReqSave[':ID_MODEL'] = $idMode;
        }
        
        if ($oldPosition<$position) {
            $req = "UPDATE $tab SET NM_ORDRE = NM_ORDRE - 1 WHERE ID_PERE = :parent AND NM_ORDRE > :oldPosition AND NM_ORDRE <= :position ".$whereTab;
        } else {
            $req = "UPDATE $tab SET NM_ORDRE = NM_ORDRE + 1 WHERE ID_PERE = :parent AND NM_ORDRE >= :position AND NM_ORDRE < :oldPosition ".$whereTab;
        }
        
        $arrayReq = $arrayReqSave;
        $arrayReq[':parent'] = $parent;
        $arrayReq[':oldPosition'] = $oldPosition;
        $arrayReq[':position'] = $position;

        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);

        $req = "UPDATE $tab SET NM_ORDRE = :position WHERE ID_PERE = :parent AND ID_THEME = :idTheme ".$whereTab;
        
        $arrayReq = $arrayReqSave;
        $arrayReq[':parent'] = $parent;
        $arrayReq[':idTheme'] = $idTheme;
        $arrayReq[':position'] = $position;

        
        $requete = $this->pdo->prepare($req);
        $requete->execute($arrayReq);
    }
}
