<?php
namespace Helpers\Pagination;

use Skeleton\Renderer\RendererInterface;

class Paginator
{

    public int $nombreArticle;
    public mixed $nombreDePage;
    public mixed $spliter;
    public mixed $premierArticle;
    public mixed $dernierArticle;
    public mixed $pageEnCours;

    /**
     *  $paginator      = new \Helpers\Pagination\Paginator();
     *  $infopagination = $paginator->generate($listeFourniss, 0, $post, $this->renderer);
     *
     * @param array<mixed> $article    : liste à affiché
     * @param int $spliter      : le nombre d'article qu'on veut dans la page
     * @param array<mixed> $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);
    }

    /**
     * @param array<mixed> $article
     */
    public function nombreDePageTotal(array $article, int $spliter) :void
    {
        $this->nombreArticle = count($article);
        
        $this->spliter = $spliter;
        
        $nombreDePageSpliter = $this->nombreArticle / $spliter;
        
        $nombreDePageSpliter = explode(".", (string)$nombreDePageSpliter);
        
        if (isset($nombreDePageSpliter[1])) {
            $this->nombreDePage = intval($nombreDePageSpliter[0]) + 1;
        } else {
            $this->nombreDePage = intval($nombreDePageSpliter[0]);
        }
    }

    private function articleAfficher(int $pageEnCours = 1) :void
    {
        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;
        }
    }

    /**
     * @param array<mixed> $article
     * @param array<mixed> $params
     * @return array<mixed>
     */
    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,
        ];
    }

    /**
     * @param array<mixed> $params
     */
    public function generatPostForJquery(array $params = []) :string
    {
        $this_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']['infoPagination']['currentPage']??'')
        ) {
            unset($_SESSION['PaginatorGestion']);
        }
    }
}
