<?php
/**
 * Created by PhpStorm.
 * User: jirislav
 * Date: 19.5.17
 * Time: 23:57
 */

namespace App\Http\ViewHelpers;

use App\EntityParameter;
use Form;
use Input;
use Lang;


/**
 * Class HtmlAggregator
 * @package Http\ViewHelpers
 */
class HtmlAggregator
{

    /**
     * @param $entityClass
     * @param $labelWidth
     * @param $inputWidth
     * @param null $entity
     * @return string
     */
    public static function entityParametersToHtml($entityClass, $labelWidth, $inputWidth, $entity = null)
    {
        $finalHtml = '';

        foreach ($entityClass->parameters as $parameter) {
            $paramName = $parameter['name'];
            $paramFormName = 'param_' . $paramName;
            $currentParamHtml = '<div class="form-group">';

            $currentParamHtml .= Form::label($paramFormName, Lang::getFromJson($parameter['name']), ['class' => "col-md-$labelWidth control-label"]);
            $currentParamHtml .= "<div class='col-md-$inputWidth'>";

            switch ($parameter['type']) {
                case EntityParameter::STRING:
                    if ($parameter['max'] === null || $parameter['max'] > EntityParameter::STRING_DB_LEN)
                        $parameter['max'] = EntityParameter::STRING_DB_LEN;

                    $inputParams = [
                        'class' => 'form-control',
                        'autofocus',
                        'maxlength' => $parameter['max']
                    ];

                    if ($parameter['required'])
                        $inputParams['required'] = 'required';

                    if ($entity === null)
                        $value = Input::old($paramFormName);
                    else
                        $value = $entity->$paramName;

                    $currentParamHtml .= Form::text($paramFormName, $value, $inputParams);
                    break;

                /** @noinspection PhpMissingBreakStatementInspection */
                case EntityParameter::TINY_INT:
                    if ($parameter['max'] === null || $parameter['max'] > 127)
                        $parameter['max'] = 127;

                    if ($parameter['max'] != 127)
                        $min = 0;
                    else
                        $min = -128;

                case EntityParameter::INT:
                    if ($parameter['max'] === null || $parameter['max'] > 2147483647)
                        $parameter['max'] = 2147483647;

                    if (!isset($min))
                        $min = -2147483648;

                    $inputParams = [
                        'class' => 'form-control',
                        'autofocus',
                        'min' => $min,
                        'max' => $parameter['max'],
                        'placeholder' => ($parameter['max'] === 2147483647 && $min === -2147483648) ?
                            Lang::getFromJson('Insert a number here') :
                            "< $min, ${parameter['max']} >"
                    ];

                    unset($min);

                    if ($parameter['required'])
                        $inputParams['required'] = 'required';

                    if ($entity === null)
                        $value = Input::old($paramFormName);
                    else
                        $value = $entity->$paramName;

                    $currentParamHtml .= Form::number($paramFormName, $value, $inputParams);
                    break;
                case EntityParameter::DATETIME:

                    if ($entity === null)
                        $value = Input::old($paramFormName);
                    else
                        $value = $entity->$paramName;

                    $currentParamHtml .= "<datepicker placeholder=\"" .
                        Lang::getFromJson('Click to pick a date') .
                        "\" format='yyyy-MM-dd' language='" . Lang::getLocale() .
                        "' val='" . $value .
                        ($parameter['required'] ? "' v-bind:required='required" : '') .
                        "' name='$paramFormName'></datepicker>";
                    break;
                case EntityParameter::BLOB:

                    $inputParams = [
                        //'class' => 'form-control',
                        //'autofocus',
                    ];

                    if ($parameter['required'])
                        $inputParams['required'] = 'required';

                    $currentParamHtml .= Form::file($paramFormName, $inputParams);
                    break;
            }

            $currentParamHtml .= '</div></div>';
            $finalHtml .= $currentParamHtml;
        }

        return $finalHtml;
    }
}