<?php 


declare(strict_types=1);

namespace PrestaShop\Module\LCVPrestaTheme\Config;


use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

/**
 * [obj parametrage theme]
 */
class ConfigTheme {

    /**
     * Chemin de stockage par défaut de la configuration du thème
     */
    const themeConfigFile = _PS_ROOT_DIR_ . '/themes/config2.json';

    /**
     * description des themes 
     *
     * @var ConfigGeneral[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigGenral>
     */
    public $configGeneral = [
        [
            "primary" => "#f31212",
            "secondary" => "#000000",
            "fontTitle" => "lato",
            "fontText" => "lato",
            "container" => "contained"
        ]
    ];


    /**
     * description des header
     *
     * @var ConfigHeader[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigHeader>
     */
    public $configHeader = [
        [
            "header" => "header1",
            "background" => "#ffffff",
            "color" => "#000000"
        ]
    ];


    /**
     * description des footer
     *
     * @var ConfigFooter[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigFooter>
     */
    public $configFooter = [
        [
            "footer" => "footer1",
            "background" => "#f8f8f8",
            "color" => "#666666"
        ]
    ];


    /**
     * description des resaux sociaux
     *
     * @var ConfigResaux[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigResaux>
     */
    public $configResaux = [
        [
            "image" => "/modules/lcvprestatheme/img/tiktok.png",
            "url" => "http:\/\/localhost\/",
            "isVisible" => true
        ],
        [
            "image" => "/modules/lcvprestatheme/img/facebook.png",
            "url" => "http:\/\/localhost\/",
            "isVisible" => true
        ],
        [
            "image" => "/modules/lcvprestatheme/img/instagram.png",
            "url" => "/",
            "isVisible" => true
        ]

    ];

     /**
     * Group qui contien les items categorie image
     *
     * @var ConfigCategory[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigCategory>
     */
    public $configCategory = [];

    /**
     * Group qui contien les items categorie image
     *
     * @var ConfigCategoryGroup[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigCategoryGroup>
     */
    public $configCategoryGroup = [];


   /**
     * Group qui contien les items du block produit 
     *
     * @var ConfigProductHomeGroup[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigProductHomeGroup>
     */
    public $configProductHomeGroup = [];

    /**
     * Group qui contien les items categorie image
     *
     * @var ConfigProductHome[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigProductHome>
     */
    public $configProductHome = [];



    /**
     * Group qui contien les items slider 
     *
     * @var ConfigAbout[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigAbout>
     */
    public $configAbout = [];
    
    /**
     * Group qui contien les items categorie image
     *
     * @var ConfigAboutGroup[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigAboutGroup>
     */
    public $configAboutGroup = [];

    
    /**
     * Group qui contien les items slider 
     *
     * @var ConfigSliderGroup[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigSliderGroup>
     */
    public $configSliderGroup = [];

  /**
     * Group qui contien les items slider 
     *
     * @var ConfigSlider[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigSlider>
     */
    public $configSlider= [];
    
    
    /**
     * Group qui contien les items slider 
     *
     * @var ConfigReassuranceGroup[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigReassuranceGroup>
     */
    public $configReassuranceGroup = [];

       
    /**
     * description de la reassurance
     *
     * @var ConfigReassurance[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigReassurance>
     */
    public $configReassurance = [];





    
     /**
     * description de la livraison
     *
     * @var ConfigLivraison[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigLivraison>
     */
    public $configLivraison = [
        [
            "isVisible" => true,
            "freeDelivery" => 50,
            "deliveryTime" => 3,
            "shippingTime" => 14,
            "noShippingDays" => [
                "saturday",
                "sunday"
            ]
        ]
    ];

        
     /**
     * description de la page catalogue
     *
     * @var ConfigCatalogue[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigCatalogue>
     */
    public $configCatalogue = [
        [
            "color" => false,
            "size" => false,
            "show" => false,
            "colProduct" => 4,
            "pageProduct" => 12,
            "filter" => "filtreGauche",
            "paginationMode" => "infinite"
        ]
    ];


    /**
     * description de la page produit
     *
     * @var ConfigProduct[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigProduct>
     */
    public $configProduct = [
        [
            "imagePosition" => "imagesuppGauche",
            "reduction" => true,
            "quantity" => false,
            "information" => "information2"
        ]
    ];


    /**
     * description du bandeau en haut de page
     *
     * @var ConfigTopBandeau[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigTopBandeau>
     */
    public $configTopBandeau = [
        [
            "text" => "Livraison gratuite à partir de 50€",
            "image" => "",
            "isVisible" => true
        ],
        [
            "text" => "Paiement sécurisé avec le 3DSECURE",
            "image" => "",
            "isVisible" => true
        ]
    ];
    

    
    /**
     * parametre du bandeau en haut de page
     *
     * @var ConfigTopBandeauParameter[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigTopBandeauParameter>
     */
    public $configTopBandeauParameter = [
        [
             "background" => "#000000",
            "color" => "#ffffff",
            "isVisible" => true
        ]
    ];


    /**
     * Ordre personnalisé des blocs de la home
     *
     * @var string[]
     * @Type Array<string>
     */
    public $configBlockHome = [];


     /**
     * Configuration du bloc marque
     *
     * @var ConfigBrand[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigBrand>
     */
    public $configBrand = [];


    /**
     * Configuration du bloc menu parent
     *
     * @var ConfigMenuParent[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigMenuParent>
     */
    public $configMenuParent = [];

  /**
     * Configuration du bloc menu child
     *
     * @var ConfigMenuChild[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigMenuChild>
     */
    public $configMenuChild = [];


    /**
     * Configuration de Google Tag Manager
     *
     * @var ConfigTracking[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigTracking>
     */
    public $configTracking = [];


    /**
     * Configuration des Media du Menu
     *
     * @var ConfigMenuMedia[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigMenuMedia>
     */
    public $configMenuMedia = [];


    /**
     * Configuration du pop-up
     *
     * @var ConfigPopUp[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigPopUp>
     */
    public $configPopUp = [];


    
    /**
     * Customisation du CSS
     *
     * @var ConfigCss[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigCss>
     */
    public $configCss = [];


    /**
     * store localisation
     *
     * @var ConfigStore[]
     * @Type Array<PrestaShop\Module\LCVPrestaTheme\Config\ConfigStore>
     */
    public $configStore = [];


        
    /**
     * Obtient un serializer compatible JSON capable de sérialiser un objet de type ConfigTheme     
     *
     * @return Serializer
     */
    private static function getSerializer() : Serializer
    {
        return new Serializer( [
            new ObjectNormalizer(null, null, null, 
                new PropertyInfoExtractor([], [
                    new PhpDocExtractor(),
                    new ReflectionExtractor(),
                ])), 
            new ArrayDenormalizer()
        ], [new JsonEncoder()]);
    }

    public static function load(string $configFile = self::themeConfigFile) : ConfigTheme {
        if (!file_exists($configFile)){
            $fichierJson = new ConfigTheme();
            $fichierJson->save();
        }
        
        //throw new \Exception("fichier de configuration de thème introuvable");
        $content = file_get_contents($configFile);
        if(!$content) {
            $fichierJson = new ConfigTheme();
            $fichierJson->save($configFile);
            $content = file_get_contents($configFile);
        }
        
        $config = self::getSerializer()->deserialize($content, self::class, 'json');
        

    
        
        // Les éléments uniques
        if (!$config->configGeneral) 
            $config->configGeneral = [new ConfigGeneral()];
        if (!$config->configHeader) 
            $config->configHeader = [new ConfigHeader()];   
        if (!$config->configFooter)
            $config->configFooter = [new ConfigFooter()];
        if (!$config->configCatalogue)
            $config->configCatalogue = [new ConfigCatalogue()];        

    

        if (!$config->configLivraison)
            $config->configLivraison = [new configLivraison()];

        if (!$config->configProduct)
            $config->configProduct = [new ConfigProduct()];

        if (!$config->configCatalogue)
            $config->configCatalogue = [new ConfigCatalogue()];

        if (!$config->configTopBandeauParameter)
            $config->configTopBandeauParameter = [new ConfigTopBandeauParameter()];

        if (!$config->configTracking)
            $config->configTracking = [new ConfigTracking()];            
        
        if (!isset($config->configResaux)) $config->configResaux = [];           
        if (!isset($config->configTopBandeau)) $config->configTopBandeau = [];
        if (!isset($config->configMenuParent)) $config->configMenuParent = [];
        if (!isset($config->configMenuChild)) $config->configMenuChild = [];
        if (!isset($config->configTracking)) $config->configTracking = [];
        if (!isset($config->configMenuMedia)) $config->configMenuMedia = [];
        if (!isset($config->configReassurance)) $config->configReassurance = [];  

        
        return $config;
    }

    public function save(string $outputFile = self::themeConfigFile) {       
        $jsonContent = self::getSerializer()->serialize($this, 'json', ['json_encode_options' => \JSON_PRETTY_PRINT]);
        file_put_contents($outputFile, $jsonContent);
    }



}