<?php

/** Management view of Publisher */
final class ExtensionStorePublisherManageController
  extends ExtensionStoreBaseController {

  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(
      pht('This is basically the management view of a Publisher.'));

    $curtain = $this->buildCurtain();
    $details = $this->buildDetailsView();

    $timeline = $this->buildTransactionTimeline(
      $publisher,
      new ExtensionStorePublisherTransactionQuery());

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

    $view = id(new PHUITwoColumnView())
      ->setHeader($header)
      ->setCurtain($curtain)
      ->setMainColumn(array(
          $timeline,
        ))
      ->addPropertySection(pht('Description'), $details)
      ->addPropertySection($instances_header, $instances_view);


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

  private function buildCurtain() {
    $viewer = $this->getViewer();
    $publisher = $this->getPublisher();

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

    $can_create_package = true;

    $id = $publisher->getID();
    $publisher_key = $publisher->getPublisherKey();
    // TODO fix uris
    $edit_uri = $this->getApplicationURI("/publisher/edit/{$id}/");
    $store_uri = $this->getStoreUri("/e/{$publisher_key}/");
    $create_uri = './new/';
    $archive_uri = $this->getApplicationURI("/type/{$id}/archive/");

    $curtain = $this->newCurtainView($publisher);

    $curtain->addAction(
      id(new PhabricatorActionView())
        ->setName(pht('View In Store'))
        ->setIcon('fa-cart-plus')
        ->setHref($store_uri));

    $curtain->addAction(
      id(new PhabricatorActionView())
        ->setName(pht('Edit Publisher'))
        ->setIcon('fa-pencil')
        ->setDisabled(!$can_edit)
        ->setHref($edit_uri));

    $curtain->addAction(
      id(new PhabricatorActionView())
        ->setName(pht('New Extension'))
        ->setIcon('fa-plus')
        ->setDisabled(!$can_create_package)
        ->setHref($create_uri));

    if ($publisher->isArchived()) {
      $curtain->addAction(
        id(new PhabricatorActionView())
          ->setName(pht('Unarchive'))
          ->setIcon('fa-check')
          ->setDisabled(!$can_edit)
          ->setWorkflow($can_edit)
          ->setHref($archive_uri));
    } else {
      $curtain->addAction(
        id(new PhabricatorActionView())
          ->setName(pht('Archive Publisher'))
          ->setIcon('fa-ban')
          ->setDisabled(!$can_edit)
          ->setWorkflow($can_edit)
          ->setHref($archive_uri));
    }

    return $curtain;
  }

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

    $publisher_phid = $this->getPublisher()->getPHID();
    $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('View All'))
        ->setHref($all_extensions_uri)
        ->setTag('a')
        ->setButtonType(PHUIButtonView::BUTTONTYPE_SIMPLE))
      ->addActionLink((new PHUIButtonView())
        // TODO check policy
        ->setText(pht('New Extension'))
        ->setIcon('fa-plus')
        ->setHref('./new/')
        ->setTag('a')
        ->setButtonType(PHUIButtonView::BUTTONTYPE_SIMPLE));

    return array($header, $box);
  }

  public function buildApplicationCrumbs() {
    $crumbs = parent::buildApplicationCrumbs();

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

    return $crumbs;
  }
}
