<?php

trait ExtensionStorePublisherControllerTrait {

  private $publisher;

  public function setPublisher(ExtensionStorePublisher $publisher) {
    $this->publisher = $publisher;
    return $this;
  }

  public function getPublisher() {
    return $this->publisher;
  }

  protected function findPublisher(AphrontRequest $request) {
    $viewer = $request->getViewer();
    $id = $request->getURIData('id');
    $key = $request->getURIData('publisherKey');

    $query = new ExtensionStorePublisherQuery()
      ->setViewer($viewer);

    if ($id !== null) {
      $query->withIDs(array($id));
    } else if ($key !== null) {
      $query->withKeys(array($key));
    } else {
      return null;
    }

    $publisher = $query->executeOne();
    if ($publisher) {
      $this->setPublisher($publisher);
    }
    return $publisher;
  }

  protected function buildHeaderView() {
    $viewer = $this->getViewer();
    $publisher = $this->getPublisher();


    if ($publisher->isArchived()) {
      $status_icon = 'fa-ban';
      $status_color = 'dark';
    } else {
      $status_icon = 'fa-check';
      $status_color = 'bluegrey';
    }
    $status_name =
      ExtensionStorePublisherStatus::getStatusName($publisher->getStatus());

    return id(new PHUIHeaderView())
      ->setHeader($publisher->getName())
      ->setViewer($viewer)
      ->setPolicyObject($publisher)
      ->setStatus($status_icon, $status_color, $status_name)
      ->setHeaderIcon('fa-university');
  }


  protected function buildDetailsView() {
    $viewer = $this->getViewer();
    $publisher = $this->getPublisher();

    $view = id(new PHUIPropertyListView())
      ->setViewer($viewer);

    $view->addProperty(pht('Publisher Key'), $publisher->getPublisherKey());

    $description = $publisher->getDescription();
    if (strlen($description)) {
      $view->addTextContent(
        new PHUIRemarkupView($viewer, $description));
    }

    return $view;
  }


  protected function addApplicationCrumbsSection(PHUICrumbsView $crumbs) {
    $publisher = $this->getPublisher();
    $uri = $publisher->getURI();

    $crumbs->addCrumb(
      new PHUICrumbView()
        ->setHref($this->getApplicationURI("publisher"))
        ->setName(pht("Publishers"))
        ->setIcon('fa-university'));
    $crumbs->addTextCrumb($publisher->getName(), $uri);
    $crumbs->setBorder(true);
    return $crumbs;
  }

  protected function buildExtensionsViewContent() {
    $viewer = $this->getViewer();
    $publisher = $this->getPublisher();

    $extensions = id(new ExtensionStoreExtensionQuery())
      ->setViewer($viewer)
      ->setLimit(10)
      ->withPublisherPHIDs(array($publisher->getPHID()))
      ->execute();


    $content = id(new ExtensionStoreExtensionSearchEngine())
      ->setViewer($viewer)
      ->renderResultsDirectly($extensions);

    // $content is PhabricatorApplicationSearchResultView
    $content->setNoDataString(pht('No package published yet!'));
    $box = new PHUIObjectBoxView();

    if ($content->getObjectList()) {
      $box->setObjectList($content->getObjectList());
    }
    if ($content->getTable()) {
      $box->setTable($content->getTable());
    }
    if ($content->getContent()) {
      $box->appendChild($content->getContent());
    }

    return $box;
  }
}
