<?php

final class SemiStructuredObjectTypeEditEngine
  extends PhabricatorEditEngine {

  const ENGINECONST = 'sst:objecttype';

  public function isEngineConfigurable() {
    return false;
  }

  public function getEngineName() {
    return pht('Object Type');
  }

  public function getSummaryHeader() {
    return pht('Edit Object Type');
  }

  public function getSummaryText() {
    return pht('This engine is used to modify object types.');
  }

  public function getEngineApplicationClass() {
    return 'SemiStructuredDataApplication';
  }

  protected function newEditableObject() {
    $viewer = $this->getViewer();
    return SemiStructuredObjectType::initializeNewObjectType($viewer);
  }

  protected function newObjectQuery() {
    return new SemiStructuredObjectTypeQuery();
  }

  protected function getObjectCreateTitleText($object) {
    return pht('Create Object Type');
  }

  protected function getObjectCreateButtonText($object) {
    return pht('Create Object Type');
  }

  protected function getObjectCreateCancelURI($object) {
    return '/semistructured/';
  }

  protected function getEditorURI() {
    return $this->getApplication()->getApplicationURI('editclass/');
  }

  protected function getObjectEditTitleText($object) {
    return pht('Edit Object Type: %s', $object->getName());
  }

  protected function getObjectEditShortText($object) {
    return pht('Edit Object Type');
  }

  protected function getObjectCreateShortText() {
    return pht('Create Object Type');
  }

  protected function getObjectName() {
    return pht('Object Type');
  }

  protected function getObjectViewURI($object) {
    return $object->getURI();
  }

  protected function buildCustomEditFields($object) {
    $custom_field_help = pht(
      'See [[ %s | Custom Field Configuration ]] document '.
        'for help with **%s**.',
      'https://we.phorge.it/book/phorge/article/'.
        'custom_fields/#custom-field-configuration',
      pht('Custom Fields Definition'));

    $fields = array(
      id(new PhabricatorTextEditField())
        ->setKey('name')
        ->setLabel(pht('Name'))
        ->setDescription(pht('Name of the object type.'))
        ->setConduitDescription(pht('Rename the type.'))
        ->setConduitTypeDescription(pht('New type name.'))
        ->setTransactionType(
          SemiStructuredObjectTypeNameTransaction::TRANSACTIONTYPE)
        ->setIsRequired(true)
        ->setValue($object->getName()),
      id(new PhabricatorRemarkupEditField())
        ->setKey('description')
        ->setLabel(pht('Description'))
        ->setDescription(pht('Object Type long description.'))
        ->setConduitTypeDescription(pht('New type description.'))
        ->setTransactionType(
          SemiStructuredObjectTypeDescriptionTransaction::TRANSACTIONTYPE)
        ->setValue($object->getDescription()),

      id(new PhabricatorInstructionsEditField())
        ->setValue($custom_field_help),

      id(new PhabricatorTextAreaEditField())
        ->setKey('customfieldsdef')
        ->setLabel(pht('Custom Fields Definition'))
        ->setDescription(
          pht('Custom fields for the instances of this type (JSON).'))
        ->setConduitTypeDescription(
          pht('Custom Fields Definition (JSON, but as a String!).'))
        ->setMonospaced(true)
        ->setIsRequired(false)
        ->setTransactionType(
          SemiStructuredObjectTypeCustomFieldsTransaction::TRANSACTIONTYPE)
        ->setValue(
          id(new PhutilJSON())
            ->encodeFormatted($object->getCustomFieldsConfig())),
    );

    return $fields;
  }

}
