<?php
class File {

  /**
   * Renderiza o HTML para o editor do campo de arquivo.
   *
   * @param mixed $id ID do arquivo anexado
   * @return string HTML do editor do campo de arquivo
   */
  private static function editor($id) {
    $html = sprintf('
    <div class="o2-file-field-editor">
      <button type="button" title="Remover" class="o2-file-field-remove"><span class="material-icons">delete</span></button>
      <button type="button" title="Editar" class="o2-file-field-edit"><span class="material-icons">edit</span></button>
      <span class="material-icons">description</span>
    </div>
    ');
    return $html;
  }

  /**
   * Renderiza o HTML para exibir os detalhes do arquivo anexado.
   *
   * @param mixed $id ID do arquivo anexado
   * @return string HTML dos detalhes do arquivo anexado
   */
  private static function details($id) {
    $file_data = wp_get_attachment_metadata($id);
    $html = '
      <div class="o2-file-field-datails">
        <div>
          <b class="o2-file-field-details-title">%s</b>
        </div>
        <div>
          <b>Nome do Arquivo: </b>
          <span class="o2-file-field-details-filename">
            <a href="%s" target="_blank">%s</a>
          </span>
        </div>
        <div>
          <b>Tamanho do arquivo: </b>
          <span class="o2-file-field-details-filesize">%s</span>
        </div>
        <div>
          <b>Tipo do arquivo: </b>
          <span class="o2-file-field-details-filetype">%s</span>
        </div>
      </div>
    ';

    if ($file_data) {

      $filetitle = get_the_title($id);
      $filename = basename(get_attached_file($id));
      $filesize = filesize(get_attached_file($id));
      $filetype = pathinfo($filename, PATHINFO_EXTENSION);
      $fileurl = wp_get_attachment_url($id);

      if (!empty($filesize)) {
        $size = $filesize;
        $units = array('B', 'KB', 'MB', 'GB', 'TB');
        $unit_index = 0;

        while ($size >= 1024 && $unit_index < count($units) - 1) {
          $size /= 1024;
          $unit_index++;
        }

        $size = round($size, 0) . ' ' . $units[$unit_index];
      }

      $filesize_formated = $size;

      $formated = sprintf(
        $html,
        $filetitle,
        $fileurl,
        $filename,
        $filesize_formated,
        $filetype
      );
    } else {
      $formated = sprintf($html, "", "", "", "", "");
    }
    return $formated;
  }

  /**
   * Renderiza o HTML quando nenhum arquivo está selecionado.
   *
   * @return string HTML quando nenhum arquivo está selecionado
   */
  public static function unselected() {
    $html = sprintf('
      <button type="button" title="Anexar Arquivo" class="o2-button-icon o2-button-blue o2-file-field-add">
        Anexar
        <span class="material-icons">add</span>
      </button>
      Nenhum arquivo anexado
    ');

    return $html;
  }

  /**
   * Renderiza o campo de arquivo completo.
   *
   * @param string $key Chave do campo de arquivo
   * @param mixed $value Valor do campo de arquivo (ID do arquivo anexado)
   * @return string HTML do campo de arquivo completo
   */
  public static function render($key, $value) {
    $selected = $value ? get_post($value, 'attachment') : false;

    $html = '<div class="o2-file-field">';

    $html .= sprintf('<div class="o2-file-field-unselected %s">%s</div>', ($selected ? "d-none" : ""), self::unselected());
    $html .= sprintf('<div class="o2-file-field-selected %s">%s%s</div>', ($selected ? "" : "d-none"), self::editor($value), self::details($value));
    $html .= Input::render(['name' => $key, 'value' => $value, 'type' => 'hidden']);

    $html .= '</div>';
    return $html;
  }
}
