<?php

final class ExtensionStoreExtensionEditEngine
  extends ExtensionStoreEditEngine {

  const ENGINECONST = 'est:extension';

  private $publisher;

  public function getEngineName() {
    return pht('Extension');
  }

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

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

  public function setPublisher(ExtensionStorePublisher $publisher) {
    $this->publisher = $publisher;
    return $this;
  }

  protected function newEditableObject() {
    $viewer = $this->getViewer();

    return ExtensionStoreExtension::initializeNewExtension($viewer)
      ->attachPublisher($this->publisher);
  }

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

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

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

  protected function getObjectCreateCancelURI($object) {
    if ($this->publisher) {
      return $this->publisher->getURI();
    }
    return 'extensionstore/';
  }

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

  public function getCreateURI($form_key) {

    if ($this->publisher) {
      return $this->getApplication()->getApplicationURI(
        "type/{$this->publisher->getID()}/new/");
    }

    return parent::getCreateURI($form_key);
  }

  protected function getObjectEditTitleText($object) {
    return pht('Edit Extension: %d', $object->getID());
  }

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

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

  protected function getObjectName() {
    return pht('Extension');
  }

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

  protected function buildCustomEditFields($object) {

    $fields = array();
    $fields[] = id(new PhabricatorTextEditField())
        ->setKey('publisherPHID')
        ->setLabel(pht('Publisher'))
        ->setDescription(pht('The publisher of this extension.'))
        ->setConduitDescription(pht('Set publisher.'))
        ->setIsHidden(true)
        ->setTransactionType(
          ExtensionStoreExtensionPublisherTransaction::TRANSACTIONTYPE)
        ->setIsRequired(true)
        ->setValue($object->getPublisherPHID());

    if ($this->getIsCreate()) {
      $fields[] = id(new PhabricatorTextEditField())
        ->setKey('extensionKey')
        ->setLabel(pht('Extension Key'))
        ->setDescription(pht('Unique key to identify the extension.'))
        ->setTransactionType(
          ExtensionStoreExtensionKeyTransaction::TRANSACTIONTYPE)
        ->setIsRequired(true)
        ->setValue($object->getExtensionKey());

      $fields[] = id(new PhabricatorTextEditField())
        ->setKey('phutilname')
        ->setLabel(pht('Library Phutil Name'))
        ->setDescription(
          pht(
            'Unique name to identify the library at runtime. '.
            'Consider using the extension-key.'))
        ->setTransactionType(
          ExtensionStoreExtensionPhutilLibNameTransaction::TRANSACTIONTYPE)
        ->setIsRequired(true)
        ->setValue($object->getPhutilLibName());
    }

    $fields[] = id(new PhabricatorTextEditField())
        ->setKey('name')
        ->setLabel(pht('Name'))
        ->setDescription(pht('Name of the extension.'))
        ->setConduitDescription(pht('Rename the extension.'))
        ->setConduitTypeDescription(pht('New extension name.'))
        ->setTransactionType(
          ExtensionStoreExtensionNameTransaction::TRANSACTIONTYPE)
        ->setValue($object->getName());

    $fields[] = id(new PhabricatorRemarkupEditField())
        ->setKey('description')
        ->setLabel(pht('Free text'))
        ->setDescription(pht('Extension free text (Remarkup).'))
        ->setConduitTypeDescription(pht('New free text.'))
        ->setTransactionType(
          ExtensionStoreExtensionDescriptionTransaction::TRANSACTIONTYPE)
        ->setValue($object->getDescription());


    if ($this->publisher) {
      array_unshift(
        $fields,
        (new PhabricatorStaticEditField())
          ->setKey('publisher')
          ->setLabel(pht('Publisher'))
          ->setValue($this->publisher->getName()));
    }

    return $fields;
  }


}
