<?php
	/**
	 * @version     1.0.2
	 * @package     Joomla 4
	 * @subpackage  Formea / Repeatables
	 * @author      Feseur Sdn Bhd
	 * @copyright   Copyright (C) 2010 - 2020 Feseur Sdn Bhd
	 * @license     GNU/GPL, see LICENSE.php
	 */


  defined('_JEXEC') or die();

  // no direct access
  use Feseur\Library\FsrResponse;
  use Joomla\CMS\Factory;
  use Joomla\CMS\Filesystem\File;
  use Joomla\CMS\Filesystem\Folder;
  use Joomla\CMS\Language\Text;
  use Joomla\CMS\WebAsset\WebAssetManager;
  use Joomla\Component\Formea\Administrator\Helper\FormeaHelper;
  use Joomla\Component\Formea\Site\Helper\FormeaGeneralHelper;
  use Joomla\Component\Formea\Site\Libraries\FormeaCore;
  use Joomla\Component\Formea\Site\Libraries\FormeaElement;
  use Joomla\Component\Formea\Site\Libraries\FormeaForm;
  use Joomla\Component\Formea\Site\Libraries\FormeaGridItem;
  use Joomla\Database\DatabaseDriver;

  class FormeaRepeatables extends FormeaCore
  {
    private $repeatablePath;
    private $core;


    public function __construct($config)
    {
      parent::__construct($config);
      $this->repeatablePath = FORMEA_CORE_PATH . '/Repeatables/';
      $app                  = Factory::getApplication();
      /** @var WebAssetManager $wa */
      $wa       = $app->getDocument()->getWebAssetManager();
      $registry = $wa->getRegistry();
      $registry->addRegistryFile('components/com_formea/plugins/core/Repeatables/joomla.asset.json');
    }

    /**
     * JS file list to load
     *
     * @since 1.0.0
     */
    public function getLoadableScript()
    {
      return ['site.formea-repeatables.js'];
    }

    public function getLoadableStyle()
    {
      return ['com_formea.formea-repeatables'];
    }

    public function refactorGroupBlock($htmlStructure, $groupedElement)
    {
      $enabled = (int) $this->formParams->get('enabled', 0);
      if ($enabled > 0)
      {
        $app     = Factory::getApplication();
        $lang    = $app->getLanguage();
        $langTag = $lang->getTag();

        if (Folder::exists(FORMEA_CORE_PATH . '/Repeatables/language/' . $langTag))
        {
          $langStringIni = parse_ini_file(FORMEA_CORE_PATH . '/Repeatables/language/' . $langTag . '/Repeatables.ini');
          $lang->load('Repeatables', FORMEA_CORE_PATH . '/Repeatables', $langTag);
        }
        else
        {
          $langStringIni = parse_ini_file(FORMEA_CORE_PATH . '/Repeatables/language/en-GB/Repeatables.ini');
          $lang->load('Repeatables', FORMEA_CORE_PATH . '/Repeatables', 'en-GB');
        }

        $addNewText  = Text::_($this->formParams->get('add_new', 'REPEATABLES_ADD_NEW'));
        $removeText  = Text::_($this->formParams->get('add_new', 'REPEATABLES_REMOVE'));
        $addClass    = $this->formParams->get('add_class', 'btn btn-sm btn-primary');
        $removeClass = $this->formParams->get('remove_class', 'btn btn-sm btn-danger');

        $setIndex = $groupedElement->setIndex;

        $repeatableInitConfig = [
          'formId'       => $this->formId,
          'group_id'     => $this->group_id,
          'uniqueString' => $this->formUniqueString,
          'cid'          => $this->id,
          'newText'      => $addNewText,
          'removeText'   => $removeText,
          'addClass'     => explode(' ',$addClass),
          'removeClass'  => explode(' ',$removeClass)
        ];

        $body                  = '<div class="repeatable_element" id="repeatable_' . $this->formId . '_' . $this->formUniqueString . '_' . $this->id . '_' . $setIndex . '"';
        $body                  .= '  data-formea-repeatable-init="' . htmlspecialchars(json_encode($repeatableInitConfig)) . '"';
        $body                  .= ' data-formea-repeatable-index="' . $setIndex . '"';
        $body                  .= '>';
        $body                  .= $htmlStructure['body'];
        $body                  .= '<div class="formea-repeatable-action"><div class="addNew"><button type="button" class="' . $addClass . '"';
        $body                  .= ' data-formea-repeatable-action="add" onclick="FormeaRepeatable.action(' . $this->formId . ',' . $this->group_id . ',\'' . $this->formUniqueString . '\',this)">';
        $body                  .= '  <span class="spinner-border me-1 spinner-border-sm" hidden="true"></span>';
        $body                  .= '<span role="status">' . $addNewText . '</span></button></div><div style="display:none;" class="removeCurrent"></div></div>';
        $body                  .= '</div>';
        $htmlStructure['body'] = $body;
      }

      return $htmlStructure;
    }


  }
