import * as solana from '@solana/web3.js';

/*
(async () => {
  // Connect to cluster
  var connection = new web3.Connection(
    web3.clusterApiUrl('devnet'),
    'confirmed',
  );

  // Generate a new wallet keypair and airdrop SOL
  var wallet = web3.Keypair.generate();
  var airdropSignature = await connection.requestAirdrop(
    wallet.publicKey,
    web3.LAMPORTS_PER_SOL,
  );

  //wait for airdrop confirmation
  await connection.confirmTransaction(airdropSignature);

  // get account info
  // account data is bytecode that needs to be deserialized
  // serialization and deserialization is program specic
  let account = await connection.getAccountInfo(wallet.publicKey);
  console.log(account);
})();

*/

//import * as config from "./../algo-config.js";
import store from '../../store';

const Solana = {
  status: {
    PENDING: 0,
    SUCCESS: 1,
    FAILED: 2
  },

  isValidAddress: address => {
    //return solana.isOnCurve(address);
  },

  getClient: network => {
  /*
    if (store.chains.algo.rpc[network].chain.search("testnet") >= 0) {
      token = {
        "X-API-Key": store.chains.algo.rpc[network].key
      };
    }
*/
    return new solana.Connection(
    solana.clusterApiUrl('devnet'),
    'confirmed',
  );
  },

  createWallet: () => {
  
  var wallet = solana.Keypair.generate();
  

    return { address: wallet.publicKey, mnemonic: wallet.secretKey, sk: wallet.secretKey };
  },

  getWallet: mnemonic => {
  //
  },

  getAccount: async (network, address) => {
    return await Solana.getClient(network).accountInformation(address);
  },
/*
  createTransaction: async (network, { to, amount, memo, date }) => {
    let tx = await Algorand.getClient(network).getTransactionParams();
    tx.to = to;
    tx.amount = Number(amount) * Math.pow(10, 6); // convert to micro-algos.
    tx.genesisHash = tx.genesishashb64;

    const now = new Date().getTime();
    let lastRound = tx.lastRound;

    // Transaction to be processed in the future.
    if (date.getTime() > now) {
      // 1 block per 4.5s.
      const seconds = (date.getTime() - now) / 1000;
      const blocks = Math.ceil(seconds / 4.5);
      tx.firstRound = lastRound + blocks;
      tx.lastRound = lastRound + blocks + parseInt(1000);
      console.log(`Need to wait for ${blocks} blocks`);
    } else {
      tx.firstRound = lastRound;
      tx.lastRound = lastRound + parseInt(1000);
    }

    if (memo !== undefined) {
      tx.note = algosdk.encodeObj(memo);
    }

    return tx;
  },

  sendTransaction: async (network, tx, secretKey) => {
    try {
      let txParams = await Algorand.getClient(network).getTransactionParams();
      let lastRound = txParams.lastRound;

      if (tx.firstRound <= lastRound && lastRound <= tx.lastRound) {
        const signedTx = algosdk.signTransaction(tx, secretKey);
        const txResponse = await Algorand.getClientForTx(
          network
        ).sendRawTransaction(signedTx.blob);

        console.log(`Sent the transaction: ${txResponse.txId}`);

        await Algorand.checkTxStatus(network, txResponse.txId);

        return txResponse.txId;
      }

      // wait for atleast 4.5 seconds and try again.
      await Algorand.sleep(4500);
    } catch (err) {
      console.log(err);
      console.log("Failed to make an algorand transaction!");
    }

    return await Algorand.sendTransaction(network, tx, secretKey);
  },

  sleep: ms => {
    return new Promise(resolve => setTimeout(resolve, ms));
  },

  checkTxStatus: async (network, txId) => {
    try {
      return await Algorand.getClient(network).pendingTransactionInformation(
        txId
      );
    } catch (err) {
      return await Algorand.checkTxStatus(network, txId);
    }
  }
  */
};

export default Solana;
