<?php

namespace Modules\ProjetAchat\Service;

use \Entity\Model\DoarCat;
use Helpers\Validator\Validator;
use Modules\ProjetAchat\Repository\Categorie;

class CategorieService
{
    
    private $pdo;

    /**
     * @var DoarCat $doarCat
     */
    private $doarCat;

    /**
     * @var Categorie $categorieRepository
     */
    private $categorieRepository;

    /**
     * @var \Modules\ProjetAchat\Repository\Donnee $donneeRepository
     */
    private $donneeRepository;

    /**
     * @var DonneeService $donneeService
     */
    private $donneeService;


    /**
     * CategorieService constructor.
     */
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
        $this->doarCat = new DoarCat($this->pdo);
        $this->categorieRepository = new Categorie($this->pdo);
        $this->donneeRepository = new \Modules\ProjetAchat\Repository\Donnee($this->pdo);
        $this->donneeService = new \Modules\ProjetAchat\Service\DonneeService($this->pdo);
    }

    /**
     * @return array<mixed>
    */
    public function findCategory(Int $id): ?array
    {
        return $this->doarCat->find($id);
    }

    public function createCategory(array $payload): void
    {
        $this->doarCat->insert($payload);
    }

    public function updateCategory(array $payload, Int $id): void
    {
        $this->doarCat->update($payload, $id);
    }

    public function deleteCategory(string $typeObjet, int $idObjet, int $idCategorie, int $postHeritage = 0) : void
    {
        $listeDeDonnees = $this->donneeRepository->getListeDonee($typeObjet, $idObjet, $idCategorie);
        if ($listeDeDonnees) {
            array_map(function ($e) use ($postHeritage) {
                $this->donneeService->supprimerDonnee($e['TYPE_OBJET'], $e['ID_OBJET'], $e['ID_DONNEE'], $postHeritage);
            }, $listeDeDonnees);
        }

        $category = $this->doarCat->find($idCategorie);
        $this->categorieRepository->deleteWithParam($typeObjet, $idObjet, $idCategorie);
        $this->categorieRepository->reorderCategory($typeObjet, $idObjet, $category['ID_PARENT'], $category['NUM_ORDRE']);

        $listFils = $this->categorieRepository->findSon($typeObjet, $idObjet, $idCategorie);
        
        if ($listFils) {
            foreach ($listFils as $fils) {
                $this->deleteCategory($typeObjet, $idObjet, $fils['ID']);
            }
        }
    }

    public function getParentCategory(Int $id): int
    {
        return (int)$this->findCategory($id)['ID_PARENT'];
    }

    public function getRightOrder(Int $idParent, string $typeObject): int
    {
        return (int) $this->categorieRepository->getRightOrder($idParent, $typeObject) + 1;
    }

    public function getRightId(string $typeObjet, int $idObjet): int
    {
        return (int) $this->categorieRepository->rqGetLastIdFromCateg($typeObjet, $idObjet);
    }

    public function hasData(Int $idCategory): bool
    {
        return sizeof($this->donneeRepository->getListeDonee('B', 0, $idCategory)) > 0;
    }
    
    
    
    public function reOrderAnomalie(string $typeObjet, int $idObjet, int $parent): void
    {
        $erreur = $this->categorieRepository->orderIsError($typeObjet, $idObjet, $parent);
        if ($erreur) {
            $listCat = $this->doarCat->fetchAll([], ['filter_egal_TYPE_OBJET' => $typeObjet,'filter_egal_ID_OBJET' => $idObjet,'filter_egal_ID_PARENT' => $parent, 'tri_NUM_ORDRE' => 'asc']);
            $i = 1;
            
            foreach ($listCat as $cat) {
                $this->doarCat->update(['NUM_ORDRE' => $i], $cat['ID']);
                $i++;
            }
        }
    }
    
    public function reOrder(string $typeObjet, int $idObjet, int $idCat, int $oldParent, int $parent, int $oldPosition, int $position): void
    {
        //Le treeview calcule les position lui même, il faut ajouter 1 pour avoir la cohérence avec la BDD
        $oldPosition = $oldPosition+1;
        $position = $position+1;
        $this->reOrderAnomalie($typeObjet, $idObjet, $oldParent);

        if ($oldParent != $parent) {
            $this->reOrderAnomalie($typeObjet, $idObjet, $parent);
            $this->categorieRepository->reOrderOtherParent($typeObjet, $idObjet, $idCat, $oldParent, $parent, $oldPosition, $position);
            
            $this->categorieRepository->reorderCategory($typeObjet, $idObjet, $oldParent, $oldPosition);
        } else {
            $this->categorieRepository->reOrderSameParent($typeObjet, $idObjet, $idCat, $parent, $oldPosition, $position);
        }
    }
}
