<?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 function Vvveb\__;
use Vvveb\Controller\Crud;
use function Vvveb\siteSettings;
use Vvveb\System\Cache;
use Vvveb\System\Sites;

class NewsletterList extends Crud {
	protected string $type = 'newsletter_list';

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

	protected string $controller = 'newsletter-list';

	protected string $list = 'newsletter_list';

	protected string $listController = 'lists';

	protected string $module = 'plugins/newsletter';

	function test() {
		$this->view->success[]	= __('Test sent!');
		$this->send();
	}

	function send() {
		$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';
		$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->post['newsletter'] ?? [];
		$view->newsletter['from'] = $view->newsletter['from'] ?? $site['contact-email'];

		$admin_path    = \Vvveb\adminPath();
		$newsletterUrl = $admin_path . 'index.php?module=plugins/newsletter/editor&action=newsletter';
		$previewUrl = $admin_path . 'index.php?module=plugins/newsletter/editor&action=newsletter';
		$processUrl = $admin_path . 'index.php?module=plugins/newsletter/newsletter-list&action=import';

		$view->newsletter['preview_url'] = '';
		$newsletter_post_id = $view->newsletter['newsletter_post_id'] ?? $this->request->get['newsletter_post_id'] ?? null;
		$newsletter_list_id =  $view->newsletter['newsletter_list_id'] ?? $this->request->get['newsletter_list_id'] ?? null;

		if ($newsletter_post_id) {
			$view->newsletter['newsletter_post_id'] = $newsletter_post_id;
			$postModel  = new \Vvveb\Sql\Plugins\Newsletter\Newsletter_postSQL();
			if ($post = $postModel->get(['newsletter_post_id' => $newsletter_post_id])) {
				$view->newsletter['newsletter_post_id_text'] = $post['name'];
				$view->newsletter['subject'] = $post['name'];
			}

			$view->newsletter['preview_url'] = $previewUrl . '&newsletter_post_id=' . $newsletter_post_id;
		}

		if ($newsletter_list_id) {
			$view->newsletter['newsletter_list_id'] = $newsletter_list_id;
			$listModel  = new \Vvveb\Sql\Plugins\Newsletter\Newsletter_listSQL();
			if ($list = $listModel->get(['newsletter_list_id' => $newsletter_list_id])) {
				$view->newsletter['newsletter_list_id_text'] = $list['name'];
			}
		}

		$view->newsletter['step']        = 100;
		$view->newsletter['sleep_step']  = 1000;
		$view->newsletter['sleep_email'] = 100;
		$view->preview_url               = $previewUrl;
		$view->process_url               = $processUrl;

		$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);
	}
}
