<?php

/**
 * @package Dotclear
 * @subpackage Backend
 *
 * @copyright Olivier Meunier & Association Dotclear
 * @copyright AGPL-3.0
 */
declare(strict_types=1);

namespace Dotclear\Process\Backend;

use Dotclear\App;
use Dotclear\Helper\Html\Form\Button;
use Dotclear\Helper\Html\Form\Form;
use Dotclear\Helper\Html\Form\Input;
use Dotclear\Helper\Html\Form\Label;
use Dotclear\Helper\Html\Form\Note;
use Dotclear\Helper\Html\Form\Para;
use Dotclear\Helper\Html\Form\Span;
use Dotclear\Helper\Html\Form\Submit;
use Dotclear\Helper\Html\Form\Textarea;
use Dotclear\Helper\Html\Form\Url;
use Dotclear\Helper\Html\Html;
use Dotclear\Helper\Process\TraitProcess;
use Exception;

/**
 * @since 2.27 Before as admin/blog.php
 */
class Blog
{
    use TraitProcess;

    public static function init(): bool
    {
        App::backend()->page()->checkSuper();

        App::backend()->blog_id   = '';
        App::backend()->blog_url  = '';
        App::backend()->blog_name = '';
        App::backend()->blog_desc = '';

        return self::status(true);
    }

    public static function process(): bool
    {
        if (!isset($_POST['id']) && (isset($_POST['create']))) {
            // Create a blog
            $cur = App::blog()->openBlogCursor();

            $blog_id = is_string($blog_id = $_POST['blog_id']) ? $blog_id : '';
            if ($blog_id === '') {
                throw new Exception('Blog ID empty!');
            }

            $blog_url                 = is_string($blog_url = $_POST['blog_url']) ? $blog_url : '';
            $blog_name                = is_string($blog_name = $_POST['blog_name']) ? $blog_name : '';
            $blog_desc                = is_string($blog_desc = $_POST['blog_desc']) ? $blog_desc : '';
            App::backend()->blog_id   = $blog_id;
            $cur->blog_id             = $blog_id;
            App::backend()->blog_url  = $blog_url;
            $cur->blog_url            = $blog_url;
            App::backend()->blog_name = $blog_name;
            $cur->blog_name           = $blog_name;
            App::backend()->blog_desc = $blog_desc;
            $cur->blog_desc           = $blog_desc;

            try {
                # --BEHAVIOR-- adminBeforeBlogCreate -- Cursor, string
                App::behavior()->callBehavior('adminBeforeBlogCreate', $cur, $blog_id);

                App::blogs()->addBlog($cur);

                # Default settings and override some
                $blog_settings = App::blogSettings()->createFromBlog($blog_id);
                $blog_settings->get('system')->put('lang', App::auth()->getInfo('user_lang'));
                $blog_settings->get('system')->put('blog_timezone', App::auth()->getInfo('user_tz'));

                if (str_ends_with($blog_url, '?')) {
                    $blog_settings->get('system')->put('url_scan', 'query_string');
                } else {
                    $blog_settings->get('system')->put('url_scan', 'path_info');
                }

                # --BEHAVIOR-- adminAfterBlogCreate -- Cursor, string, BlogSettingsInterface
                App::behavior()->callBehavior('adminAfterBlogCreate', $cur, $blog_id, $blog_settings);
                App::backend()->notices()->addSuccessNotice(sprintf(__('Blog "%s" successfully created.'), Html::escapeHTML($blog_name)));
                App::backend()->url()->redirect('admin.blog', ['id' => $blog_id]);
            } catch (Exception $e) {
                App::error()->add($e->getMessage());
            }
        }

        return true;
    }

    public static function render(): void
    {
        if (!empty($_REQUEST['id'])) {
            App::backend()->edit_blog_mode = true;
            App::task()->loadProcess((new \ReflectionClass(BlogPref::class))->getShortName());
        } else {
            $user_lang = is_string($user_lang = App::auth()->getInfo('user_lang')) ? $user_lang : 'en';

            App::backend()->page()->open(
                __('New blog'),
                App::backend()->page()->jsConfirmClose('blog-form'),
                App::backend()->page()->breadcrumb(
                    [
                        __('System')   => '',
                        __('Blogs')    => App::backend()->url()->get('admin.blogs'),
                        __('New blog') => '',
                    ]
                )
            );

            echo
            // Form
            (new Form('blog-form'))
                ->action(App::backend()->url()->get('admin.blog'))
                ->method('post')
                ->fields([
                    // Form Nonce
                    App::nonce()->formNonce(),
                    // Blog ID
                    (new Note())
                        ->class('form-note')
                        ->text(sprintf(__('Fields preceded by %s are mandatory.'), (new Span('*'))->class('required')->render())),
                    (new Para())
                        ->items([
                            (new Input('blog_id'))
                                ->size(30)
                                ->maxlength(32)
                                ->required(true)
                                ->placeholder(__('Blog ID'))
                                ->translate(false)
                                ->label(
                                    (new Label(
                                        '<span>*</span> ' . __('Blog ID:'),
                                        Label::OUTSIDE_LABEL_BEFORE
                                    ))
                                    ->class('required')
                                ),
                        ]),
                    (new Note())
                        ->class('form-note')
                        ->text(__('At least 2 characters using letters, numbers or symbols.')),
                    // Blog name
                    (new Para())
                        ->items([
                            (new Input('blog_name'))
                                ->size(30)
                                ->maxlength(255)
                                ->required(true)
                                ->placeholder(__('Blog name'))
                                ->lang($user_lang)
                                ->spellcheck(true)
                                ->label(
                                    (new Label(
                                        '<span>*</span> ' . __('Blog name:'),
                                        Label::OUTSIDE_LABEL_BEFORE
                                    ))
                                    ->class('required')
                                ),
                        ]),
                    // Blog URL
                    (new Para())
                        ->items([
                            (new Url('blog_url'))
                                ->size(30)
                                ->maxlength(255)
                                ->required(true)
                                ->placeholder(__('Blog URL'))
                                ->translate(false)
                                ->label(
                                    (new Label(
                                        '<span>*</span> ' . __('Blog URL:'),
                                        Label::OUTSIDE_LABEL_BEFORE
                                    ))
                                    ->class('required')
                                ),
                        ]),
                    // Blog description
                    (new Para())
                        ->class('area')
                        ->items([
                            (new Textarea('blog_desc'))
                                ->cols(60)
                                ->rows(5)
                                ->lang($user_lang)
                                ->spellcheck(true)
                                ->label(
                                    (new Label(
                                        __('Blog description:'),
                                        Label::OUTSIDE_LABEL_BEFORE
                                    ))
                                ),
                        ]),
                    // Buttons
                    (new Para())
                        ->class('form-buttons')
                        ->separator(' ')
                        ->items([
                            (new Submit(['create']))
                                ->accesskey('s')
                                ->value(__('Create')),
                            (new Button(['cancel']))
                                ->value(__('Back'))
                                ->class(['go-back', 'reset', 'hidden-if-no-js']),
                        ]),

                ])->render();

            App::backend()->page()->helpBlock('core_blog_new');
            App::backend()->page()->close();
        }
    }
}
