<?php

/**
 * @package         Tassos Code Snippets
 * @version         1.0.1-RC2 Free
 *
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            https://www.tassos.gr
 * @copyright       Copyright © 2026 Tassos Marinos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
 */

namespace Tassos\CodeSnippets\Administrator\View\Snippet;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Helper\ContentHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\GenericDataException;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Toolbar\ToolbarHelper;

/**
 * View class for editing a snippet.
 */
class HtmlView extends BaseHtmlView
{
    /**
     * The Form object
     *
     * @var  \Joomla\CMS\Form\Form
     */
    protected $form;

    /**
     * The active item
     *
     * @var  object
     */
    protected $item;

    /**
     * The model state
     *
     * @var  object
     */
    protected $state;

    /**
     * Whether to use Joomla's core UI for the sidebar
     *
     * @var  bool
     */
    public $useCoreUI = true;

    /**
     * Display the view
     *
     * @param   string  $tpl  The name of the template file to parse
     *
     * @return  void
     */
    public function display($tpl = null)
    {
        $this->form  = $this->get('Form');
        $this->item  = $this->get('Item');
        $this->state = $this->get('State');

        // Check for errors.
        if (count($errors = $this->get('Errors')))
        {
            throw new GenericDataException(implode("\n", $errors), 500);
        }

        $this->addToolbar();

        parent::display($tpl);
    }

    /**
     * Add the page title and toolbar.
     *
     * @return  void
     */
    protected function addToolbar()
    {
        Factory::getApplication()->input->set('hidemainmenu', true);

        $user       = Factory::getApplication()->getIdentity();
        $userId     = $user->get('id');
        $isNew      = ($this->item->id == 0);
        $canDo      = ContentHelper::getActions('com_tassoscodesnippets', 'category', $this->item->catid);

        ToolbarHelper::title(
            Text::_('COM_TASSOSCODESNIPPETS_SNIPPET_' . ($isNew ? 'ADD' : 'EDIT')),
            'code'
        );

        // Build the actions for new and existing records.
        if ($isNew)
        {
            if ($canDo->get('core.create'))
            {
                ToolbarHelper::apply('snippet.apply');
                ToolbarHelper::saveGroup(
                    [
                        ['save', 'snippet.save'],
                        ['save2new', 'snippet.save2new']
                    ],
                    'btn-success'
                );
            }
        }
        else
        {
            // Since it's an existing record, check the edit permission.
            $itemEditable = $canDo->get('core.edit') || ($canDo->get('core.edit.own') && $this->item->created_by == $userId);

            if ($itemEditable)
            {
                ToolbarHelper::apply('snippet.apply');
                ToolbarHelper::saveGroup(
                    [
                        ['save', 'snippet.save'],
                        ['save2new', 'snippet.save2new'],
                        ['save2copy', 'snippet.save2copy']
                    ],
                    'btn-success'
                );
            }
        }

        ToolbarHelper::cancel('snippet.cancel', $isNew ? 'JTOOLBAR_CANCEL' : 'JTOOLBAR_CLOSE');
    }
}
