<?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\Snippets;

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\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;

/**
 * View class for a list of snippets.
 */
class HtmlView extends BaseHtmlView
{
    /**
     * An array of items
     *
     * @var  array
     */
    protected $items;

    /**
     * The pagination object
     *
     * @var  \Joomla\CMS\Pagination\Pagination
     */
    protected $pagination;

    /**
     * The model state
     *
     * @var  \Joomla\CMS\Object\CMSObject
     */
    protected $state;

    /**
     * Form object for search filters
     *
     * @var  \Joomla\CMS\Form\Form
     */
    public $filterForm;

    /**
     * The active search filters
     *
     * @var  array
     */
    public $activeFilters;

    /**
     * Display the view
     *
     * @param   string  $tpl  The name of the template file to parse
     *
     * @return  void
     */
    public function display($tpl = null)
    {
        $viewLayout = Factory::getApplication()->input->get('layout', 'default');

        // Import layout only needs the toolbar
        if ($viewLayout === 'import')
        {
            $this->addToolbar();
            parent::display($tpl);
            return;
        }

        $this->items         = $this->get('Items');
        $this->pagination    = $this->get('Pagination');
        $this->state         = $this->get('State');
        $this->filterForm    = $this->get('FilterForm');
        $this->activeFilters = $this->get('ActiveFilters');

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

        $this->addToolbar();

        // Load the clickable shortcode copy-to-clipboard helper used by the
        // Location column. The asset is declared in
        // media/com_tassoscodesnippets/joomla.asset.json, which Joomla loads
        // automatically for the active component.
        Factory::getApplication()->getDocument()->getWebAssetManager()
            ->useScript('com_tassoscodesnippets.shortcode-copy');

        parent::display($tpl);
    }

    /**
     * Add the page title and toolbar.
     *
     * @return  void
     */
    protected function addToolbar()
    {
        $viewLayout = Factory::getApplication()->input->get('layout', 'default');

        // Import layout: minimal toolbar
        if ($viewLayout === 'import')
        {
            ToolbarHelper::title(Text::_('COM_TASSOSCODESNIPPETS_IMPORT_TITLE'), 'upload');
            ToolbarHelper::back('JTOOLBAR_BACK', 'index.php?option=com_tassoscodesnippets&view=snippets');
            return;
        }

        $canDo = ContentHelper::getActions('com_tassoscodesnippets', 'category');
        $user  = Factory::getApplication()->getIdentity();

        // Get the toolbar object instance
        $toolbar = Toolbar::getInstance('toolbar');

        ToolbarHelper::title(Text::_('COM_TASSOSCODESNIPPETS_SNIPPETS_TITLE'), 'code');

        if ($canDo->get('core.create'))
        {
            $toolbar->addNew('snippet.add');
        }

        if ($canDo->get('core.edit.state') || $canDo->get('core.admin'))
        {
            $dropdown = $toolbar->dropdownButton('status-group')
                ->text('JTOOLBAR_CHANGE_STATUS')
                ->toggleSplit(false)
                ->icon('icon-ellipsis-h')
                ->buttonClass('btn btn-action')
                ->listCheck(true);

            $childBar = $dropdown->getChildToolbar();

            if ($canDo->get('core.edit.state'))
            {
                $childBar->publish('snippets.publish')->listCheck(true);
                $childBar->unpublish('snippets.unpublish')->listCheck(true);
                $childBar->archive('snippets.archive')->listCheck(true);
                $childBar->checkin('snippets.checkin')->listCheck(true);
            }

            if ($canDo->get('core.delete'))
            {
                $childBar->trash('snippets.trash')->listCheck(true);
            }

            // Export button (requires row selection)
            $childBar->standardButton('export', Text::_('COM_TASSOSCODESNIPPETS_EXPORT'), 'snippets.export')
                ->icon('icon-download')
                ->listCheck(true);

            // Duplicate button
            if ($canDo->get('core.create'))
            {
                $childBar->standardButton('duplicate', Text::_('COM_TASSOSCODESNIPPETS_DUPLICATE'), 'snippets.duplicate')
                    ->icon('icon-copy')
                    ->listCheck(true);
            }

            // Add a batch button
            if (
                $user->authorise('core.create', 'com_tassoscodesnippets')
                && $user->authorise('core.edit', 'com_tassoscodesnippets')
            )
            {
                $childBar->popupButton('batch', 'JTOOLBAR_BATCH')
                    ->popupType('inline')
                    ->textHeader(Text::_('COM_TASSOSCODESNIPPETS_BATCH_OPTIONS'))
                    ->url('#joomla-dialog-batch')
                    ->modalWidth('800px')
                    ->modalHeight('fit-content')
                    ->listCheck(true);
            }
        }

        // Import button (navigates to import layout)
        $toolbar->standardButton('import', Text::_('COM_TASSOSCODESNIPPETS_IMPORT'), 'snippets.import')
            ->icon('icon-upload')
            ->listCheck(false);

        if ($this->state->get('filter.state') == -2 && $canDo->get('core.delete'))
        {
            $toolbar->delete('snippets.delete')
                ->text('JTOOLBAR_EMPTY_TRASH')
                ->message('JGLOBAL_CONFIRM_DELETE')
                ->listCheck(true);
        }

        if ($canDo->get('core.admin') || $canDo->get('core.options'))
        {
            $toolbar->preferences('com_tassoscodesnippets');
        }

        // Logs viewer
        $toolbar->linkButton('logs', Text::_('COM_TASSOSCODESNIPPETS_LOGS_TITLE'))
            ->url('index.php?option=com_tassoscodesnippets&view=logs')
            ->icon('icon-file-alt');
    }
}
