<?php

final class ExtensionStoreReleaseEditEngine
  extends ExtensionStoreEditEngine {

  const ENGINECONST = 'est:release';

  private $extension;

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

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

  public function getSummaryText() {
    return pht('This engine is used to edit Extensions releases.');
  }

  public function setExtension(ExtensionStoreExtension $extension) {
    $this->extension = $extension;
    return $this;
  }

  protected function newEditableObject() {
    $viewer = $this->getViewer();
    return ExtensionStoreRelease::initializeNewRelease($viewer)
      ->attachExtension($this->extension);
  }

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

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

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

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

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

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

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

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

  protected function getObjectCreateCancelURI($object) {
    if ($this->extension) {
      return $this->extension->getURI();
    }
    return $this->getApplication()->getApplicationURI();
  }

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

  protected function buildCustomEditFields($object) {
    $fields = array();

    if ($this->getIsCreate()) {
      $fields[] = id(new PhabricatorTextEditField())
        ->setKey('extension')
        ->setLabel(pht('Extension'))
        ->setDescription(pht('Extension this release is for.'))
        ->setTransactionType(
          ExtensionStoreReleaseExtensionTransaction::TRANSACTIONTYPE)
        ->setIsRequired(true)
        ->setIsHidden(true)
        ->setValue($object->getExtensionPHID());

      $fields[] = id(new PhabricatorTextEditField())
        ->setKey('name')
        ->setLabel(pht('Name'))
        ->setDescription(pht('Name of the version.'))
        ->setTransactionType(
          ExtensionStoreReleaseNameTransaction::TRANSACTIONTYPE)
        ->setIsRequired(true)
        ->setValue($object->getName());

      $fields[] = id(new PhabricatorTextEditField())
          // TODO this is bad, there's something better in Pholio
        ->setKey('filePHID')
        ->setLabel(pht('File'))
        ->setDescription(pht('File to serve as this release.'))
        ->setTransactionType(
          ExtensionStoreReleaseFileTransaction::TRANSACTIONTYPE)
        ->setIsRequired(true)
        ->setValue($object->getDownloadFilePHID());


        // filephid
    }

    // TODO else {}

    return $fields;
  }

}
