<?php

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

require_once __DIR__ ."/../../../Kernel/API/Entity/DigitalSignage4_AutomatisationEntity.php";
require_once __DIR__ ."/../../../Kernel/API/Linker/DigitalSignage4_CMS_Linker.php";
require_once __DIR__ ."/../../../Kernel/Translations/DigitalSignage4_Translate.php";
require_once "DigitalSignage4_Manager.php";

class DigitalSignage4_AutomatisationManager extends DigitalSignage4_Manager
{
    /**
     * @var DigitalSignage4_CMS_Linker|null
     */
    private $cms = null;
    private $entity_name = "DigitalSignage_automatisation";

    /**
     * @param DigitalSignage4_CMS_Linker $cms_linker
     * @throws DigitalSignage4_Exception
     */
    public function __construct($cms_linker)
    {
        if (!$cms_linker instanceof DigitalSignage4_CMS_Linker) {
            throw new DigitalSignage4_Exception(esc_html(DigitalSignage4_Translate::getTraduction('mauvais_cms_linker')));
        } else {
            $this->cms = $cms_linker;
        }
    }

    /**
     * @throws DigitalSignage4_Exception
     */
    public static function set($cms_linker)
    {
        if (!$cms_linker instanceof DigitalSignage4_CMS_Linker) {
            throw new DigitalSignage4_Exception(esc_html(DigitalSignage4_Translate::getTraduction('mauvais_cms_linker')));
        } else {
            return new DigitalSignage4_AutomatisationManager($cms_linker);
        }
    }

    /**
     * @param DigitalSignage4_AutomatisationEntity $automatisation
     * @throws DigitalSignage4_Exception
     */
    public function add_automatisation($automatisation)
    {
        if ($automatisation instanceof DigitalSignage4_AutomatisationEntity) {
            $automatisations_existantes  = $this->cms->get_data($this->entity_name);

            if (!$automatisations_existantes) {
                $automatisations_existantes = array();
            }

            $automatisations_existantes[] = $automatisation;
            if ($this->cms->save_data($this->entity_name, $automatisations_existantes)) {
                return true;
            }
        } else {
            throw new DigitalSignage4_Exception(esc_html(DigitalSignage4_Translate::getTraduction('mauvais_parametre_type', 'add_automatisation', 'DigitalSignage4_AutomatisationEntity')));
        }

        return false;
    }

    /**
     * @param DigitalSignage4_AutomatisationEntity $automatisation
     * @throws DigitalSignage4_Exception
     */
    public function remove_automatisation($automatisation)
    {
        if ($automatisation instanceof DigitalSignage4_AutomatisationEntity) {
            $automatisations_existantes  = $this->cms->get_data($this->entity_name);

            foreach ($automatisations_existantes  as $key => $automatisations_existante) {
                if ($automatisations_existante->getId() == $automatisation->getId()) {
                    unset($automatisations_existantes[$key]);
                }
            }

            if ($this->cms->save_data($this->entity_name, $automatisations_existantes)) {
                return true;
            }
        } else {
            throw new DigitalSignage4_Exception(esc_html(DigitalSignage4_Translate::getTraduction('mauvais_parametre_type', 'remove_automatisation', 'DigitalSignage4_AutomatisationEntity')));
        }

        return false;
    }

    /**
     * @return DigitalSignage4_AutomatisationEntity[]
     */
    public function get_automatisations()
    {
        $automatisations_existantes  = $this->cms->get_data($this->entity_name);

        if (!$automatisations_existantes) {
            $automatisations_existantes = array();
        }

        return $automatisations_existantes;
    }

    /**
     * @param $id
     * @return array|mixed
     */
    public function get_automatisation($id)
    {

        /**
         * @var DigitalSignage4_AutomatisationEntity[] $automatisations_existantes
         */
        $automatisations_existantes  = $this->cms->get_data($this->entity_name);

        if (!$automatisations_existantes) {
            $automatisations_existantes = array();
        }

        foreach ($automatisations_existantes as $automatisation_existante) {
            if ($automatisation_existante->getId() == $id) {
                return $automatisation_existante;
            }
        }

        return false;
    }

    public function get_last_id()
    {
        $automatisations_existantes  = $this->cms->get_data($this->entity_name);
        $last_id = 0;
        foreach ($automatisations_existantes as $automatisation_existante) {
            if ($automatisation_existante->getId() > $last_id) {
                $last_id = $automatisation_existante->getId();
            }
        }
        return $last_id;
    }



    public function update_automatisation($automatisation)
    {
        $automatisations_existantes  = $this->cms->get_data($this->entity_name);

        foreach ($automatisations_existantes as $key => $automatisation_existante) {
            if ($automatisation->getId() == $automatisation_existante->getId()) {
                $automatisations_existantes[$key] = $automatisation;
            }
        }

        return $this->cms->save_data($this->entity_name, $automatisations_existantes);
    }
}
