<?php

trait ExtensionStoreExtensionControllerTrait {

  private $extension;
  private ?ExtensionStorePublisher $publisher = null ;
  private ?ExtensionStoreRelease $latestRelease = null;

  public function setExtension(ExtensionStoreExtension $extension) {
    $this->extension = $extension;
    $this->setPublisher($extension->getPublisher());
    return $this;
  }

  public function getExtension(): ?ExtensionStoreExtension {
    return $this->extension;
  }

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

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

  protected function getLatestRelease() {
    return $this->latestRelease;
  }

  protected function findExtension(AphrontRequest $request) {
    $viewer = $request->getViewer();
    $extension_id = $request->getURIData('id');
    if ($extension_id === null) {
      $extension_id = $request->getURIData('extensionId');
    }

    $publisher_key = $request->getURIData('publisherKey');
    $extension_key = $request->getURIData('extensionKey');

    $query = new ExtensionStoreExtensionQuery()
      ->setViewer($viewer)
      ->needLatestRelease(true)
      ->needPublishers(true);

    if ($extension_id !== null) {
      $query->withIDs(array($extension_id));
    } else if ($publisher_key !== null && $extension_key != null) {
      $query
        ->withPublisherKeys(array($publisher_key))
        ->withExtensionKeys(array($extension_key));
    } else {
      return null;
    }

    $extension = $query->executeOne();
    if (!$extension) {
      return null;
    }

    $this->setExtension($extension);

    $this->latestRelease = new ExtensionStoreReleaseQuery() // TODO make property of Extension.
      ->setViewer($viewer)
      ->withExtension($extension)
      ->setOrder('newest')
      ->setLimit(1)
      ->executeOne();

  $this->latestRelease = $extension->getLatestRelease();

    return $extension;
  }

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

    if ($extension->isArchived()) {
      $status_icon = 'fa-ban';
      $status_color = 'dark';
    } else {
      $status_icon = 'fa-check';
      $status_color = 'bluegrey';
    }

    $status_name =
      ExtensionStoreExtensionStatus::getStatusName($extension->getStatus());

    return id(new PHUIHeaderView())
      ->setHeader(
        pht(
          '%s: %s',
          $publisher->getName(),
          $extension->getName()))
      ->setViewer($viewer)
      ->setPolicyObject($publisher)
      ->setStatus($status_icon, $status_color, $status_name)
      ->setHeaderIcon('fa-gift');
  }


  protected function buildDetailsView() {
    $viewer = $this->getViewer();
    $extension = $this->getExtension();

    $full_ext_key = $extension->getFullExtensionKey();

    $view = id(new PHUIPropertyListView())
      ->setViewer($viewer)
      ->addProperty(pht('Extension Key'), $full_ext_key)
      ->addProperty(pht('Phutil library name'), $extension->getPhutilLibName())
      ->addProperty(pht('Category'), $extension->getCategory());


    switch ($extension->getCategory()) {
      case 'server':
        $to_install = sprintf('./bin/extensions install %s', $full_ext_key);
        break;

      default:
        $to_install = pht('Ask the AI, it usually knows everything.');
        break;
    }

    $view->addProperty(
      pht('To install'),
      phutil_tag('tt', array(), $to_install));

    $release = $this->getLatestRelease();
    if ($release) {
      $release_text = pht(
        '%s, released on %s',
        $release->getName(),
        phabricator_time($release->getDateCreated(), $viewer));
    } else {
      $release_text = pht("Nothing yet");
    }
    $view->addProperty(pht('Latest Release'), $release_text);

    $description = $extension->getDescription();
    if (strlen($description)) {
      $view->addSectionHeader(pht('Description'));
      $view->addTextContent(
        new PHUIRemarkupView($viewer, $description));
    }

    return $view;
  }

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

    $crumbs->addTextCrumb(
      $publisher->getName(),
      $publisher->getURI('items/'));
    if ($extension) {
      $crumbs->addTextCrumb(
        pht('%d %s', $extension->getID(), $extension->getName()), $extension->getURI());
    }
    $crumbs->setBorder(true);
    return $crumbs;
  }


}
