<?php

/** Store view of Publisher */
final class ExtensionStorePublisherBrowseController
  extends ExtensionStoreBrowseBaseController {

  use ExtensionStorePublisherControllerTrait;

  public function handleRequest(AphrontRequest $request) {
    $publisher = $this->findPublisher($request);
    if (!$publisher) {
      return new Aphront404Response();
    }

    $crumbs = $this->buildApplicationCrumbs();
    $title = $publisher->getName();

    $header = $this->buildHeaderView();
    $header->setSubheader(
      array(
        pht(
          'This is basically the store view of a Publisher, for users who '.
          'are looking for extensions. '),
        pht(
          'The same controller is used for both the dedicated domain, '.
          'and for the `/extensionstore/view/` location. '.
          'In the dedicated domain, there\'s no way to log-in.'),
    ));

    $details = $this->buildDetailsView();


    list($instances_header, $instances_view) = $this->buildExtensionsView();

    $view = new PHUITwoColumnView()
      ->setHeader($header)
      ->addPropertySection(pht('Description'), $details)
      ->addPropertySection($instances_header, $instances_view);


    return $this->newPage()
      ->setTitle($title)
      ->setCrumbs($crumbs)
      ->setPageObjectPHIDs(array($publisher->getPHID()))
      ->appendChild($view);
  }

  private function buildExtensionsView() {
    $box = $this->buildExtensionsViewContent();


    $publisher_phid = $this->getPublisher()->getPHID();
    // TODO
    $all_extensions_uri =
      $this->getApplicationURI("/extension/?publisherPHID={$publisher_phid}");

    $header = pht('Extensions by this Publisher');

    $header = (new PHUIHeaderView())
      ->setHeader($header)
      ->setHeaderIcon('fa-gift')
      ->addActionLink((new PHUIButtonView())
        ->setText(pht('See all'))
        ->setHref($all_extensions_uri)
        ->setTag('a')
        ->setDisabled(true)
        ->setButtonType(PHUIButtonView::BUTTONTYPE_SIMPLE));

    return array($header, $box);
  }

  public function buildApplicationCrumbs() {
    $publisher = $this->getPublisher();
    $id = $publisher->getID();
    $publisherKey = $publisher->getPublisherKey();

    $manage_uri = $this->getFullApplicationURI("/publisher/{$publisherKey}/");

    $crumbs = parent::buildApplicationCrumbs();

    $crumbs = $this->addApplicationCrumbsSection($crumbs);

    $crumbs->addAction(
      new PHUIListItemView()
        ->setName(pht('Manage'))
        ->setHref($manage_uri)
        ->setIcon('fa-pencil'));

    return $crumbs;
  }
}
