<?php

/**
 * FanPress CM 5.x
 * @license http://www.gnu.org/licenses/gpl.txt GPLv3
 */

namespace fpcm\components\editor;

/**
 * Article editor plugin base model
 *
 * @abstract
 * @package fpcm\components\editor
 * @author Stefan Seehafer aka imagine <fanpress@nobody-knows.org>
 * @copyright (c) 2011-2022, Stefan Seehafer
 * @license http://www.gnu.org/licenses/gpl.txt GPLv3
 * @since 3.1.0
 */
abstract class articleEditor extends \fpcm\model\abstracts\staticModel {

    /**
     * Dateilisten-Objekt
     * @var \fpcm\model\files\mediaFilesList
     */
    protected $fileList;

    /**
     * Konstruktor
     */
    public function __construct()
    {
        parent::__construct();
        $this->fileList = new \fpcm\model\files\mediaFilesList();
    }

    /**
     * Pfad der Editor-Template-Datei
     * @return string
     */
    abstract public function getEditorTemplate();

    /**
     * Pfad der Kommentar-Editor-Template-Datei
     * @return string
     */
    abstract public function getCommentEditorTemplate();

    /**
     * Liefert zu ladender Javascript-Dateien für Editor zurück
     * @return array
     */
    abstract public function getJsFiles() : array;

    /**
     * Liefert zu ladender CSS-Dateien für Editor zurück
     * @return array
     */
    abstract public function getCssFiles() : array;

    /**
     * Array von Javascript-Variablen, welche in Editor-Template genutzt werden
     * @return array
     */
    abstract public function getJsVars();

    /**
     * Array von Variablen, welche in Editor-Template genutzt werden
     * @return array
     */
    abstract public function getViewVars();

    /**
     * Array von Sprachvariablen für Nutzung in Javascript
     * @return array
     * @since 3.3
     */
    abstract public function getJsLangVars() : array;

    /**
     * Arary mit Informationen u. a. für template-Plugin von TinyMCE
     * @see \fpcm\model\abstracts\articleEditor::getTemplateDrafts()
     * @return array
     */
    public function getTemplateDrafts() : array
    {
        $templatefilelist = new \fpcm\model\files\templatefilelist();

        $ret = [];
        foreach ($templatefilelist->getFolderList() as $file) {

            $basename = basename($file);

            if ($basename === 'index.html') {
                continue;
            }

            $ret[$basename] = $basename;
        }

        return $ret;
    }

    /**
     * Returns a list of dialogs
     * @since 5.3.0
     * @return array
     */
    public function getDialogs() : array
    {
        return [];
    }

    /**
     * Editor-Styles initialisieren
     * @return array
     */
    protected function getEditorStyles() : array
    {
        if (!trim($this->config->system_editor_css)) {
            return [];
        }

        $classes = explode(PHP_EOL, preg_replace('/[\.\{\}]+/i', '', $this->config->system_editor_css));

        $editorStyles = [];
        foreach ($classes as $class) {
            $class = trim(trim($class, '.[}'));
            $editorStyles[$class] = $class;
        }

        $ev = $this->events->trigger('editor\addStyles', $editorStyles);
        if (!$ev->getSuccessed() || !$ev->getContinue()) {
            trigger_error(sprintf("Event editor\addStyles failed. Returned success = %s, continue = %s", $ev->getSuccessed(), $ev->getContinue()));
            return [];
        }

        $styles = $ev->getData();
        if (!is_array($styles) || !count($styles)) {
            return [];
        }

        return $styles;
    }

    /**
     * Editor-Links initialisieren
     * @return string
     */
    public function getEditorLinks(string $labelString = 'label') : array
    {
        $ev = $this->events->trigger('editor\addLinks');
        if (!$ev->getSuccessed() || !$ev->getContinue()) {
            trigger_error(sprintf("Event editor\addLinks failed. Returned success = %s, continue = %s", $ev->getSuccessed(), $ev->getContinue()));
            return [];
        }

        $links = $ev->getData();
        if (!is_array($links) || !count($links)) {
            return [];
        }

        return $links;
    }

    /**
     * Dateiliste initialisieren
     * @return array
     */
    public function getFileList(string $labelString = '') : array
    {
        if (!$labelString) {
            $labelString = static::FILELIST_LABEL;
        }

        $data = [];
        foreach ($this->fileList->getDatabaseList() as $image) {

            $base = basename($image->getFilename());

            $data[] = [
                $labelString => $image->getAltText() ? $image->getAltText() . " ({$base})" : $base,
                static::FILELIST_VALUE => $image->getImageUrl()
            ];

        }

        $ev = $this->events->trigger('editor\getFileList', [
            'label' => 'label',
            'files' => $data
        ]);

        if (!$ev->getSuccessed() || !$ev->getContinue()) {
            trigger_error(sprintf("Event editor\getFileList failed. Returned success = %s, continue = %s", $ev->getSuccessed(), $ev->getContinue()));
            return $data;
        }

        $res = $ev->getData();
        return $res['files'] ?? [];
    }

    /**
     * Gibt Textpattern-Konfiguration zurück,
     * nur in TinyMCE genutzt
     * @return array
     */
    protected function getTextPatterns() : array
    {
        return [
            ['start' => '- ', 'cmd' => 'InsertUnorderedList'],
            ['start' => '* ', 'cmd' => 'InsertUnorderedList'],
            ['start' => '# ', 'cmd' => 'InsertOrderedList'],
            ['start' => '1. ', 'cmd' => 'InsertOrderedList'],
            ['start' => '_', 'end' => '_', 'format' => 'italic'],
            ['start' => '*', 'end' => '*', 'format' => 'bold'],
        ];
    }

}
