<?php

final class SemiStructuredObjectType extends SemiStructuredDAO
  implements
    PhabricatorPolicyInterface,
    PhabricatorApplicationTransactionInterface,
    // PhabricatorMarkupInterface, ?
    PhabricatorMentionableInterface,
    PhabricatorSubscribableInterface,
    PhabricatorProjectInterface,
    PhabricatorConduitResultInterface,
    PhabricatorFlaggableInterface,
    PhabricatorDestructibleInterface,
    PhabricatorFerretInterface,
    PhabricatorFulltextInterface {


  protected $viewPolicy;
  protected $editPolicy;

  protected $name;
  protected $status;
  protected $description;

  protected $customFieldsConfig = array();

  const STATUS_ACTIVE = 'active';
  const STATUS_ARCHIVED = 'archived';


  public static function initializeNewObjectType(PhabricatorUser $actor) {
    return id(new self())
      ->setName('')
      ->setViewPolicy(PhabricatorPolicies::getMostOpenPolicy())
      ->setEditPolicy($actor->getPHID())
      ->setStatus(self::STATUS_ACTIVE);
  }


  public function getCustomFieldsConfig() {
    if (!$this->customFieldsConfig) {
      return array();
    }
    return $this->customFieldsConfig;
  }

  public static function getStatusNameMap() {
    return array(
      self::STATUS_ACTIVE => pht('Active'),
      self::STATUS_ARCHIVED => pht('Archived'),
    );
  }

  protected function getConfiguration() {
    return array(
      self::CONFIG_AUX_PHID => true,
      self::CONFIG_SERIALIZATION => array(
        'customFieldsConfig' => self::SERIALIZATION_JSON,
      ),
      self::CONFIG_COLUMN_SCHEMA => array(
        'name' => 'sort255',
        'status' => 'text32',
        'description' => 'text',
      ),
    ) + parent::getConfiguration();
  }

  public function getPHIDType() {
    return SemiStructuredObjectTypePHIDType::TYPECONST;
  }

  public function isArchived() {
    return ($this->getStatus() == self::STATUS_ARCHIVED);
  }

  public function getURI($path = '') {
    return urisprintf('/semistruct/type/%d/%s', $this->getID(), $path);
  }

  public function getObjectName() {
    return pht('Object Type %d', $this->getID());
  }

  public function getIcon() {
    // TODO copy the feature from Projects, where you can put a custom image
    // or select from a list.
    return SemiStructuredDataApplication::ICON_OBJECT_TYPE;
  }


/* -(  PhabricatorApplicationTransactionInterface  )------------------------- */


  public function getApplicationTransactionEditor() {
    return new SemiStructuredObjectTypeTransactionEditor();
  }

  public function getApplicationTransactionTemplate() {
    return new SemiStructuredObjectTypeTransaction();
  }


/* -(  PhabricatorPolicyInterface  )----------------------------------------- */


  public function getCapabilities() {
    return array(
      PhabricatorPolicyCapability::CAN_VIEW,
      PhabricatorPolicyCapability::CAN_EDIT,
      // TODO can-create-instances
    );
  }

  public function getPolicy($capability) {
    switch ($capability) {
      case PhabricatorPolicyCapability::CAN_VIEW:
        return $this->getViewPolicy();
      case PhabricatorPolicyCapability::CAN_EDIT:
        return $this->getEditPolicy();
    }
  }

  public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
    return false;
  }


/* -(  PhabricatorConduitResultInterface  )---------------------------------- */


  public function getFieldSpecificationsForConduit() {
    return array(
      (new PhabricatorConduitSearchFieldSpecification())
        ->setKey('name')
        ->setType('string')
        ->setDescription(pht('The name of the object type.')),
      (new PhabricatorConduitSearchFieldSpecification())
        ->setKey('description')
        ->setType('remarkup')
        ->setDescription(pht('The description.')),
      (new PhabricatorConduitSearchFieldSpecification())
        ->setKey('status')
        ->setType('string')
        ->setDescription(pht('Status of this object data.')),
      (new PhabricatorConduitSearchFieldSpecification())
        ->setKey('customFieldsConfig')
        ->setType('map<string, wild>')
        ->setDescription(pht('Custom Fields definition')),
    );
  }

  public function getFieldValuesForConduit() {
    return array(
      'name' => $this->getName(),
      'description' => array(
        'raw' => $this->getDescription(),
      ),
      'status' => $this->getStatus(),
      'customFieldsConfig' => $this->getCustomFieldsConfig(),
    );
  }

  public function getConduitSearchAttachments() {
    return array();
  }


/* -(  PhabricatorFerretInterface  )----------------------------------------- */


public function newFerretEngine() {
  return new SemiStructuredObjectTypeFerretEngine();
}

/* -(  PhabricatorFulltextInterface  )--------------------------------------- */


public function newFulltextEngine() {
  return new SemiStructuredObjectTypeFulltextEngine();
}


/* -(  PhabricatorDestructibleInterface  )----------------------------------- */


  public function destroyObjectPermanently(
    PhabricatorDestructionEngine $engine) {

    $this->openTransaction();
    // TODO destroy all instances....
      $this->delete();
    $this->saveTransaction();
  }


/* -(  PhabricatorSubscribableInterface  )----------------------------------- */

  public function isAutomaticallySubscribed($phid) {
    return false;
  }

}
