<?php
	/**
	 * @package     Formea.Plugin
	 * @subpackage  Core.Pdf
	 *
	 * @copyright   Copyright (C) 2010-2024 Feseur Sdn Bhd. All rights reserved.
	 * @license     GNU General Public License version 2 or later; see LICENSE.txt
	 */

  namespace Joomla\Component\Formea\Plugins\Core\Pdf\Field;

  use Joomla\CMS\Factory;
  use Joomla\CMS\Form\Field\ColorField;
  use Joomla\CMS\Form\FormField;
  use Joomla\CMS\HTML\HTMLHelper;
  use Joomla\CMS\Language\Text;
  use Joomla\CMS\WebAsset\WebAssetManager;
  use Joomla\Component\Formea\Site\Helper\FormeaGeneralHelper;
  use Joomla\Database\DatabaseDriver;


  class FormeaCorePdfColorField extends FormField
  {
    /**
     * The name field type.
     *
     * @var    string
     */
    protected $type = 'FormeaCorePdfColor';

    private $languages = [];
    private $langTag = 'en_GB';
    private $app;


    public function __construct($form = null)
    {
      parent::__construct($form);
      $this->app = Factory::getApplication();
      $this->languages = FormeaGeneralHelper::getLanguages();
      $langTag = $this->app->getLanguage()->getTag();
      $this->langTag = str_replace('-', '_', $langTag);
      /** @var WebAssetManager $wa */
      $wa = $this->app->getDocument()->getWebAssetManager();
      $wa->usePreset('minicolors')
        ->useScript('field.color-adv');
    }


    /**
     * Method to get the field input markup.
     *
     * @return  string  The field input markup.
     *
     * @since   1.7.3
     */
    protected function getInput()
    {
      $validate = $this->validate;
      $format = $this->format;
      $keywords = $this->keywords;
      $disabled = $this->disabled;
      $readonly = $this->readonly;
      $class = $this->class;
      $color = '';
      if ($validate !== 'color' && in_array($format, ['rgb', 'rgba'], true)) {
        $alpha = ($format === 'rgba');
        $placeholder = $alpha ? 'rgba(0, 0, 0, 0.5)' : 'rgb(0, 0, 0)';
      } else {
        $placeholder = '#rrggbb';
      }

      $inputclass   = ($keywords && ! in_array($format, ['rgb', 'rgba'], true)) ? ' keywords' : ' ' . $format;
      $class        = ' class="form-control ' . trim('minicolors ' . $class) . ($validate === 'color' ? '' : $inputclass) . '"';
      $format       = $format ? ' data-format="' . $format . '"' : '';
      $keywords     = $keywords ? ' data-keywords="' . $keywords . '"' : '';
      $validate     = $validate ? ' data-validate="' . $validate . '"' : '';
      $disabled     = $disabled ? ' disabled' : '';
      $readonly     = $readonly ? ' readonly' : '';
      $autocomplete = !empty($autocomplete) ? ' autocomplete="' . $autocomplete . '"' : '';
      $lang = Factory::getApplication()->getLanguage();
// Force LTR input value in RTL, due to display issues with rgba/hex colors
      $direction = $lang->isRtl() ? ' dir="ltr" style="text-align:right"' : '';

      $nameBg = $this->name.'[bg]';
      $idBg = $this->id.'_bg';
      $colorBg = "";
      if(isset($this->value->bg)){
        $colorBg = $this->value->bg;
      }

      $nameText = $this->name.'[text]';
      $idText = $this->id.'_text';
      $colorText = "";
      if(isset($this->value->text)){
        $colorText = $this->value->text;
      }

      $html ='<div class="d-block mb-3"><div class="row">';
      $html .='<div class="col-md-6">';
      $html .='<div class="row align-items-center">';
      $html .='<div class="col-md-6">';
      $html .='<label for="'.$idBg.'">'.Text::_('FORMEA_PDF_BG_COLOR').'</label>';
      $html .='</div>';
      $html .='<div class="col-md-6">';
      $html .= '<input type="text" name="'.$nameBg.'" id="'.$idBg.'" value="'.$this->escape($colorBg).'"';
      if(!empty($class)){$html .= $class;}
      if(!empty($format)){$html .= $format;}
      if(!empty($keywords)){$html .= $keywords;}
      if(!empty($validate)){$html .= $validate;}
      if(!empty($disabled)){$html .= $disabled;}
      if(!empty($readonly)){$html .= $readonly;}
      if(!empty($autocomplete)){$html .= $autocomplete;}
      if(!empty($direction)){$html .= $direction;}
      $html .='/>';
      $html .='</div>';
      $html .='</div>';
      $html .='</div>';
      $html .='<div class="col-md-6">';
      $html .='<div class="row align-items-center">';
      $textStyle=' text-end';
      if($this->app->getLanguage()->isRtl()){
        $textStyle = ' text-start';
      }
      $html .='<div class="col-md-6 '.$textStyle.'">';
      $html .='<label for="'.$idText.'">'.Text::_('FORMEA_PDF_TEXT_COLOR').'</label>';
      $html .='</div>'; //md-6
      $html .='<div class="col-md-6">';
      $html .= '<input type="text" name="'.$nameText.'" id="'.$idText.'" value="'.$this->escape($colorText).'"';
      if(!empty($class)){$html .= $class;}
      if(!empty($format)){$html .= $format;}
      if(!empty($keywords)){$html .= $keywords;}
      if(!empty($validate)){$html .= $validate;}
      if(!empty($disabled)){$html .= $disabled;}
      if(!empty($readonly)){$html .= $readonly;}
      if(!empty($autocomplete)){$html .= $autocomplete;}
      if(!empty($direction)){$html .= $direction;}
      $html .='/>';
      $html .='</div>'; //md-6
      $html .='</div>'; //row


      $html .='</div>';
      $html .='</div>';
      $html .='</div>';//.d-block


      return $html;
    }

    /**
     * Method to escape output.
     *
     * @param   string  $output  The output to escape.
     *
     * @return  string  The escaped output.
     *
     * @note the ENT_COMPAT flag was replaced by ENT_QUOTES in Joomla 4.0 to also escape single quotes
     *
     * @since   3.0
     */
    public function escape($output)
    {
      return $output === null ? '' : htmlspecialchars($output, ENT_QUOTES, 'UTF-8');
    }

    /**
     * Method to get a control group with label and input.
     *
     * @param   array  $options  Options to be passed into the rendering of the field
     *
     * @return  string  A string containing the html for the control group
     *
     * @since   3.2
     */
    public function renderField($options = [])
    {
      return $this->getInput();
    }

    protected function getLabel()
    {
      return '';
    }

  }
