<?php

if ( ! defined( 'ABSPATH' ) ) exit;

require_once __DIR__ . "/../Kernel/Translations/DigitalSignage4_Translate.php";
require_once __DIR__ . "/../Kernel/API/Manager/DigitalSignage4_ForfaitManager.php";
require_once "DigitalSignage4_Controller_interface.php";


	/*
    if (!class_exists("Smarty")) {
        require_once __DIR__ . "/../vendor/smarty/smarty/libs/Smarty.class.php";
    }
	*/
	
	require_once __DIR__ . '/../vendor/autoload.php'; // Si vous utilisez Composer
//use Smarty\Engine\Smarty;
use Smarty\Smarty;
	/*
	require_once __DIR__ . "/../vendor/autoload.php";
use Smarty\Smarty;
$smarty = new Smarty();
*/

/*
abstract class DigitalSignage4_Controller extends Smarty implements Controller_i
{*/
abstract class DigitalSignage4_Controller implements DigitalSignage4_Controller_interface
{
    protected $smarty;
	public function __construct()
    {
        $this->smarty = new Smarty();
        // Configuration de Smarty, si nécessaire
    }
	
	public function assign($tpl_var, $value = null)
    {
        return $this->smarty->assign($tpl_var, $value);
    }
	
	public function display($template)
    {
        return $this->smarty->display($template);
    }
	
    protected function setWPTemplate($link)
    {
        $this->setGlonals();
        $this->checkUpdate();
        $this->checkWooCommerceInstance();
        $this->trialMessage();
        $this->my_admin_notices();

        try {
            $this->assign('t', get_class(new DigitalSignage4_Translate()));
            $this->display(plugin_dir_path(__FILE__) . "../Views/templates/admin/" . $link . ".tpl");
        } catch (\SmartyException  $e) {
            echo (esc_html($e->getMessage()));
        } catch (\Exception  $e) {
            echo esc_html($e->getMessage());
        }
    }

    public function my_admin_notices()
    {

        //array_unique supprimes les doublons pouvans etre generés dans le main du module (digitalsignage.php) car il est executé plusieurs fois avant l'ouverture d'une page.
        $errors = array_unique((isset($_SESSION['Errors'])) ? map_deep($_SESSION['Errors'], "sanitize_text_field") : array());
        foreach ($errors as $error) {
            echo ('<div style="font-size: 1.2em" class="error notice is-dismissible auto-dismiss"><p>' . esc_html($error) . '</p></div>');
        }

        //array_unique supprimes les doublons pouvans etre generés dans le main du module (digitalsignage.php) car il est executé plusieurs fois avant l'ouverture d'une page.
        $success = array_unique((isset($_SESSION['Success'])) ? map_deep($_SESSION['Success'], "sanitize_text_field") : array());
        foreach ($success as $succes) {
            echo ('<div style="font-size: 1.2em" class="updated notice is-dismissible auto-dismiss"><p>' . esc_html($succes) . '</p></div>');
        }

        unset($_SESSION['Errors']);
        unset($_SESSION['Success']);
    }

    private function checkWooCommerceInstance()
    {
        if (!DigitalSignage4_Tools::isPrestashop()) {
            if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))) || !class_exists('WooCommerce')) {
                $_SESSION['Errors'][] = DigitalSignage4_Translate::getTraduction("Extension_wooCommerce_pas_installe", DigitalSignage4_Translate::getTraduction("Digital_Signage_4"));
            }
        }
    }

    private function setGlonals()
    {
        if (! isset($_SESSION['Errors'])) {
            $_SESSION['Errors'] = array();
        }
        if (! isset($_SESSION['Success'])) {
            $_SESSION['Success'] = array();
        }
    }

    private function trialMessage()
    {
        $forfait = DigitalSignage4_ForfaitManager::instance()->getForfait();
		
		

        if ($forfait && $forfait->getTrial() == true) {
            echo ('<div style="font-size: 1.2em; border-left: 4px solid #25b9d7;" class="info notice is-dismissible auto-dismiss"><p>');
			$allowed_html = array(
									'strong' => array(),
									'b' => array(),
									'br' => array(),
									'a' => array(
										'href' => array(),
										'title' => array(),
										'target'=>array(),
									),
								);
								
            echo wp_kses(DigitalSignage4_Translate::getTraduction("Message_compte_essai", DigitalSignage4_Translate::getTraduction("Digital_Signage_4")),$allowed_html);
            echo ('<br>'.wp_kses(DigitalSignage4_Translate::getTraduction("Message_upgrade_compte", DigitalSignage4_Tools::getAcaciaRedirectionPageUrl("account&token=".DigitalSignage4_ForfaitManager::instance()->getToken(),"yes")),$allowed_html).'</p></div>');
        }
    }

    private function checkUpdate(){
    }
}
