<?php

/** Store view of Extension */
final class ExtensionStoreExtensionBrowseController
  extends ExtensionStoreBrowseBaseController {

  use ExtensionStoreExtensionControllerTrait;

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

    $this->setExtension($extension);
    $publisher = $this->getPublisher();

    $crumbs = $this->buildApplicationCrumbs();
    $title = pht(
      '%s: %s %s',
      $publisher->getName(),
      $extension->getExtensionKey(),
      $extension->getName());

    $header = $this->buildHeaderView();
    $details = $this->buildDetailsView();

    $timeline = $this->buildTransactionTimeline(
      $extension,
      new ExtensionStoreExtensionTransactionQuery());

    $view = new PHUITwoColumnView()
      ->setHeader($header)
      ->setMainColumn(array())
      ->addPropertySection(pht('Details'), $details);

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


  protected function buildApplicationCrumbs() {
    $id = $this->getExtension()->getID();
    $manage_uri = $this->getFullApplicationURI("/extension/{$id}/");

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

    return $crumbs;
  }
}
