<?php

namespace BitCode\BitForm\Frontend\Form\View\Conversational\Fields;

use BitCode\BitForm\Core\Util\FrontendHelpers;

class FileUploadField
{
  public static function init($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = null)
  {
    $inputWrapper = new ConversationalInputWrapper($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value);
    $input = self::field($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error, $value);
    return $inputWrapper->wrapper($input);
  }

  private static function field($field, $rowID, $field_name, $form_atomic_Cls_map, $formID, $error = null, $value = null)
  {
    $fh = new ConversationalFieldHelpers($formID, $field, $rowID, $form_atomic_Cls_map);
    $prefixIcn = $fh->icon('prefixIcn', 'pre-i');
    $suffixIcn = $fh->icon('suffixIcn', 'suf-i');
    $name = $fh->name();
    $req = $fh->required();
    $ariaDescribedBy = $fh->ariaDescribedBy();
    $ariaRequired = $fh->ariaRequired();
    $readonlyCls = isset($field->readonly) ? 'readonly' : '';
    $disabledCls = isset($field->disabled) ? 'disabled' : '';
    $btnTxt = isset($field->btnTxt) ? $field->btnTxt : '';
    $showSelectStatus = '';
    $maxSizeSection = '';
    $maxSize = isset($field->config->maxSize) ? $field->config->maxSize : '';
    $sizeUnit = isset($field->config->sizeUnit) ? $field->config->sizeUnit : '';
    $bfFrontendFormIds = FrontendHelpers::$bfFrontendFormIds;
    $contentCount = count($bfFrontendFormIds);

    if ($fh->property_exists_nested($field, 'config->showSelectStatus', true)) {
      $showSelectStatus = sprintf(
        '<div
        %1$s
        class="%2$s %3$s"
        >
           No Choosen File
        </div>
',
        $fh->getCustomAttributes('file-select-status'),
        $fh->getConversationalMultiCls('file-select-status'),
        $fh->getCustomClasses('file-select-status')
      );
    }

    if (
      $fh->property_exists_nested($field, 'config->allowMaxSize', true)
      && $fh->property_exists_nested($field, 'config->showMaxSize', true)
      && $fh->property_exists_nested($field, 'config->maxSize')
      && 0 !== $field->config->maxSize
    ) {
      $maxSizeLbl = $fh->property_exists_nested($field, 'config->maxSizeLabel', '', 1) ? $field->config->maxSizeLabel : "(Max {$maxSize} ${sizeUnit})";
      $maxSizeSection = sprintf(
        '<small
        %1$s
        class="%2$s %3$s"
        >
          %4$s
        </small>
',
        $fh->getCustomAttributes('max-size-lbl'),
        $fh->getConversationalMultiCls('max-size-lbl'),
        $fh->getCustomClasses('max-size-lbl'),
        $fh->esc_html($maxSizeLbl)
      );
    }

    return '    <div 
      ' . $fh->getCustomAttributes('file-up-container') . '
      class="' . $fh->getConversationalMultiCls('file-up-container') . ' ' . $fh->getCustomClasses('file-up-container') . '"
    >
      <div
        ' . $fh->getCustomAttributes('file-up-wrpr') . '
        class="' . $fh->getConversationalMultiCls('file-up-wrpr') . ' ' . $fh->getCustomClasses('file-up-wrpr') . ' ' . $readonlyCls . ' ' . $disabledCls . '"
      >
        <div
          ' . $fh->getCustomAttributes('file-input-wrpr') . '
          class="' . $fh->getConversationalMultiCls('file-input-wrpr') . ' ' . $fh->getCustomClasses('file-input-wrpr') . '"
        >
          <div
            ' . $fh->getCustomAttributes('btn-wrpr') . '
            class="' . $fh->getConversationalMultiCls('btn-wrpr') . ' ' . $fh->getCustomClasses('btn-wrpr') . '"
          >
            <button
              ' . $fh->getCustomAttributes('inp-btn') . '
              type="button"
              class="' . $fh->getConversationalMultiCls('inp-btn') . ' ' . $fh->getCustomClasses('inp-btn') . '"
            >
             ' . $prefixIcn . '
              <span
                ' . $fh->getCustomAttributes('btn-txt') . '
                class="' . $fh->getConversationalMultiCls('btn-txt') . ' ' . $fh->getCustomClasses('btn-txt') . '"
              >
                ' . $fh->kses_post($btnTxt) . '
              </span>
              ' . $suffixIcn . '
            </button>
            ' . $showSelectStatus . '
            ' . $maxSizeSection . '
            <input
              ' . $fh->getCustomAttributes('file-upload-input') . '
              type="file"
              class="' . $fh->getConversationalMultiCls('file-upload-input') . ' ' . $fh->getCustomClasses('file-upload-input') . '"
              id="' . $rowID . '-' . $contentCount . '"
              ' . $name . '
              ' . $req . '
              ' . $ariaRequired . '
              ' . $ariaDescribedBy . '
              ' . $fh->disabled() . '
              ' . $fh->readonly() . '
              aria-disabled="true"
              tabindex="-1"
            />
          </div>
          <div 
            ' . $fh->getCustomAttributes('err-wrp') . '
            class="err-wrp ' . $fh->getCustomClasses('err-wrp') . '"
          ></div>
        </div>
      </div>
    </div>';
  }
}
