contract PrimeMinisterInu is Context, IERC20, Ownable {
    
    using SafeMath for uint256;
    using Address for address;
    

    uint256 private _maxsupply = 1000000000 * 10**9;
    uint256 public limitbuy = 20000000 * 10**9;
    uint256 public _walletMax = 30000000 * 10**9;
    uint256 private mintokensbeforetrade = 10000 * 10**9; 
    uint256 public _antibot = 0;  
    uint256 public _buytax = 0;
    uint256 public _selltax = 0;
    
    mapping (address => uint256) _balances;
    mapping (address => mapping (address => uint256)) private _allowances;

    address payable public marketingWalletAddress = payable(0xdb936b7e5386aA70cCC146d7FA7aa769c1ddc6c4); 
    address payable public rewardsWalletAddress = payable(0x2e9A2f7a2e95284a51E430658e1347c8b27F7A69); 
    address public immutable zeroAddress = 0x000000000000000000000000000000000000dEaD; // burn address
  
   
    mapping (address => bool) public nofeeforaddress;
    mapping (address => bool) public nolimitwalletaddress;
    mapping (address => bool) public blacklistbot;

    string private _name = "PRIME MINISTER INU";
    string private _symbol = "$PRIMEINU";
    uint8 private _decimals = 9;
    
    uint256 public _autolp = 2;
    uint256 public _marketing = 5;
    uint256 public _development = 5;  

 
    
    IUniswapV2Router02 public uniswapV2Router;
    address public uniswapV2Pair;
    
    bool inSwapAndLiquify;
    bool public swapAndLiquifyEnabled = true;
    bool public swapAndLiquifyByLimitOnly = false;
    bool public checkWalletLimit = true;

    event SwapAndLiquifyEnabledUpdated(bool enabled);
    event SwapAndLiquify(
        uint256 tokensSwapped,
        uint256 ethReceived,
        uint256 tokensIntoLiqudity
    );
    
    event SwapETHForTokens(
        uint256 amountIn,
        address[] path
    );
    
    event SwapTokensForETH(
        uint256 amountIn,
        address[] path
    );
    
    modifier lockTheSwap {
        inSwapAndLiquify = true;
        _;
        inSwapAndLiquify = false;
    }
    
    constructor () {
        
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D); 

        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());

        uniswapV2Router = _uniswapV2Router;
        _allowances[address(this)][address(uniswapV2Router)] = _maxsupply;

        _buytax = _autolp.add(_marketing).add(_development);
        _selltax = _buytax.add(_antibot);
        
        nofeeforaddress[owner()] = true;
        nofeeforaddress[address(this)] = true;
        
        nolimitwalletaddress[owner()] = true;
        nolimitwalletaddress[address(uniswapV2Pair)] = true;

        _balances[_msgSender()] = _maxsupply;
        emit Transfer(address(0), _msgSender(), _maxsupply);
    }

    function name() public view returns (string memory) {
        return _name;
    }

    function symbol() public view returns (string memory) {
        return _symbol;
    }

    function decimals() public view returns (uint8) {
        return _decimals;
    }

    function MaxSupply() public view override returns (uint256) {
        return _maxsupply;
    }

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

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

    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }


    function approve(address spender, uint256 amount) public override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    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);
    }

    function blacklist(address account, bool newValue) public onlyOwner {
        blacklistbot[account] = newValue;
    }

    function ExcludedFromFee(address account, bool newValue) public onlyOwner {
        nofeeforaddress[account] = newValue;
    }

    function setvariable(uint256 newautolp, uint256 newmarketingfee, uint256 newdevelopmentfee, uint256 newbotblock) external onlyOwner() {
        
        _autolp = newautolp;
        _marketing = newmarketingfee;
        _development = newdevelopmentfee;
        _antibot = newbotblock;
        _buytax = _autolp.add(_marketing).add(_development);
        _selltax = _buytax.add(_antibot);
    }


    function openlimitwalletfunction(bool newValue) external onlyOwner {
       checkWalletLimit = newValue;
    }

    // use for presale contract
    function setnolimitwallet(address holder, bool exempt) external onlyOwner {
        nolimitwalletaddress[holder] = exempt;
    }

    function tokensbeforetrade(uint256 newLimit) external onlyOwner() {
        mintokensbeforetrade = newLimit;
    }

    function setSwapAndLiquifyByLimitOnly(bool newValue) public onlyOwner {
        swapAndLiquifyByLimitOnly = newValue;
    }
    

    function transferToAddressETH(address payable recipient, uint256 amount) private {
        recipient.transfer(amount);
    }
   
    function takeFee(address sender, address recipient, uint256 amount) internal returns (uint256) {
        
        uint256 feeAmount = recipient == uniswapV2Pair ? amount.mul(_selltax).div(100) 
                                : amount.mul(_buytax).div(100);

        _balances[address(this)] = _balances[address(this)].add(feeAmount);
        emit Transfer(sender, address(this), feeAmount);

        return amount.sub(feeAmount);
    }

    function marketingwallet(address newAddress) external onlyOwner() {
        marketingWalletAddress = payable(newAddress);
    }

    function airdropwallet(address newAddress) external onlyOwner() {
        rewardsWalletAddress = payable(newAddress);
    }

    function changeRouter(address newRouterAddress) public onlyOwner returns(address newPairAddress) {

        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(newRouterAddress); 

        newPairAddress = IUniswapV2Factory(_uniswapV2Router.factory()).getPair(address(this), _uniswapV2Router.WETH());

        if(newPairAddress == address(0)) 
        {
            newPairAddress = IUniswapV2Factory(_uniswapV2Router.factory())
                .createPair(address(this), _uniswapV2Router.WETH());
        }

        uniswapV2Pair = newPairAddress; 
        uniswapV2Router = _uniswapV2Router; 
    }

    receive() external payable {}

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

    function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
        return true;
    }

    function _transfer(address sender, address recipient, uint256 amount) private returns (bool) {

        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        require(!blacklistbot[sender] && !blacklistbot[recipient], "To/from address is blacklisted!");
        require(amount > 0, "Transfer amount must be greater than zero");
        
        if(inSwapAndLiquify)
        { 
            return _basicTransfer(sender, recipient, amount); 
        }
        else
        {
            if(sender != owner() && recipient != owner()) {
                require(amount <= limitbuy, "Transfer amount exceeds the maxbuylimit.");
            }
            
            uint256 contractTokenBalance = balanceOf(address(this));
            bool overMinimumTokenBalance = contractTokenBalance >= mintokensbeforetrade;
            
            if (overMinimumTokenBalance && !inSwapAndLiquify && sender != uniswapV2Pair && swapAndLiquifyEnabled) 
            {
                if(swapAndLiquifyByLimitOnly)
                    contractTokenBalance = mintokensbeforetrade;
                swapAndLiquify(contractTokenBalance);    
            }

            _balances[sender] = _balances[sender].sub(amount, "Insufficient Balance");

            uint256 finalAmount = (nofeeforaddress[sender] || nofeeforaddress[recipient]) ? 
                                         amount : takeFee(sender, recipient, amount);

            if(checkWalletLimit && !nolimitwalletaddress[recipient])
                require(balanceOf(recipient).add(finalAmount) <= _walletMax);

            _balances[recipient] = _balances[recipient].add(finalAmount);

            emit Transfer(sender, recipient, finalAmount);
            return true;
        }
    }

    function _basicTransfer(address sender, address recipient, uint256 amount) internal returns (bool) {
        _balances[sender] = _balances[sender].sub(amount, "Insufficient Balance");
        _balances[recipient] = _balances[recipient].add(amount);
        emit Transfer(sender, recipient, amount);
        return true;
    }

    function swapAndLiquify(uint256 tAmount) private lockTheSwap {
       
        uint256 tokensForLP = tAmount.div(_buytax).mul(_autolp).div(2);
        uint256 tokensForSwap = tAmount.sub(tokensForLP);

        swapTokensForEth(tokensForSwap);
        uint256 amountReceived = address(this).balance;

        uint256 totalETHFee = _buytax.sub(_autolp.div(2));
        
        uint256 amountLiquidity = amountReceived.mul(_autolp).div(totalETHFee).div(2);
        uint256 amountRewards = amountReceived.mul(_development).div(totalETHFee);
        uint256 amountMarketing = amountReceived.sub(amountLiquidity).sub(amountRewards);

        transferToAddressETH(marketingWalletAddress, amountMarketing);
        transferToAddressETH(rewardsWalletAddress, amountRewards);
        addLiquidity(tokensForLP, amountLiquidity);
    }
    
    function swapTokensForEth(uint256 tokenAmount) private {
        // 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), tokenAmount);

        // make the swap
        uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
            tokenAmount,
            0, // accept any amount of ETH
            path,
            address(this), // The contract
            block.timestamp
        );
        
        emit SwapTokensForETH(tokenAmount, path);
    }

    function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
        // approve token transfer to cover all possible scenarios
        _approve(address(this), address(uniswapV2Router), tokenAmount);

        // add the liquidity
        uniswapV2Router.addLiquidityETH{value: ethAmount}(
            address(this),
            tokenAmount,
            0, // slippage is unavoidable
            0, // slippage is unavoidable
            owner(),
            block.timestamp
        );
    }
    function limitwallet(uint256 newLimit) external onlyOwner {
        _walletMax  = newLimit;
    }

    function newlimitbuy(uint256 maxbuy) external onlyOwner() {
        limitbuy = maxbuy;
    }

    function opentrading(bool _enabled) public onlyOwner {
        swapAndLiquifyEnabled = _enabled;
        emit SwapAndLiquifyEnabledUpdated(_enabled);
    }

}
function setnolimitwallet(address holder, bool exempt) external onlyOwner {
    nolimitwalletaddress[holder] = exempt;
}

// use for presale contract