<?php

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

    require_once __DIR__ . "/../../../Kernel/API/Entity/DigitalSignage4_GabaritEntity.php";
    require_once __DIR__ . "/../../../Kernel/API/Linker/DigitalSignage4_API_Linker.php";
    require_once __DIR__ . "/../../../Kernel/Translations/DigitalSignage4_Translate.php";
    require_once "DigitalSignage4_Manager.php";
	
    class DigitalSignage4_GabaritManager extends DigitalSignage4_Manager
    {
        // Singleton
        private static $instance = null;

        public function __construct()
        {
        }

        public static function instance()
        {
            if (self::$instance == null) {
                self::$instance = new DigitalSignage4_GabaritManager();
            }

            return self::$instance;
        }


        private $managerEntityName = "Gabarit";
        private $gabarits = array();

        /**
         * @return DigitalSignage4_GabaritEntity[]|false
         */
        public function getGabarits()
        {
            if (empty($this->gabarits)) {
                try {
                    $this->gabarits = DigitalSignage4_API_Linker::get_entitys($this->managerEntityName, DigitalSignage4_GabaritEntity::class, DigitalSignage4_ForfaitManager::instance()->getToken());

                    if (! $this->gabarits) {
                        $this->gabarits = array();
                    }
                } catch (DigitalSignage4_Exception $e) {
                    $this->gabarits = array();
                    $this->addUserMessage(DigitalSignage4_Translate::getTraduction('error_api', $e->getMessage()));
                }
            }

            return array_values($this->gabarits);
        }

        public function getGabarit($id)
        {
            foreach ($this->getGabarits() as $gabarit) {
                if ($gabarit->getId() == $id) {
                    return $gabarit;
                }
            }

            return false;
        }
    }
