<?php

/**
 * @version 1.6.0
 * @package Raffle
 * @copyright (C) 2011 migusbox.com
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPLv2
 *
 * editor-xtd-button for raffle
 *
 */
defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Object\CMSObject;
use Joomla\CMS\Uri\Uri;
use BlackSheepResearch\Component\UserPoints\Site\UserPoints;
use BlackSheepResearch\Component\UserPoints\Site\WebAssetManager;

class plgButtonRaffle extends CMSPlugin {

    public function __construct(& $subject, $config) {
        parent::__construct($subject, $config);
        $this->loadLanguage();
    }

    function onDisplay($name) {
        $lang = Factory::getLanguage();
        $lang->load('plg_editors-xtd_raffle', JPATH_ADMINISTRATOR);
        $wa = WebAssetManager::getInstance('com_userpoints');
        $wa->useScript('core');
        $wa->useScript('modal-content');

        $link = 'index.php?option=com_userpoints&task=editorInsertRaffle&amp;tmpl=component&amp;e_name=' . $name;

        $button = new CMSObject();
        $button->set('modal', true);
        $button->set('link', $link);
        $button->set('text', Text::_('PLG_RAFFLE_BUTTON_RAFFLE'));
        $button->set('name', 'ticket');

        // SVG icon option
        $button->set('iconSVG', '<svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M18 10v1h4v2h-4v1h4v7h-7v-4h-1v4h-1v-4h-1v4H5v-7h4v-1H5v-2h4v-1H5V3h7v4h1V3h1v4h1V3h7v7h-4zm-8 2v1h1v-1h-1zm2 0v1h1v-1h-1zm2 0v1h1v-1h-1zm-4 2v1h1v-1h-1zm2 0v1h1v-1h-1zm2 0v1h1v-1h-1zm-4 2v1h1v-1h-1zm2 0v1h1v-1h-1zm2 0v1h1v-1h-1z" fill="currentColor"/></svg>');

        // Set the appropriate modal options
        if (version_compare(JVERSION, '4.0', '<')) {
            $button->set('options', "{handler: 'iframe', size: {x: 500, y: 580}}");
        } else {
            $button->set('options', [
                'height' => '580',
                'width' => '500',
                'bodyHeight' => '70',
                'modalWidth' => '80',
            ]);
        }

        return $button;
    }
}
