
import MetaMask from './MetaMask.js';
import WalletConnect from './WalletConnect.js';
import BinanceChain from './BinanceChain.js';
import Erc20 from './contract/Erc20'
import UniswapV2Router02 from './contract/UniswapV2Router02'
import UniswapV2Library from './contract/UniswapV2Library'
var tp = require('tp-js-sdk')
let chainTab = {
    BSC: [{
        chainId: "0x38", // A 0x-prefixed hexadecimal string
        chainName: "Binance Smart Chain Mainnet",
        nativeCurrency: {
            name: "BNB",
            symbol: "BNB", // 2-6 characters long
            decimals: 18,
        },
        rpcUrls: ['https://bsc-dataseed.binance.org/:443'],
        blockExplorerUrls: ['https://bscscan.com/'],
    }],
    BSCTest: [{
        chainId: "0x61", // A 0x-prefixed hexadecimal string
        chainName: "Binance Smart Chain Testnet",
        nativeCurrency: {
            name: "tBNB",
            symbol: "tBNB", // 2-6 characters long
            decimals: 18,
        },
        rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
        blockExplorerUrls: ['https://bscscan.com/'],
    }],
    Rinkeby: [{
        chainId: "0x4", // A 0x-prefixed hexadecimal string
        chainName: "Rinkeby Test",
        nativeCurrency: {
            name: "ETH",
            symbol: "ETH", // 2-6 characters long
            decimals: 18,
        },
        rpcUrls: ['https://rinkeby.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'],
        blockExplorerUrls: ['https://rinkeby.etherscan.io'],
    }],
}

const WalletEnum = {
    MetaMask: 1,
    WalletConnect: 2,
    BinanceChain: 3,
}

const ReturnCodeEnum ={
    SUCCESS : 0,                
    METAMASK_NOT_INSTALL:101,   
    NOT_CONNECTED:102,          
    NOT_CURRENT_CHAIN:103,     
    SIGNATURE_FAILED:105,       
    CONTRACT_ERROR:200,          
}

var currentChain = chainTab.BSCTest;   
var walletCurType=null;
let currentWallet = null;

function init (_type) {
    switch (_type) {
        case WalletEnum.MetaMask:
            currentWallet = MetaMask;
            break
        case WalletEnum.WalletConnect:
            currentWallet = WalletConnect;
            break
        case WalletEnum.BinanceChain:
            currentWallet = BinanceChain;
            break
        default:
            break;
    }
    localStorage.setItem('walletConnectType', walletCurType)
}

function changeWalletType (_type,fun) {
    walletCurType = _type;
    init(walletCurType);
    currentWallet.init(this,fun);
}

function crateContract(provider){
    Erc20.init(provider,this)
    UniswapV2Router02.init(provider,this)
    UniswapV2Library.init(provider,this)
}

function isTokenPocker () {
    return tp.isConnected();
}

function getWalletCurType () {
    return walletCurType;
}

function connectWallectAccount (func) {
    currentWallet.connectWallectAccount(func);
}

async function addTokensToMetaMask(address){
    if(currentWallet == MetaMask){
        let info = await Erc20.getTokenInfo(address);
        console.log(info.symbol, info.decimals)
        MetaMask.addTokens(info);
    }
}

export default {
    WalletEnum,
    currentChain,
    // currentAccount,
    // currentChaidId,
    ReturnCodeEnum,
    getWalletCurType,
    init,
    changeWalletType,
    connectWallectAccount,
    isTokenPocker,
    crateContract,
    addTokensToMetaMask,
}