<?php

namespace Blesta\Core\Util\Input\Fields;

use Blesta\Core\Util\Input\Fields\InputFields;
use Blesta\Core\Util\Input\Fields\InputField;
use Configure;
use Loader;
use View;

/**
 * Input Field Html
 *
 * Generates HTML for a given set of InputField objects, which may consist of
 * a label, tooltip, input field, or some combination thereof.
 *
 * @package blesta
 * @subpackage core.Util.Input.Fields
 * @copyright Copyright (c) 2021, Phillips Data, Inc.
 * @license http://www.blesta.com/license/ The Blesta License Agreement
 * @link http://www.blesta.com/ Blesta
 * @see InputField
 * @see InputFields
 */
#[\AllowDynamicProperties]
class Html
{
    private $container_selector = '.generated_fields_div';

    /**
     * @var InputFields The fields for which to generate HTML
     */
    private $fields;

    public function __construct(?InputFields $fields = null)
    {
        if ($fields === null) {
            $this->fields = new InputFields();
        } else {
            $this->setFields($fields);
        }

        Loader::loadHelpers($this, ['Form', 'Html']);

        // Initialize the main view
        $this->view = new View('partial_fields');

        // Fall back to the stock portal template when the active template lacks
        // partial_fields.pdt; the View only checks default_view as a fallback.
        $stock_view = $this->getStockPortalView();
        if ($stock_view !== null) {
            $this->view->default_view = $stock_view;
        }

        Loader::loadHelpers($this->view, ['Form', 'Html']);
    }

    /**
     * Returns the stock portal view directory (e.g. "client/bootstrap") for the
     * portal currently being rendered, or null if it cannot be determined.
     *
     * @return string|null
     */
    private function getStockPortalView()
    {
        $current_view = (string) Configure::get('System.default_view');
        if ($current_view === '' || strpos($current_view, DS) === false) {
            return null;
        }

        $portal = strstr($current_view, DS, true);
        $stock_template = Configure::get('Blesta.default_' . $portal . '_view_template');
        if (!$stock_template) {
            return null;
        }

        return $portal . DS . $stock_template;
    }

    /**
     * Sets the InputFields that should be used for generating HTML
     *
     * @param InputFields $fields The fields for which to generate HTML
     */
    public function setFields(InputFields $fields)
    {
        $this->fields = $fields;
    }

    /**
     * Gets the HTML for displaying the currently attached configurable options
     *
     * @param string $file The file to use for displaying configurable options
     * @param string $view_dir The directory in which to look for the configurable option display file
     * @return string The configurable option HTML
     */
    public function generate($file = null, $view_dir = null)
    {
        $this->view->set('fields', $this->fields->getFields());
        $this->view->set('html', $this->fields->getHtml());
        if ($file !== null) {
            $this->view->setView($file, null);
        }

        if ($view_dir !== null) {
            $this->view->setView(null, $view_dir);
        }

        return $this->view->fetch();
    }

    /**
     * Gets the selector for the element containing the configurable options
     *
     * @return string The element selector
     */
    public function getContainerSelector()
    {
        return $this->container_selector;
    }

    /**
     * Sets the selector for the element containing the configurable options
     *
     * @param string $container_selector The element selector
     */
    public function setContainerSelector($container_selector)
    {
        $this->container_selector = $container_selector;
    }
}
