<?php

/**
 * Vvveb
 *
 * Copyright (C) 2022  Ziadin Givan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

namespace Vvveb\Plugins\Newsletter\Controller;

use Vvveb\Controller\Crud;
use Vvveb\Plugins\Newsletter\System\Functions;
use function Vvveb\siteSettings;
use Vvveb\System\Cache;
use Vvveb\System\Sites;


class NewsletterPost extends Crud {
	protected string $type = 'newsletter_post';

	protected string $modelName = 'Plugins\Newsletter\newsletter_post';

	//protected $model = 'newsletter_post';

	protected string $controller = 'newsletter-post';

	protected $post = 'newsletter_post';

	protected $postController = 'posts';

	protected string $module = 'plugins/newsletter';

	function index(): void {
		$newsletter_post_id = $this->request->get['newsletter_post_id'] ?? null;
		$view = $this->view;

		$admin_path  = \Vvveb\adminPath();

		$controllerPath  = $admin_path . 'index.php?module=media/media';
		$view->scanUrl   = "$controllerPath&action=scan";
		$view->uploadUrl = "$controllerPath&action=upload";
		$view->linkUrl   = $admin_path . 'index.php?module=content/post&action=urlAutocomplete';
		$view->send_url    = $admin_path . 'index.php?module=plugins/newsletter/newsletter-send&newsletter_post_id=' . $newsletter_post_id;
		$view->design_url   = $admin_path . 'index.php?module=plugins/newsletter/editor&newsletter_post_id=' . $newsletter_post_id;
		$theme           = Sites::getTheme(SITE_ID) ?? 'default';
		$view->themeCss  = '';

		if (file_exists(DIR_THEMES . "$theme/css/admin-post-editor.css")) {
			$view->themeCss .= PUBLIC_PATH . "themes/$theme/css/admin-post-editor.css";
		} else {
			if (file_exists(DIR_THEMES . "$theme/css/style.css")) {
				$view->themeCss .= PUBLIC_PATH . "themes/$theme/css/style.css";
			}
		}

		foreach (['custom.css', 'fonts.css'] as $css) {
			if (file_exists(DIR_THEMES . "$theme/css/$css")) {
				$view->themeCss .= ',' . PUBLIC_PATH . "themes/$theme/css/$css";
			}
		}

		$site = siteSettings();
		$view->newsletter = $this->request->get['newsletter'] ?? [];
		$view->newsletter['from'] = $view->newsletter['from'] ?? $site['contact-email'];

		$view->themeFonts = Cache::getInstance()->cache(APP, 'fonts-list-' . $theme, function () use ($theme) {
			$fonts = \Vvveb\System\Media\Font::themeFonts($theme);
			$names = [];

			if ($fonts) {
				foreach ($fonts as $font) {
					if (isset($font['font-family'])) {
						$names[$font['font-family']] = null;
					}
				}
			}

			return $names;
		}, 604800);



		//templates
		$folder = DIR_PLUGINS . 'newsletter' . DS . 'public' . DS . 'templates';
		$templates  = Functions::templatesList($folder);

		//change file path to url
		array_walk_recursive($templates, function (&$file) {
			$template = str_replace(DIR_PLUGINS . 'newsletter' . DS . 'public' . DS, '', $file);
			$file = PUBLIC_PATH . 'plugins/newsletter/' . $template;
		});

		$theme  = Sites::getTheme($this->global['site_id']);
		$folder = DIR_THEMES . $theme . DS . 'email';
		$themeTemplates  = Functions::templatesList($folder);

		//change file path to url
		array_walk_recursive($themeTemplates, function (&$file) use ($theme) {
			$template = str_replace(DIR_PUBLIC, '', $file);
			$file = PUBLIC_PATH . $template;
		});

		$view->templates = $templates + $themeTemplates;
		$view->publicPath = PUBLIC_PATH;

		parent::index();
	}
}
