/*
 * Copyright 2019 ConsenSys AG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package tech.pegasys.pantheon.tests.acceptance.permissioning;

import static org.assertj.core.api.Assertions.assertThat;

import tech.pegasys.pantheon.ethereum.core.Hash;
import tech.pegasys.pantheon.tests.acceptance.dsl.AcceptanceTestBase;
import tech.pegasys.pantheon.tests.acceptance.dsl.WaitUtils;
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.Condition;
import tech.pegasys.pantheon.tests.acceptance.dsl.condition.perm.NodeSmartContractPermissioningConditions;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.Node;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.cluster.Cluster;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.cluster.ClusterConfiguration;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.cluster.ClusterConfigurationBuilder;
import tech.pegasys.pantheon.tests.acceptance.dsl.node.configuration.permissioning.PermissionedNodeBuilder;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.Transaction;
import tech.pegasys.pantheon.tests.acceptance.dsl.transaction.perm.NodeSmartContractPermissioningTransactions;

import java.io.IOException;
import java.math.BigInteger;

class NodeSmartContractPermissioningAcceptanceTestBase extends AcceptanceTestBase {

  private final NodeSmartContractPermissioningTransactions smartContractNodePermissioning;
  private final NodeSmartContractPermissioningConditions nodeSmartContractPermissioningConditions;

  protected static final String CONTRACT_ADDRESS = "0x0000000000000000000000000000000000009999";
  protected static final String GENESIS_FILE = "/permissioning/simple_permissioning_genesis.json";

  protected final Cluster permissionedCluster;

  protected NodeSmartContractPermissioningAcceptanceTestBase() {
    super();
    smartContractNodePermissioning = new NodeSmartContractPermissioningTransactions(accounts);
    nodeSmartContractPermissioningConditions =
        new NodeSmartContractPermissioningConditions(smartContractNodePermissioning);
    this.permissionedCluster = permissionedCluster();
  }

  private Cluster permissionedCluster() {
    final ClusterConfiguration clusterConfiguration =
        new ClusterConfigurationBuilder().awaitPeerDiscovery(false).build();
    return new Cluster(clusterConfiguration, net);
  }

  protected Node permissionedNode(final String name, final Node... localConfigWhiteListedNodes) {
    PermissionedNodeBuilder permissionedNodeBuilder =
        this.permissionedNodeBuilder
            .name(name)
            .genesisFile(GENESIS_FILE)
            .nodesContractEnabled(CONTRACT_ADDRESS);
    if (localConfigWhiteListedNodes != null && localConfigWhiteListedNodes.length > 0) {
      permissionedNodeBuilder.nodesPermittedInConfig(localConfigWhiteListedNodes);
    }
    return permissionedNodeBuilder.build();
  }

  protected Node bootnode(final String name) {
    try {
      return pantheon.createCustomGenesisNode(name, GENESIS_FILE, true);
    } catch (IOException e) {
      throw new RuntimeException("Error creating node", e);
    }
  }

  protected Node node(final String name) {
    try {
      return pantheon.createCustomGenesisNode(name, GENESIS_FILE, false);
    } catch (IOException e) {
      throw new RuntimeException("Error creating node", e);
    }
  }

  protected Node miner(final String name) {
    try {
      return pantheon.createCustomGenesisNode(name, GENESIS_FILE, false, true);
    } catch (IOException e) {
      throw new RuntimeException("Error creating node", e);
    }
  }

  @Override
  public void tearDownAcceptanceTestBase() {
    permissionedCluster.stop();
    super.tearDownAcceptanceTestBase();
  }

  protected Transaction<Hash> allowNode(final Node node) {
    return smartContractNodePermissioning.allowNode(CONTRACT_ADDRESS, node);
  }

  protected Condition nodeIsAllowed(final Node node) {
    return nodeSmartContractPermissioningConditions.nodeIsAllowed(CONTRACT_ADDRESS, node);
  }

  protected Transaction<Hash> forbidNode(final Node node) {
    return smartContractNodePermissioning.forbidNode(CONTRACT_ADDRESS, node);
  }

  protected Condition nodeIsForbidden(final Node node) {
    return nodeSmartContractPermissioningConditions.nodeIsForbidden(CONTRACT_ADDRESS, node);
  }

  protected Condition connectionIsAllowed(final Node source, final Node target) {
    return nodeSmartContractPermissioningConditions.connectionIsAllowed(
        CONTRACT_ADDRESS, source, target);
  }

  protected Condition connectionIsForbidden(final Node source, final Node target) {
    return nodeSmartContractPermissioningConditions.connectionIsForbidden(
        CONTRACT_ADDRESS, source, target);
  }

  protected void waitForBlockHeight(final Node node, final long blockchainHeight) {
    WaitUtils.waitFor(
        120,
        () ->
            assertThat(node.execute(ethTransactions.blockNumber()))
                .isGreaterThanOrEqualTo(BigInteger.valueOf(blockchainHeight)));
  }
}
