<?php
namespace TwigExtension;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class TriTableExtension extends AbstractExtension
{

    public function getFunctions()
    {
        return [
            new TwigFunction('triTable', $this->triTable(...), [
                'is_safe' => [
                    'html'
                ]
            ]),
            new TwigFunction('sortTable', $this->sortTable(...), [
                'is_safe' => [
                    'html'
                ]
            ])
        ];
    }
    
    /**
     * sortTable
     *
     * $params
     *      key         : hidden input id = tri_key
     *      value       : sent by clicking 'asc', 'desc' or ''
     *      title       : table column name
     *      div         :
     *      path        :
     *      formName    : name of the form
     *
     * @param  array<string> $params
     * @return string
     */
    public function sortTable(array $params)
    {
        return $this->triTable(
            $params['key'],
            $params['value'],
            $params['title'],
            $params['div'] ?? null,
            $params['path'] ?? null,
            $params['formName'] ?? null
        );
    }
    
    /**
     * @param ?string $value
     * @param string $titre
     * @param string $div
     * @param string $path
     * @param string $nomForm
     * @return string
     */
    public function triTable(string $key, $value, $titre, $div = null, $path = null, $nomForm = null)
    {
        $html='';
        $key = str_replace('#point#', '.', $key);
        $value = strtolower($value ?? '');

        // TODO : factorize and modernize
        if ($div == null) {
            $targetForm = empty($nomForm) ? "'form:first'" : "'#' + CSS.escape(".json_encode($nomForm).")";
            if ($value =='') {
                $html = '<span class="trier default" onclick="$(\'#tri_'.$key.'\').val(\'asc\');$('.htmlspecialchars($targetForm).').submit();">'.$titre;
            }

            if ($value =='asc') {
                $html ='<span class="trier up" onclick="$(\'#tri_'.$key.'\').val(\'desc\');$('.htmlspecialchars($targetForm).').submit();">'.
                $titre.'';
            }
            if ($value =='desc') {
                $html ='<span class="trier down" onclick="$(\'#tri_'.$key.'\').val(\'\');$('.htmlspecialchars($targetForm).').submit();">'.
                $titre.'';
            }
        } else {
            if ($value =='') {
                $html = '<span class="trier default" onclick="$(\'#tri_'.$key.'\').val(\'asc\');ajaxRechercheTableau(\''.$path.'\', \''.$div.'\', \''.$nomForm.'\');">'.$titre;
            }

            if ($value =='asc') {
                $html = '<span class="trier up" onclick="$(\'#tri_'.$key.'\').val(\'desc\');ajaxRechercheTableau(\''.$path.'\', \''.$div.'\', \''.$nomForm.'\');">'.
                $titre.'';
            }
            if ($value =='desc') {
                $html = '<span class="trier down" onclick="$(\'#tri_'.$key.'\').val(\'\');ajaxRechercheTableau(\''.$path.'\', \''.$div.'\', \''.$nomForm.'\');">'.
                $titre.'';
            }
        }

        return $html . ('</span><input type="hidden" name="tri_' . $key . '" id="tri_' . $key . '" value="' . $value . '" />');
    }
}
