<?php

final class PassphraseCredentialTransactionEditor
  extends PhabricatorApplicationTransactionEditor {

  public function getTransactionTypes() {
    $types = parent::getTransactionTypes();

    $types[] = PhabricatorTransactions::TYPE_VIEW_POLICY;
    $types[] = PhabricatorTransactions::TYPE_EDIT_POLICY;

    $types[] = PassphraseCredentialTransaction::TYPE_NAME;
    $types[] = PassphraseCredentialTransaction::TYPE_DESCRIPTION;
    $types[] = PassphraseCredentialTransaction::TYPE_USERNAME;
    $types[] = PassphraseCredentialTransaction::TYPE_SECRET_ID;
    $types[] = PassphraseCredentialTransaction::TYPE_DESTROY;

    return $types;
  }

  protected function getCustomTransactionOldValue(
    PhabricatorLiskDAO $object,
    PhabricatorApplicationTransaction $xaction) {
    switch ($xaction->getTransactionType()) {
      case PassphraseCredentialTransaction::TYPE_NAME:
        if ($this->getIsNewObject()) {
          return null;
        }
        return $object->getName();
      case PassphraseCredentialTransaction::TYPE_DESCRIPTION:
        return $object->getDescription();
      case PassphraseCredentialTransaction::TYPE_USERNAME:
        return $object->getUsername();
      case PassphraseCredentialTransaction::TYPE_SECRET_ID:
        return $object->getSecretID();
      case PassphraseCredentialTransaction::TYPE_DESTROY:
        return $object->getIsDestroyed();
    }

    return parent::getCustomTransactionOldValue($object, $xaction);
  }

  protected function getCustomTransactionNewValue(
    PhabricatorLiskDAO $object,
    PhabricatorApplicationTransaction $xaction) {
    switch ($xaction->getTransactionType()) {
      case PassphraseCredentialTransaction::TYPE_NAME:
      case PassphraseCredentialTransaction::TYPE_DESCRIPTION:
      case PassphraseCredentialTransaction::TYPE_USERNAME:
      case PassphraseCredentialTransaction::TYPE_SECRET_ID:
      case PassphraseCredentialTransaction::TYPE_DESTROY:
        return $xaction->getNewValue();
    }
    return parent::getCustomTransactionNewValue($object, $xaction);
  }

  protected function applyCustomInternalTransaction(
    PhabricatorLiskDAO $object,
    PhabricatorApplicationTransaction $xaction) {
    switch ($xaction->getTransactionType()) {
      case PassphraseCredentialTransaction::TYPE_NAME:
        $object->setName($xaction->getNewValue());
        return;
      case PassphraseCredentialTransaction::TYPE_DESCRIPTION:
        $object->setDescription($xaction->getNewValue());
        return;
      case PassphraseCredentialTransaction::TYPE_USERNAME:
        $object->setUsername($xaction->getNewValue());
        return;
      case PassphraseCredentialTransaction::TYPE_SECRET_ID:
        $old_id = $object->getSecretID();
        if ($old_id) {
          $this->destroySecret($old_id);
        }
        $object->setSecretID($xaction->getNewValue());
        return;
      case PassphraseCredentialTransaction::TYPE_DESTROY:
        // When destroying a credential, wipe out its secret.
        $is_destroyed = $xaction->getNewValue();
        $object->setIsDestroyed($is_destroyed);
        if ($is_destroyed) {
          $secret_id = $object->getSecretID();
          if ($secret_id) {
            $this->destroySecret($secret_id);
            $object->setSecretID(null);
          }
        }
        return;
      case PhabricatorTransactions::TYPE_VIEW_POLICY:
        $object->setViewPolicy($xaction->getNewValue());
        return;
      case PhabricatorTransactions::TYPE_EDIT_POLICY:
        $object->setEditPolicy($xaction->getNewValue());
        return;
    }

    return parent::applyCustomInternalTransaction($object, $xaction);
  }

  protected function applyCustomExternalTransaction(
    PhabricatorLiskDAO $object,
    PhabricatorApplicationTransaction $xaction) {

    switch ($xaction->getTransactionType()) {
      case PassphraseCredentialTransaction::TYPE_NAME:
      case PassphraseCredentialTransaction::TYPE_DESCRIPTION:
      case PassphraseCredentialTransaction::TYPE_USERNAME:
      case PassphraseCredentialTransaction::TYPE_SECRET_ID:
      case PassphraseCredentialTransaction::TYPE_DESTROY:
      case PhabricatorTransactions::TYPE_VIEW_POLICY:
      case PhabricatorTransactions::TYPE_EDIT_POLICY:
        return;
    }

    return parent::applyCustomExternalTransaction($object, $xaction);
  }

  private function destroySecret($secret_id) {
    $table = new PassphraseSecret();
    queryfx(
      $table->establishConnection('w'),
      'DELETE FROM %T WHERE id = %d',
      $table->getTableName(),
      $secret_id);
  }

  protected function validateTransaction(
    PhabricatorLiskDAO $object,
    $type,
    array $xactions) {

    $errors = parent::validateTransaction($object, $type, $xactions);

    switch ($type) {
      case PassphraseCredentialTransaction::TYPE_NAME:
        $missing = $this->validateIsEmptyTextField(
          $object->getName(),
          $xactions);

        if ($missing) {
          $error = new PhabricatorApplicationTransactionValidationError(
            $type,
            pht('Required'),
            pht('Credential name is required.'),
            nonempty(last($xactions), null));

          $error->setIsMissingFieldError(true);
          $errors[] = $error;
        }
        break;
      case PassphraseCredentialTransaction::TYPE_USERNAME:
        $missing = $this->validateIsEmptyTextField(
          $object->getUsername(),
          $xactions);

        if ($missing) {
          $error = new PhabricatorApplicationTransactionValidationError(
            $type,
            pht('Required'),
            pht('Username is required.'),
            nonempty(last($xactions), null));

          $error->setIsMissingFieldError(true);
          $errors[] = $error;
        }
        break;
    }

    return $errors;
  }


}
