contract UNIVE is IERC20, Ownable {
    using IterableMapping for IterableMapping.Map;
    IterableMapping.Map private tokenHoldersMap;

    IUniswapV2Router02 public uniswapV2Router;
    //address public routerAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; //uniswap router2 address
    address public routerAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; //uniswap router2 address for 4 main testnet
    //address public routerAddress = 0x10ED43C718714eb63d5aA57B78B54704E256024E; //pancakeswap router2 address
    //address public routerAddress = 0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3; //pancakeswap router2 testnet address
    address public uniswapV2Pair;

    string constant public name = "UNIVE X";
    string constant public symbol = "UNVX";
    uint8 constant public decimals = 8;

    uint256 private _totalSupply = 500000000 * 10**8;
    uint256 public _finalSupply = 50000000 * 10**8;
    uint256 public _totalLiquidityFee = 0;

    uint8 public feePurchase = 10;
    uint8 public feePurchaseHolder = 5;
    uint8 public feePurchaseBurn = 2;
    uint8 public feePurchaseLiquidity = 3;
    uint8 public feeSale = 15;
    uint8 public feeSaleHolder = 10;
    uint8 public feeSaleBurn = 2;
    uint8 public feeSaleLiquidity = 3;
    uint256 private feeTotalAmount = 0;    
    uint256 private feeHolderAmount = 0;
    uint256 private feeBurnAmount = 0;
    uint256 private feeLiquidityAmount = 0;
    uint256 public holderThreshold = 20000 * 10**8;
    uint256 public transactionThreshold = 1000 * 10**8;
    uint256 public liquidityThreshold = 10000 * 10**8;
    uint256 public rewardThreshold = 1000 * 10**8;
    bool public taxEnable = true;
    uint256 public maxTxAmount = 10000*10**8;
    
    mapping(bytes32 => uint256) public bridged;   
    mapping (address => bool) public isExcludedFromTax;
    mapping (address => bool) public AllowedContract;
    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;

    bool private swapping;
    modifier lockTheSwap {
        swapping = true;
        _;
        swapping = false;
    }

    event TransferFee(address sender, address recipient, uint256 amount);
    event SwapAndLiquify(uint256 tokensSwapped, uint256 ethReceived, uint256 tokensIntoLiqudity);
    
    constructor() {        
        _balances[_msgSender()] = _totalSupply;
        isExcludedFromTax[_msgSender()] = true;
        isExcludedFromTax[routerAddress] = true;
        tokenHoldersMap.set(_msgSender(), 0, 0, true);
        
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(routerAddress);
        // Create a uniswap pair for this new token
        //address _uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());

        uniswapV2Router = _uniswapV2Router;
        //uniswapV2Pair = _uniswapV2Pair;
    }

    function setRouterAddress(address routerAddress_) external onlyOwner {
        routerAddress = routerAddress_;
        uniswapV2Router = IUniswapV2Router02(routerAddress);         
    }

    function setUniswapV2Pair(address uniswapV2Pair_) external onlyOwner {
        uniswapV2Pair = uniswapV2Pair_;  
    }

    function setFeePurchase(uint8 feePurchaseHolder_, uint8 feePurchaseBurn_, uint8 feePurchaseLiquidity_) external onlyOwner {
        require(feePurchaseHolder_+feePurchaseBurn_+feePurchaseLiquidity_ < 100, "UNIVE: Total fee exceed 100%");
        feePurchaseHolder = feePurchaseHolder_;
        feePurchaseBurn = feePurchaseBurn_;
        feePurchaseLiquidity = feePurchaseLiquidity_;
        feePurchase = feePurchaseHolder + feePurchaseBurn + feePurchaseLiquidity;
    }

    function setFeeSale(uint8 feeSaleHolder_, uint8 feeSaleBurn_, uint8 feeSaleLiquidity_) external onlyOwner {
        require(feeSaleHolder_+feeSaleBurn_+feeSaleLiquidity_ <= 100, "UNIVE: Total fee exceed 100%");
        feeSaleHolder = feeSaleHolder_;
        feeSaleBurn = feeSaleBurn_;
        feeSaleLiquidity = feeSaleLiquidity_;
        feeSale = feeSaleHolder + feeSaleBurn + feeSaleLiquidity;
    }

    function setThreshold(uint256 holderThreshold_, uint256 transactionThreshold_, uint256 liquidityThreshold_, uint256 rewardThreshold_) external onlyOwner {
        holderThreshold = holderThreshold_;
        transactionThreshold = transactionThreshold_;
        liquidityThreshold = liquidityThreshold_;
        rewardThreshold = rewardThreshold_;
    }

    function setTaxEnable(bool enable) external onlyOwner {
        taxEnable = enable;
    }

    function setExcludeFromTax(address address_, bool isExcluded) external onlyOwner {
        isExcludedFromTax[address_] = isExcluded;
    }

    function setRewardStatus(address address_, bool isStatus) external onlyOwner {   
        (uint total, uint reward, bool status) = tokenHoldersMap.get(address_);
        require(status != isStatus, "You have already done");
        tokenHoldersMap.set(address_, total, reward, isStatus);
    }

    function getHolderStatus(address address_) external view returns (uint, uint, bool) {
        (uint total, uint reward, bool status) = tokenHoldersMap.get(address_);
        return (total, reward, status);
    }

    function getNumberOfHolders() external view returns (uint256) {
        return tokenHoldersMap.keys.length;
    }

    function getETHBalance() external view returns (uint256) {
        return address(this).balance;
    }

    //to recieve ETH(BNB) from uniswapV2Router when swaping
    receive() external payable {}

    /**
     * @notice claim rewards (direct use)
     *
     */
    function claim(address receiver, address token) external {
        (uint256 total, uint256 reward, bool status) = tokenHoldersMap.get(_msgSender());

        require(status, "The reward is not allowed");
        require(reward > rewardThreshold, "Reward insufficient to withdraw");

        // get ETH from reward(UNIVE token) of the holder
        uint256 initialBalance = address(this).balance;
         
        // generate the uniswap pair path of token -> weth
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = uniswapV2Router.WETH();
        _approve(address(this), address(uniswapV2Router), reward);

        //make the swap to get ETH from the token
        uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
            reward,
            0, // accept any amount of ETH
            path,
            address(this),
            block.timestamp
        );

        uint256 newBalance = address(this).balance - initialBalance;

        if (token == uniswapV2Router.WETH()) {
            // send the reward as "ETH"
            if (newBalance > 0){
                _approve(address(this), receiver, newBalance);
                payable(receiver).transfer(newBalance);
                emit Transfer(address(this), receiver, reward); 
            } 
        } else {
            address[] memory path1 = new address[](2);
            path1[0] = uniswapV2Router.WETH();
            path1[1] = token;
            IERC20(uniswapV2Router.WETH()).approve(address(uniswapV2Router), newBalance);
            _approve(address(this), address(uniswapV2Router), newBalance);
            //make the swap to get the token required from ETH
            uniswapV2Router.swapExactETHForTokensSupportingFeeOnTransferTokens{value:newBalance}(
                0, // accept any amount of ETH
                path1,
                receiver,
                block.timestamp
            );   
        }        
        tokenHoldersMap.set(_msgSender(), total, 0, true);     
    }    

    /* function to make the deposit for bridge */
    function deposit(uint256 amount) public {
    	require(amount > 0, "0");
        require(amount <= maxTxAmount, "Deposit amount exceeds Max Tx amount");
        
        _approve(_msgSender(), address(this), amount);
        _transfer(_msgSender(), address(this), amount);
    }

    /* function to send the token to the receiver for bridge */
    function bridge(address toWallet, uint256 amount, bytes32 txHash) public onlyOwner{
        require(bridged[txHash] <= 0, "already bridged");
        require(amount <= maxTxAmount, "Claim amount exceeds Max Tx amount");

        _transfer(address(this), toWallet, amount);
        bridged[txHash] = amount;
    }

    function burn(uint256 amount) external onlyOwner{
        uint256 accountBalance = _balances[_msgSender()];
        require(accountBalance >= amount, "ERC20: burn amount exceeds balance");

        _totalSupply -= amount;
        require(_totalSupply >= _finalSupply, "ERC20:  can not burn anymore");
        unchecked {
            _balances[_msgSender()] = accountBalance - amount;
        }        
        emit Transfer(_msgSender(), address(0), amount);
    }

    function increaseAllowance(address spender, uint256 addedValue) external returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) external returns (bool) {
        uint256 currentAllowance = _allowances[_msgSender()][spender];
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        unchecked {
            _approve(_msgSender(), spender, currentAllowance - subtractedValue);
        }
        return true;
    }
    
    function AllowContractTransfer(address address_, bool isAllowed) external onlyOwner {
        AllowedContract[address_] = isAllowed;
    }

    function totalSupply() external view override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) external view override returns (uint256) {
        return _balances[account];
    }

    function allowance(address owner, address spender) external view override returns (uint256) {
        return _allowances[owner][spender];
    }

    function transfer(address recipient, uint256 amount) external override returns (bool)  {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    function approve(address spender, uint256 amount) external override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }
    
    function transferFrom( address sender, address recipient, uint256 amount) external override returns (bool) {
        _transfer(sender, recipient, amount);
        
        if (sender == _msgSender() || AllowedContract[_msgSender()]) {
            require(_allowances[_msgSender()][sender] >= amount, "ERC20: transfer amount(custom) exceeds allowance");
            unchecked {
                _approve(_msgSender(), sender, _allowances[_msgSender()][sender]- amount);
            }
        } else {
            require(_allowances[sender][_msgSender()] >= amount, "ERC20: transfer amount exceeds allowance");
            unchecked {
                _approve(sender, _msgSender(), _allowances[sender][_msgSender()]- amount);
            }
        }

        return true;
    }

    function _transfer( address sender, address recipient, uint256 amount ) private {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        
        uint256 senderBalance = _balances[sender];
        require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
        _balances[sender] = senderBalance - amount;
        
        //uint256 receiveAmount = amount;
        if (taxEnable){
            //when purchase this token
            if (sender == uniswapV2Pair) {            
                if (!isExcludedFromTax[recipient]){
                    feeHolderAmount = amount * feePurchaseHolder / 100;
                    feeBurnAmount = amount * feePurchaseBurn / 100;
                    feeLiquidityAmount = amount * feePurchaseLiquidity / 100;
                    feeTotalAmount = feeHolderAmount + feeBurnAmount + feeLiquidityAmount;
                    //receiveAmount = amount - feeTotalAmount;  
                }            
            }
            //when sell this token
            if (recipient == uniswapV2Pair) {
                if (!isExcludedFromTax[sender]){
                    feeHolderAmount = amount * feeSaleHolder / 100;
                    feeBurnAmount = amount * feeSaleBurn / 100;
                    feeLiquidityAmount = amount * feeSaleLiquidity / 100;
                    feeTotalAmount = feeHolderAmount + feeBurnAmount + feeLiquidityAmount;
                    //receiveAmount = amount - feeTotalAmount;
                }            
            }  
        }

        _balances[recipient] += amount;
        // When this token is transferred, add or remove the sender(recipient) into the holder list by the holderThreshold
        if (_balances[recipient] > holderThreshold){
            int userExist = tokenHoldersMap.getIndexOfKey(recipient);
            if (userExist == -1) {
                tokenHoldersMap.set(recipient, 0, 0, true);
            }
        }

        if (_balances[sender] < holderThreshold){
            (uint total, uint256 reward, bool status) = tokenHoldersMap.get(sender);
            status = false;
            tokenHoldersMap.set(sender, total, reward, false);
        }        
        
        // process the tax
        if ( taxEnable && (amount > transactionThreshold) && ((sender == uniswapV2Pair&&!isExcludedFromTax[recipient]) || (recipient == uniswapV2Pair&&!isExcludedFromTax[sender])) ) {   
            //Each holder must receive a number of rewards(BNB or ETH) in proportion to the number of tokens he owns.     
            if (feeHolderAmount > 0){
                uint256 iterations = 0;
                
                while(iterations < tokenHoldersMap.keys.length) {
                    address account = tokenHoldersMap.keys[iterations];
                    (uint total, uint256 reward, bool status) = tokenHoldersMap.get(account);
                    if (status){
                        uint256 reward_add = feeHolderAmount *_balances[account] / _totalSupply;
                        tokenHoldersMap.set(account, total + reward_add, reward + reward_add, true);
                    }     
                    iterations++;
                }
            }
            
            // 2% of each transaction burned            
            if ((_totalSupply - feeBurnAmount) > _finalSupply) {
                _totalSupply -= feeBurnAmount;   
            }
            
            //5% automatically injected into liquidity
            if (feeLiquidityAmount>0) {
                _totalLiquidityFee += feeLiquidityAmount;
            }
        }   

        emit Transfer(sender, recipient, amount);
    }
    
    function _approve( address owner,  address spender, uint256 amount ) private {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }
}
//to recieve ETH(BNB) from uniswapV2Router when swaping