<?php

namespace Modules\EtatStatistiques\Service;

use Helpers\Form\FormBuilder;
use Helpers\Trad\ITrad;

class FiltreUtilForm extends FormBuilder
{
    public function __construct(ITrad $trad)
    {
        $this
        ->addText('FENT_CODE_FILTRE', 15, [
            'validator' => ['mandatory' => true, 'format' => 'code']
        ])
        ->addText('FENT_LIB_FILTRE', 100, [
            'validator' => ['mandatory' => true]
        ])
        ->addSelect('FENT_TYPE_DONNEE', [
            // TODO : check key value
            'A' => $trad->t('Alpha-numérique'),
            'N' => $trad->t('Numérique'),
            'D' => $trad->t('Date'),
            'H' => $trad->t('Heure'),
            'L' => $trad->t('Booléen'),
        ])
        ->addSelect('FENT_TYPE_AFFICHAGE', [
            'L' => $trad->t('Liste'),
            'R' => $trad->t('Requête'),
        ])
        ->addBranch('BranchType', 'FENT_TYPE_AFFICHAGE', [
            'L' /* Liste */ =>
                (new FormBuilder())
                ->addTable(
                    'ListChoix',
                    (new FormBuilder())
                    ->addText('FLIS_VALEUR', 200)
                    ->addText('FLIS_LIBELLE', 200)
                    ->build(),
                    ['reorderable' => true]
                )
                ->build(),
            'R' /* Requete */ =>
                (new FormBuilder())
                ->addSelect('FENT_BASE_DE_DONNEE', [
                    'OXADWDB' => 'DataWarehouse (Oxa.DW)',
                    'OXASQLDB' => 'Principale (Oxa.SQL)',
                ])
                // ->addSwitch('FENT_IND_SAISIE_LIBRE') // Calculable
                ->addTextarea('ReqLibre', 3000, [
                    'validator' => ['sql' => true]
                ]) // FENT_NM_REQ_LIB
                // ->addTextarea('Where') // FENT_NM_WHERE // This field seems to have to uses
                
                // ->addSwitch('FENT_IND_PERIMETRE') // Calculable
                ->addText('FENT_NOM_CHP_PER_SOC', 500)
                ->addText('FENT_NOM_CHP_PER_CONF', 500)
                ->build()
        ]);
    }

//______________________________________________________________________________

    /**
     * @return array<\StdClass>
     */
    public function buildWithView(ITrad $trad)
    {
        $form = $this->build();
        foreach ($this->traverseForm($form) as $input) {
            if (!empty($input->label)) {
                continue;
            }
            $input->label = $this->getInputLabel($trad, $input->name);
        }
        return $form;
    }

    
    /**
     * Label is required by zoom validator and such
     * @todo move somewhere else
     */
    protected function getInputLabel(ITrad $trad, string $inputName) :string
    {
        switch ($inputName) {
            case 'FENT_CODE_FILTRE':
                return $trad->t('Code');
            case 'FENT_LIB_FILTRE':
                return $trad->t('Libellé');
            case 'FENT_TYPE_DONNEE':
                return $trad->t('Type de donnée');
            case 'FENT_TYPE_AFFICHAGE':
                return $trad->t('Type d’affichage');
            case 'FENT_BASE_DE_DONNEE':
                return $trad->t('Base de données');
            case 'ReqLibre':
                return $trad->t('Requête');
            case 'Where':
                return $trad->t('Clause');
            case 'FENT_NOM_CHP_PER_SOC':
                return $trad->t('Colonne société');
            case 'FENT_NOM_CHP_PER_CONF':
                return $trad->t('Colonne de confidentialité');

            //_____________

            case 'ListChoice':
                return $trad->t('Liste des choix possibles');
            case 'FLIS_VALEUR':
                return $trad->t('Valeur');
            case 'FLIS_LIBELLE':
                return $trad->t('Libellé');
        }
        return '';
    }
}
