<?php
/*
 * Software Name: Eorisis Setup
 * Software URI: https://eorisis.com/shop/software/joomla/setup
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
namespace eorisis_setup\framework\lib\core\model;
\defined('_JEXEC') or die;

use eorisis_setup\framework\lib\core\app\config as app_config;
use eorisis_setup\framework\lib\core\cms\language as cms_language;
use eorisis_setup\framework\lib\core\cms\toolbar as cms_toolbar;

class toolbar
{
	public function __construct() {}

	// --------------------------------------------------

	public function add_buttons() // void
	{
		$buttons = [
			'documentation' => 'EO_DOCUMENTATION',
			'changelog' => 'EO_CHANGELOG'
		];

		if (\version_compare(JVERSION, 4, '<'))
		{
			$buttons = \array_reverse($buttons);
		}

		foreach ($buttons as $name => $language_key)
		{
			$this->add_modal_button($name, $language_key);
		}
	}

	// --------------------------------------------------

	public function add_modal_button(string $name, string $txt_key)
	{
		app_config::init();
		$uri = 'https://eorisis.com'.app_config::get('app_uri').'/'.$name;

		// Joomla 4
		if (\version_compare(JVERSION, 4, '>='))
		{
			$title = cms_language::text('EO_EXT_TITLE').' - '.cms_language::text($txt_key);
			$this->add_modal_native_button($txt_key, $title, $uri);
			return;
		}

		// Joomla <= 3

		$icon = $name == 'documentation' ? 'help' : 'info';
		cms_toolbar::add_button($icon, $txt_key, $uri);
	}

	// --------------------------------------------------

	// Joomla 4
	private function add_modal_native_button(string $button_text_constant, string $title, string $uri, bool $show_footer = false)
	{
		static $count = 1;

		$modal = cms_toolbar::instance()->popupButton('Modal'.$count);
		$modal->text($button_text_constant);
		$modal->url($uri);
		$modal->icon('icon-info');
		$modal->modalWidth(80);
		$modal->bodyHeight(80);
		$modal->buttonClass('btn btn-secondary');

		if ($title)
		{
			$modal->title($title);
		}

		if ($show_footer)
		{
			$close_button  = '<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">';
			$close_button .=	cms_language::text('JTOOLBAR_CLOSE');
			$close_button .= '</button>';
			$modal->footer($close_button);
		}

		$count++;
	}
}
