<?php

namespace Hubleto\Framework\Description;


class Form implements \JsonSerializable
{

  /** @property array{ title: string, subTitle: string, showSaveButton: boolean, showCopyButton: boolean, showDeleteButton: boolean, saveButtonText: string, addButtonText: string, copyButtonText: string, deleteButtonText: string, headerClassName: string } */
  public array $ui = [
    'title' => '',
    'subTitle' => '',
    'showSaveButton' => true,
    'showCopyButton' => false,
    'showDeleteButton' => true,
    'saveButtonText' => '',
    'addButtonText' => '',
    'copyButtonText' => '',
    'deleteButtonText' => '',
    'headerClassName' => '',
    'templateJson' => '',
  ];

  /** @property array{ canCreate: bool, canRead: bool, canUpdate: bool, canDelete: bool } */
  public array $permissions = [
    'canCreate' => false,
    'canRead' => false,
    'canUpdate' => false,
    'canDelete' => false,
  ];

  /** @property array<string, \Hubleto\Framework\Description\Input> */
  public array $inputs = [];

  /** @property array<string, mixed> */
  public array $defaultValues = [];

  /** @property array<string> */
  public array $includeRelations = [];

  public function jsonSerialize(): array
  {
    $ui = [];
    if (!empty($this->ui['title'])) $ui['title'] = $this->ui['title'];
    if (!empty($this->ui['subTitle'])) $ui['subTitle'] = $this->ui['subTitle'];
    if (!empty($this->ui['saveButtonText'])) $ui['saveButtonText'] = $this->ui['saveButtonText'];
    if (!empty($this->ui['addButtonText'])) $ui['addButtonText'] = $this->ui['addButtonText'];
    if (!empty($this->ui['copyButtonText'])) $ui['copyButtonText'] = $this->ui['copyButtonText'];
    if (!empty($this->ui['deleteButtonText'])) $ui['deleteButtonText'] = $this->ui['deleteButtonText'];
    if (!empty($this->ui['headerClassName'])) $ui['headerClassName'] = $this->ui['headerClassName'];
    if (!empty($this->ui['templateJson'])) $ui['templateJson'] = $this->ui['templateJson'];
    if (!empty($this->ui['showSaveButton'])) $ui['showSaveButton'] = true;
    if (!empty($this->ui['showCopyButton'])) $ui['showCopyButton'] = true;
    if (!empty($this->ui['showDeleteButton'])) $ui['showDeleteButton'] = true;
    if (!empty($this->ui['readonly'])) $ui['readonly'] = true;

    $permissions = [];
    $permissions['canCreate'] = $this->permissions['canCreate'] ?? false;
    $permissions['canRead'] = $this->permissions['canRead'] ?? false;
    $permissions['canUpdate'] = $this->permissions['canUpdate'] ?? false;
    $permissions['canDelete'] = $this->permissions['canDelete'] ?? false;

    $inputs = $this->inputs;
    $defaultValues = $this->defaultValues;
    $includeRelations = $this->includeRelations;

    $json = [];
    if (count($ui) > 0) $json['ui'] = $ui;
    if (count($inputs) > 0) $json['inputs'] = $inputs;
    if (count($permissions) > 0) $json['permissions'] = $permissions;
    if (count($defaultValues) > 0) $json['defaultValues'] = $defaultValues;
    if (count($includeRelations) > 0) $json['includeRelations'] = $includeRelations;

    return $json;
  }

  public function toArray(): array
  {
    return $this->jsonSerialize();
  }

  /**
   * [Description for show]
   *
   * @param array $what
   * 
   * @return void
   * 
   */
  public function show(array $what): void
  {
    foreach ($what as $item) {
      $item = 'show' . strtoupper(substr($item, 0, 1)) . substr($item, 1);
      if (isset($this->ui[$item])) $this->ui[$item] = true;
    }
  }

  /**
   * [Description for hide]
   *
   * @param array $what
   * 
   * @return void
   * 
   */
  public function hide(array $what): void
  {
    foreach ($what as $item) {
      $item = 'show' . strtoupper(substr($item, 0, 1)) . substr($item, 1);
      if (isset($this->ui[$item])) $this->ui[$item] = false;
    }
  }

}