<?php

final class SemiStructuredObjectTypeSearchEngine
  extends PhabricatorApplicationSearchEngine {

  public function getResultTypeDescription() {
    return pht('Object Types');
  }

  public function getApplicationClassName() {
    return 'SemiStructuredDataApplication';
  }

  public function newQuery() {
    return id(new SemiStructuredObjectTypeQuery());
  }

  protected function buildCustomSearchFields() {
    return array(
      id(new PhabricatorSearchCheckboxesField())
        ->setKey('statuses')
        ->setLabel(pht('Status'))
        ->setOptions(SemiStructuredObjectType::getStatusNameMap()),
      id(new PhabricatorSearchCheckboxesField())
        ->setKey('editable')
        ->setLabel(pht('Editable'))
        ->setOptions(array('editable' => null)),
    );
  }

  protected function getURI($path) {
    return '/semistruct/'.$path;
  }

  protected function getBuiltinQueryNames() {
    $names = array();

    $names['open'] = pht('Active Types');
    $names['all'] = pht('All Types');

    return $names;
  }

  public function buildSavedQueryFromBuiltin($query_key) {
    $query = $this->newSavedQuery();
    $query->setQueryKey($query_key);
    $viewer = $this->requireViewer();

    switch ($query_key) {
      case 'all':
        return $query;
      case 'open':
        return $query->setParameter(
          'statuses',
          array(
            SemiStructuredObjectType::STATUS_ACTIVE,
          ));
    }

    return parent::buildSavedQueryFromBuiltin($query_key);
  }


  protected function buildQueryFromParameters(array $map) {
    $query = $this->newQuery();

    if ($map['statuses']) {
      $query->withStatuses($map['statuses']);
    }

    if ($map['editable'] !== null) {
      $query->withCanEdit($map['editable']);
    }

    return $query;
  }

  protected function getRequiredHandlePHIDsForResultList(
    array $objects,
    PhabricatorSavedQuery $query) {

    return array();
  }

  protected function renderResultList(
    array $items,
    PhabricatorSavedQuery $query,
    array $handles) {

    $viewer = $this->requireViewer();

    if ($items) {
      $edge_query = id(new PhabricatorEdgeQuery())
        ->withSourcePHIDs(mpull($items, 'getPHID'))
        ->withEdgeTypes(
          array(
            PhabricatorProjectObjectHasProjectEdgeType::EDGECONST,
          ));

      $edge_query->execute();
    }

    $list = id(new PHUIObjectItemListView())
      ->setViewer($viewer);

    foreach ($items as $class) {
      $item = id(new PHUIObjectItemView())
        ->setViewer($viewer)
        ->setObjectName($class->getObjectName())
        ->setHeader($class->getName())
        ->setHref($class->getURI())
        ->setObject($class);

      if ($class->isArchived()) {
        $item->setDisabled(true);
        $bg_color = 'bg-grey';
      } else {
        $bg_color = 'bg-dark';
      }

      $icon = id(new PHUIIconView())
        ->setIcon($class->getIcon());
      $item->setImageIcon($icon);

      $phid = $class->getPHID();
      $project_phids = $edge_query->getDestinationPHIDs(array($phid));
      $project_handles = $viewer->loadHandles($project_phids);

      $item->addAttribute(
        id(new PHUIHandleTagListView())
          ->setLimit(4)
          ->setNoDataString(pht('No Tags'))
          ->setSlim(true)
          ->setHandles($project_handles));

      $item->setSideColumn(
        id(new PHUIButtonView())
          ->setTag('a')
          ->setIcon('fa-users')
          ->setHref($class->getURI('items/'))
          ->setText(pht('Instances'))
          ->setColor(PHUIButtonView::GREY));

      $item->addAction((new PHUIListItemView())
          ->setName(pht('Create New Instance'))
          ->setIcon('fa-plus')
          // TODO check policy
          ->setHref($class->getURI('new/')));

      $list->addItem($item);
    }

    $result = new PhabricatorApplicationSearchResultView();
    $result->setObjectList($list);
    $result->setNoDataString(pht('No object types found.'));

    return $result;
  }


}
