<?php
  /**
   * @version     1.0.0
   * @package     Joomla 4
   * @subpackage  Formea
   * @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 Joomla\CMS\Factory;
  use Joomla\CMS\Filesystem\File;
  use Joomla\CMS\WebAsset\WebAssetManager;
  use Joomla\Component\Formea\Site\Helper\FormeaGeneralHelper;
  use Joomla\Component\Formea\Site\Libraries\FormeaCore;

  class FormeaMultipage extends FormeaCore
  {
    private $multiPagePath;
    private $core;

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

    public function drawPageElementAttribute($formId, $currentIdx, $totalPages)
    {
      $pageAttributes                    = [];
      $pageClass                         = ['content', 'multipage_' . $this->formUniqueString . '_' . $currentIdx];
      $pageAttributes['role']            = 'tabpanel';
      $pageAttributes['aria-labelledby'] = 'fm_pg_' . $this->formUniqueString . '_' . $currentIdx . '_trigger';

      return [
        'pageAttributes' => $pageAttributes,
        'pageClass'      => $pageClass,
      ];
    }

    protected function getCore()
    {
      if (empty($this->core))
      {
        $core = FormeaGeneralHelper::getCoreExtension($this->id);
        if (!empty($core))
        {
          $params = $core->params;
          if ($this->styleId > 0)
          {
            $style = FormeaGeneralHelper::getExtensionStyle($this->styleId);
            if (!empty($style))
            {
              $params = $style->params;
            }
          }
          $core->params = new Joomla\Registry\Registry($params);
        }
        $this->core = $core;
      }

      return $this->core;
    }

    public function getGeneratedScript()
    {

      $script     = '';
      $scriptFile = $this->multiPagePath . '/tmpl/script.js';
      if (File::exists($scriptFile))
      {

        $core = $this->getCore();
        if (!empty($core))
        {
          $params = $core->params;
        }
        else
        {
          $params = new Joomla\Registry\Registry([]);
        }
        $builtInNav = 'true';
        if (!filter_var($params->get('nav_auto', 1), FILTER_VALIDATE_BOOLEAN))
        {
          $builtInNav = 'false';
        }

        $allowSubmission = 0;
        if (!is_null($this->formObject))
        {
          $allowSubmission = (int) $this->formObject->allow_submission;
        }
        $replaceAbles = [
          'builtInNav:true'    => 'builtInNav: ' . $builtInNav,
          'totalPage:0,'       => 'totalPage: ' . $this->totalPages,
          'totalPage: 0,'      => 'totalPage: ' . $this->totalPages . ',',
          '[UNIQUE_STRING]'    => $this->formUniqueString,
          '\'[ALLOW_SUBMISSION]\'' => $allowSubmission,
          'var MultipageFunc'  => 'var Multipage' . $this->formUniqueString . 'Func',
        ];
        $_script      = file_get_contents($scriptFile);
        foreach ($replaceAbles as $key => $value)
        {
          $_script = str_replace($key, $value, $_script);
        }
        if (!empty($_script))
        {
          $script = $_script;
          $script .= 'document.addEventListener("DOMContentLoaded",function(){';
          $script .= 'Multipage' . $this->formUniqueString . 'Func.init();';
          $script .= '});';
        }
      }

      return $script;
    }

    public function getLoadableScript()
    {
      return [
        'com_formea.core.Multipage.bs-stepper'
      ];
    }

    public function getLoadableStyle()
    {
      return [
        'com_formea.core.Multipage.bs-stepper'
      ];
    }

  }
