<?php

abstract class SemiStructuredObjectInstanceController
  extends SemiStructuredBaseController {

  private $object;
  private $objectType;

  public function setObject(SemiStructuredObjectInstance $object) {
    $this->object = $object;
    $this->setObjectType($object->getClass());
    return $this;
  }

  public function getObject() {
    return $this->object;
  }

  public function setObjectType(SemiStructuredObjectType $object_type) {
    $this->objectType = $object_type;
    return $this;
  }

  public function getObjectType() {
    return $this->objectType;
  }

  protected function buildHeaderView() {
    $viewer = $this->getViewer();
    $object = $this->getObject();
    $object_type = $object->getClass();


    if ($object_type->isArchived()) {
      $status_icon = 'fa-ban';
      $status_color = 'dark';
    } else {
      $status_icon = 'fa-check';
      $status_color = 'bluegrey';
    }
    $status_name = idx(
      SemiStructuredObjectType::getStatusNameMap(),
      $object_type->getStatus());

    return id(new PHUIHeaderView())
      ->setHeader(
        pht(
          '%s: %d %s',
          $object_type->getName(),
          $object->getID(),
          $object->getName()))
      ->setUser($viewer)
      ->setPolicyObject($object_type)
      ->setStatus($status_icon, $status_color, $status_name)
      ->setHeaderIcon(SemiStructuredDataApplication::ICON_OBJECT_TYPE);
  }

  protected function buildApplicationCrumbs() {
    $object = $this->getObject();
    $object_type = $this->getObjectType();

    $can_create = true;

    $crumbs = parent::buildApplicationCrumbs();
    $crumbs->addTextCrumb(
      $object_type->getName(),
      $object_type->getURI('items/'));
    if ($object) {
      $crumbs->addTextCrumb(
        pht('%d %s', $object->getID(), $object->getName()), $object->getURI());
    }
    $crumbs->setBorder(true);

    $crumbs->addAction(
      id(new PHUIListItemView())
        ->setName(pht('Create %s', $object_type->getName()))
        ->setHref($object_type->getURI('new/'))
        ->setIcon('fa-plus-square')
        ->setWorkflow(!$can_create)
        ->setDisabled(!$can_create));

    return $crumbs;
  }


}
