<?php

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

require_once __DIR__ ."/../../../Kernel/API/Entity/DigitalSignage4_PlaylistEntity.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_PlaylistManager extends DigitalSignage4_Manager
{
    /**
     * @var DigitalSignage4_CMS_Linker|null
     */
    private $cms = null;
    private $entity_name = "DigitalSignage_playlist";

    /**
     * @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_PlaylistManager($cms_linker);
        }
    }

    /**
     * @param DigitalSignage4_PlaylistEntity $playlist
     *
     * @throws DigitalSignage4_Exception
     */
    public function add_playlist($playlist)
    {
        if ($playlist instanceof DigitalSignage4_PlaylistEntity) {
            $playlists_existantes = $this->cms->get_data($this->entity_name);

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

            $playlists_existantes[] = $playlist;

            return $this->cms->save_data($this->entity_name, $playlists_existantes);
        } else {
            throw new DigitalSignage4_Exception(esc_html(DigitalSignage4_Translate::getTraduction('mauvais_parametre_type', 'add_playlist', 'DigitalSignage4_PlaylistEntity')));
        }
    }

    /**
     * @param DigitalSignage4_PlaylistEntity $playlist
     *
     * @throws DigitalSignage4_Exception
     */
    public function remove_playlist($playlist)
    {
        if ($playlist instanceof DigitalSignage4_PlaylistEntity) {
            $playlists_existantes = $this->cms->get_data($this->entity_name);

            if (($key = array_search($playlist, $playlists_existantes)) !== false) {
                unset($playlists_existantes[$key]);
            }

            if ($this->cms->save_data($this->entity_name, $playlists_existantes)) {
                return true;
            }

            return false;
        } else {
            throw new DigitalSignage4_Exception(esc_html(DigitalSignage4_Translate::getTraduction('mauvais_parametre_type', 'remove_playlist', 'DigitalSignage4_PlaylistEntity')));
        }
    }

    /**
     * @return DigitalSignage4_PlaylistEntity[]|mixed
     */
    public function get_playlists()
    {
        $playlists_existantes = $this->cms->get_data($this->entity_name);

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

        return $playlists_existantes;
    }

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

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

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

        foreach ($playlists_existantes as $playlist_existante) {
            if ($playlist_existante->get_id() == $id) {
                return $playlist_existante;
            }
        }

        return false;
    }

    public function get_last_id()
    {
        $playlists_existantes = $this->cms->get_data($this->entity_name);
        $last_id              = 0;

        if ($playlists_existantes) {
            foreach ($playlists_existantes as $playlist_existante) {
                if ($playlist_existante->get_id() > $last_id) {
                    $last_id = $playlist_existante->get_id();
                }
            }
        }

        return $last_id;
    }

    /**
     * @param DigitalSignage4_PlaylistEntity $playlist
     */
    public function update_playlist($playlist)
    {
        $playlists_existantes = $this->cms->get_data($this->entity_name);
        foreach ($playlists_existantes as $key => $playlist_existante) {
            if (intval($playlist->get_id()) == intval($playlist_existante->get_id())) {
                $playlists_existantes[$key] = $playlist;
                return $this->cms->save_data($this->entity_name, $playlists_existantes);
            }
        }
        return false;
    }
}
