<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * 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
 */
defined('_JEXEC') or die;

use com_eorisis_pixels\framework\lib\core\cms\language as cms_language;
use com_eorisis_pixels\framework\lib\core\model\form as form_model;
use com_eorisis_pixels\framework\lib\core\model\params;
use com_eorisis_pixels\framework\lib\core\model\math;
use com_eorisis_pixels\framework\lib\core\view\help;

if (!class_exists('EorisisFormField', false))
{
	if (version_compare(JVERSION, '3.8', '>='))
	{
		JLoader::registerAlias('EorisisFormField', '\\Joomla\\CMS\\Form\\FormField');
	}
	else
	{
		JLoader::registerAlias('EorisisFormField', 'JFormField');
	}
}

class JFormFieldNumpro extends EorisisFormField
{
	protected $type = 'numpro';
	protected $form;

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

	protected function getLabel() : string
	{
		if (!params::field_value('label', $this->element))
		{
			return '';
		}

		return parent::getLabel().help::icon($this->element);
	}

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

	protected function getInput() : string
	{
		form_model::set($this->form);
		$id = (string)$this->element['name'];
		$value = form_model::get($id);
		$number_type = params::field_value('number', $this->element);

		switch ($number_type)
		{
			case 'natural':
				$default = math::natural_number($this->default);
				$value = math::natural_number($value, $default);
				break;

			case 'whole':
				$default = math::whole_number($this->default);
				$value = math::whole_number($value, $default);
				break;

			default:
				$default = (int)$this->default;
				$value = (int)$value;
		}

		$args = [];
		$args = $this->limit('min', $number_type, $args);
		$args = $this->limit('max', $number_type, $args);

		if ($this->class)
		{
			$args['class'] = $this->class;
		}

		if ($this->readonly)
		{
			$args['readonly'] = true;
		}

		$is_pro = defined('EORISIS_PIXELS_PRO');
		$html = '';

		if (!$is_pro)
		{
			$html .= '<div class="flex-row">';

			$value = $default;
			$args['disabled'] = true;
			$args['title'] = cms_language::text('EO_UPGRADE_TO_PRO_TO_USE_FEATURE');
		}

		$html .= form_model::number($id, $value, $args);

		if (!$is_pro)
		{
			$html .=	'<div class="field-notice">'.cms_language::text('EO_UPGRADE_TO_PRO').'</div>';
			$html .= '</div>';
		}

		return $html;
	}

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

	private function limit(string $attr, $number_type, array $args) : array
	{
		$val = (int)params::field_value($attr, $this->element);

		if ($val)
		{
			switch ($number_type)
			{
				case 'natural':
					$args[$attr] = math::natural_number($val);
					break;

				case 'whole':
					$args[$attr] = math::whole_number($val);
					break;

				default:
					$args[$attr] = $val;
			}
		}
		else
		{
			if (in_array($number_type, ['natural','whole']) && $attr == 'min')
			{
				switch ($number_type)
				{
					case 'natural':
						$args[$attr] = 1;
						break;

					case 'whole':
						$args[$attr] = 0;
						break;
				}
			}
		}

		return $args;
	}
}
