<?php

abstract class SemiStructuredObjectTypeController
  extends SemiStructuredBaseController {

  private $objectType;

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

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

  public function buildApplicationMenu() {
    return $this->buildSideNavView()->getMenu();
  }

  protected function buildHeaderView() {
    $viewer = $this->getViewer();
    $object_type = $this->getObjectType();


    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($object_type->getName())
      ->setUser($viewer)
      ->setPolicyObject($object_type)
      ->setStatus($status_icon, $status_color, $status_name)
      ->setHeaderIcon(SemiStructuredDataApplication::ICON_OBJECT_TYPE);
  }

  protected function buildApplicationCrumbs() {
    $object_type = $this->getObjectType();
    $uri = $object_type->getURI();

    $crumbs = parent::buildApplicationCrumbs();
    $crumbs->addTextCrumb($object_type->getName(), $uri);
    $crumbs->setBorder(true);
    return $crumbs;
  }

  protected function buildSideNavView($filter = null) {
    $viewer = $this->getViewer();
    $object_type = $this->getObjectType();
    $id = $object_type->getID();

    $can_edit = PhabricatorPolicyFilter::hasCapability(
      $viewer,
      $object_type,
      PhabricatorPolicyCapability::CAN_EDIT);

    $nav = id(new AphrontSideNavFilterView())
      ->setBaseURI(new PhutilURI($this->getApplicationURI()));

    $nav->addLabel(pht('Object Type'));

    $nav->addFilter(
      'view',
      pht('View Object Type'),
      $object_type->getURI(),
      SemiStructuredDataApplication::ICON_OBJECT_TYPE);

    $nav->addFilter(
      'instances',
      pht('View Instances'),
      $this->getApplicationURI("/type/{$id}/items/"),
      'fa-group');

    $nav->selectFilter($filter);

    return $nav;
  }

}
