<?php

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

require_once __DIR__ . "/../Kernel/Translations/Translate.php";
require_once __DIR__ . "/../Kernel/API/Manager/DigitalSignage4_ForfaitManager.php";
require_once __DIR__ . "/../vendor/smarty/smarty/libs/Smarty.class.php";
require_once "DigitalSignage4_Controller_interface.php";

abstract class DigitalSignage4_Controller2 extends Smarty implements DigitalSignage4_Controller_interface
{
    protected function setWPTemplate($link)
    {
        $this->setGlonals();
        $this->checkWooCommerceInstance();
        $this->trialMessage();
        $this->my_admin_notices();

        try {
            $this->assign('t', get_class(new 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 (! in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins'))) || ! class_exists('WooCommerce')) {
            $_SESSION['Errors'][] = Translate::getTraduction("Extension_wooCommerce_pas_installe", 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( Translate::getTraduction("Message_compte_essai", Translate::getTraduction("Digital_Signage_4")), $allowed_html );
								
            
            echo '<br>'.wp_kses((Translate::getTraduction("Message_upgrade_compte")), $allowed_html ).'</p></div>';
        }
    }
}
