<?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\cms;
\defined('_JEXEC') or die;

use eorisis_setup\framework\lib\core\cms\language as cms_language;
use eorisis_setup\framework\lib\core\model\form as form_model;
use eorisis_setup\framework\lib\core\view\form as form_view;

if (\version_compare(JVERSION, 4, '>='))
{
	form_model::set_backend_cols();
}

class form
{
	public function __construct() {}

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

	public static function switcher(string $id, array $options, $default, array $args = []) : string
	{
		$args = \array_replace([
			'field_name' => 'params',
			'txt_constant' => true,
			'disabled' => false,
			'title' => '',
			'is_binary' => false
		], $args);

		$selected = form_model::param($id, $default);

		if ($args['is_binary'])
		{
			$selected = (int)$selected;
			$default = (int)$default;
		}

		$selected = \array_key_exists($selected, $options) ? $selected : $default;
		$count = 0;

		if (\version_compare(JVERSION, 4, '>='))
		{
			\eorisis_setup\framework\lib\core\cms\head::add_switcher_media();

			// J4 bug needs the ON value to be last
			if (isset($options[0]) and
				isset($options[1]))
			{
				// ksort — Sort an array by key
				// from MIN value to MAX value
				\ksort($options);
			}

			$html  = '<fieldset>';
			$html .=	'<div class="switcher">';

			foreach ($options as $value => $language_key)
			{
				$checked = $selected === $value;
				$name = 'jform['.$args['field_name'].']['.$id.']';
				$arguments = [
					'id' => 'jform_'.$args['field_name'].'_'.$id.$count,
					'value' => $value,
					'title' => $args['title'],
					'checked' => $checked,
					'class' => $checked ? 'active' : '',
					'disabled' => $args['disabled']
				];

				$html .= form_view::radio($name, $arguments);
				$html .= '<label for="jform_'.$args['field_name'].'_'.$id.$count.'">';
				$html .=	cms_language::text($count ? 'JON' : 'JOFF');
				$html .= '</label>';

				$count++;
			}

			$html .=		'<span class="toggle-outside">';
			$html .=			'<span class="toggle-inside"></span>';
			$html .=		'</span>';
			$html .=	'</div>';
			$html .= '</fieldset>';
		}
		else
		{
			$jversion_3_8_or_older = \version_compare(JVERSION, '3.8', '<');
			
			$html  = '<fieldset ';
			$html .=	'id="jform_'.$args['field_name'].'_'.$id.'" ';
			$html .=	'class="btn-group'.form_view::btn_size_class().' no-animation radio">';

			foreach ($options as $value => $language_key)
			{
				$name = 'jform['.$args['field_name'].']['.$id.']';
				$arguments = [
					'id' => 'jform_'.$args['field_name'].'_'.$id.$count,
					'value' => $value,
					'checked' => $selected === $value,
					'disabled' => $args['disabled']
				];

				$html .= form_view::radio($name, $arguments);
				$html .= '<label ';
				$html .=	'for="jform_'.$args['field_name'].'_'.$id.$count.'" ';
				$html .=	$args['title'] ? ' title="'.$args['title'].'"' : '';
				if ($jversion_3_8_or_older and $arguments['checked'])
				{
					$html .= ' class="btn active btn-'.($value ? 'success' : 'danger').'"';
				}
				$html .=	$args['disabled'] ? ' disabled' : '';
				$html .=	'>';
				$html .=	$args['txt_constant'] ? cms_language::text($language_key) : $language_key;
				$html .= '</label>';

				$count++;
			}

			$html .= '</fieldset>';
		}

		return $html;
	}
}
