<?php
namespace Helpers\Pagination;

use Skeleton\Renderer\RendererInterface;

class Paginator
{

    public $nombreArticle;
    public $nombreDePage;
    public $spliter;
    public $premierArticle;
    public $dernierArticle;
    public $pageEnCours;
    public $params;

    /**
     *  $paginator      = new \Helpers\Pagination\Paginator();
     *  $infopagination = $paginator->generate($listeFourniss, 0, $post, $this->renderer);
     *
     * @param array $article    : liste à affiché
     * @param int $spliter      : le nombre d'article qu'on veut dans la page
     * @param array $params     : $post
     * @return number|mixed
     */
    public function generate(Array &$article, int $spliter = 0, array $params = [], RendererInterface $renderer = null)
    {

        
//         if(isset($params['exportDocument']) &&  $params['exportDocument']  != '')
//         {
//             new \Helpers\ExportTo\Generate($params['exportDocument'], $params['typeExport'], $article);
//
//         }
        if (!$spliter) {
            $spliter=$_SESSION['NB_ART_PAGE_RECH'];
        }

        if (!isset($params['page'])) {
            $pageEnCours = 1;
        } else {
            $pageEnCours = $params['page'];
        }
        $this->nombreDePageTotal($article, $spliter);
        
        $this->articleAfficher($pageEnCours);
        
        return $this->genererArray($article, $params);
    }

    public function nombreDePageTotal(Array &$article, int $spliter)
    {
        $this->nombreArticle = count($article);
        
        $this->spliter = $spliter;
        
        $nombreDePageSpliter = $this->nombreArticle / $spliter;
        
        $nombreDePageSpliter = explode(".", $nombreDePageSpliter);
        
        if (isset($nombreDePageSpliter[1])) {
            $this->nombreDePage = $nombreDePageSpliter[0] + 1;
        } else {
            $this->nombreDePage = $nombreDePageSpliter[0];
        }
    }

    private function articleAfficher(int $pageEnCours = 1)
    {
        if ($this->nombreDePage != 0) {
            if ($pageEnCours >= $this->nombreDePage && $this->nombreArticle % $this->spliter != 0) {
                $nbrArticle = $this->nombreArticle % $this->spliter;
                $this->dernierArticle = ($pageEnCours * $this->spliter) - ($this->spliter - $nbrArticle);
                $this->premierArticle = $this->dernierArticle - $nbrArticle;
            } else {
                $this->dernierArticle = $pageEnCours * $this->spliter;
                $this->premierArticle = $this->dernierArticle - $this->spliter;
            }
            $this->pageEnCours = $pageEnCours;
        } else {
            $this->dernierArticle = 0;
            $this->premierArticle = 0;
            $this->pageEnCours = 1;
        }
    }

    public function genererArray(array &$article, array $params = []): array
    {
        $arrayTmp = [];
        for ($i = $this->premierArticle; $i < $this->dernierArticle; $i ++) {
            $arrayTmp[] = $article[$i];
        }
        
        $article = $arrayTmp;
        return [
            'nombreDePage' => $this->nombreDePage,
            'page' => $this->pageEnCours,
            'post' => $this->generatPostForJquery($params),
            'filter' => $params,
        ];
    }

    public function generatPostForJquery(array $params = [])
    {
        $i = 0;
        foreach ($params as $key => $value) {
            if ($i != 0) {
                $this->params .= ',';
            }
            
            if (\is_array($value)) {
                $valueString ='{';
                $arrayCountString=0;
                
                foreach ($value as $val) {
                    if ($arrayCountString!=0) {
                        $valueString .=',';
                    }
                    $valueString .= "'".$val."' : '".$val."'";
                    $arrayCountString++;
                }
                
                $valueString .='}';
            } else {
                $valueString = "'".$value."'";
            }
            
            $this->params .= "'" . $key . "' : " . $valueString;
            $i ++;
        }
        // $this->params= "'baba' : 'lolo'";
        return $this->params;
    }
    
    /**
     * initSession
     * @param array<mixed> $post
     * @return void
     */
    public static function initSession($post)
    {
        if ((isset($_SESSION['PaginatorGestion']) && ($post['page']??'1') == '1')
            || $_SERVER['REQUEST_URI'] != ($_SESSION['PaginatorGestion']['currentPage']??'')
        ) {
            unset($_SESSION['PaginatorGestion']);
        }
    }
}
