import ABI from '../abi/UniswapV2Router02.json';
import { ethers } from 'ethers'
import Address from'../Address.js'


let Web3 = require('web3')
let _web3;
let contract;
let walletMgr;
let codeEnum;

function init (provider,_walletMgr) {
    walletMgr = _walletMgr;
    codeEnum = walletMgr.ReturnCodeEnum;
    _web3 = new Web3(provider);
    contract = new _web3.eth.Contract(ABI, Address.address.UniswapV2Router02)

}

function addLiquidity(tokenA,tokenB,amountA,amountB,amountAMax,amountBMax,to,dealine,fun,fun2){
    contract.methods.addLiquidity(tokenA,tokenB,amountA,amountB,amountAMax,amountBMax,to,dealine).send({
        from: walletMgr.currentAccount,
    })
    .on('transactionHash', function(hash){
        if (fun != null) fun({code:codeEnum.SUCCESS,data:hash})
    })
    .on('receipt', function(receipt){
        fun2({code:codeEnum.SUCCESS,data:receipt});
    })
    .on('error', function(error, receipt) {
        if(receipt == null){
            if (fun != null) fun(error)
        }else{
            if(fun2 != null) fun2({code:codeEnum.CONTRACT_ERROR,data:receipt})
        }
    })
}

function removeLiquidity(tokenA,tokenB,liquidity,amountAMin,amountBMin,to,dealine,fun,fun2){
    contract.methods.removeLiquidity(tokenA,tokenB,liquidity,amountAMin,amountBMin,to,dealine).send({
        from: walletMgr.currentAccount,
    })
    .on('transactionHash', function(hash){
        if (fun != null) fun({code:codeEnum.SUCCESS,data:hash})
    })
    .on('receipt', function(receipt){
        fun2({code:codeEnum.SUCCESS,data:receipt});
    })
    .on('error', function(error, receipt) {
        if(receipt == null){
            if (fun != null) fun(error)
        }else{
            if(fun2 != null) fun2({code:codeEnum.CONTRACT_ERROR,data:receipt})
        }
    })
}

function swapExactTokensForTokens(amountIn,amountOutMin,path,to,dealine,fun,fun2){
    contract.methods.swapExactTokensForTokens(amountIn,amountOutMin,path,to,dealine).send({
        from: walletMgr.currentAccount,
    })
    .on('transactionHash', function(hash){
        if (fun != null) fun({code:codeEnum.SUCCESS,data:hash})
    })
    .on('receipt', function(receipt){
        fun2({code:codeEnum.SUCCESS,data:receipt});
    })
    .on('error', function(error, receipt) {
        if(receipt == null){
            if (fun != null) fun(error)
        }else{
            if(fun2 != null) fun2({code:codeEnum.CONTRACT_ERROR,data:receipt})
        }
    })
}


function swapTokensForExactTokens(amountOut,amountOutMax,path,to,dealine,fun,fun2){
    contract.methods.swapTokensForExactTokens(amountOut,amountOutMax,path,to,dealine).send({
        from: walletMgr.currentAccount,
    })
    .on('transactionHash', function(hash){
        if (fun != null) fun({code:codeEnum.SUCCESS,data:hash})
    })
    .on('receipt', function(receipt){
        fun2({code:codeEnum.SUCCESS,data:receipt});
    })
    .on('error', function(error, receipt) {
        if(receipt == null){
            if (fun != null) fun(error)
        }else{
            if(fun2 != null) fun2({code:codeEnum.CONTRACT_ERROR,data:receipt})
        }
    })
}


 
async function getAmountsOut(amountIn,path,fun){
    console.log(path);
    let prices = await contract.methods.getAmountsOut(amountIn,path).call();
    fun({ code: codeEnum.SUCCESS, data: prices})
}


async function getAmountsIn(amountIn,path,fun){
    let prices = await contract.methods.getAmountsIn(amountIn,path).call();
    fun({ code: codeEnum.SUCCESS, data: prices})
}



export default {
    init,
    addLiquidity,
    removeLiquidity,
    getAmountsOut,
    swapExactTokensForTokens,
    getAmountsIn,
    swapTokensForExactTokens,
}


