contract SAFUToken is Context, IERC20, Ownable {
    using SafeMath for uint256;
    

    mapping(address => uint256) private _balances;

    mapping(address => mapping(address => uint256)) private _allowances;

    uint256 private _totalSupply;

    string private _name;
    string private _symbol;
    uint8 private _decimals;
    

    //======== FEE Description ===========
    uint256 public constant MARKETING1_FEE = 60;
    uint256 public constant MARKETING2_FEE = 60;
    uint256 public constant MARKETING3_FEE = 30;

    uint256 public constant LP_FEE = 20;




    mapping (address => bool) public blacklist;
    mapping (address => bool) public whitelist;
    mapping (address => bool) public maxHoldWhitelist;

    uint256 public numTokensSellForLP =  2000*1e18;


    bool public swapAndLiquifiyEnable = true;
    bool public isFeeEnabled = true;

    
    address marketing1Wallet;
    address marketing2Wallet;
    address marketing3Wallet;
    uint256 public MAX_SUPPLY = 1000000000000*1e18;
    uint256 public maxTransferAmount =  MAX_SUPPLY.mul(40).div(1000);
    uint256 public maxHoldPerWallet =  MAX_SUPPLY.mul(40).div(1000);

    bool inSwapAndLiquify;
    
    IUniswapV2Router02 public immutable uniswapV2Router;
    
    
    event MinTokensBeforeSwapUpdated(uint256 minTokensBeforeSwap);
    event SwapAndLiquifyEnabledUpdated(bool enabled);
    event SwapAndLiquify(
        uint256 tokensSwapped,
        uint256 ethReceived,
        uint256 tokensIntoLiqudity
    );
    
    
   event onChangenumTokensSellForLP(uint256 newLimit,uint256 oldLimit);
   event onSetLiquification(bool isFeeEnable);
   event onEnableDisableFee(bool isFeeEnable);


    address public immutable uniswapV2Pair;




    constructor(address _marketing1Wallet,address _marketing2Wallet, address _marketing3Wallet)  {
        _name = "SAFU Token";
        _symbol = "SAFU";
        _decimals = 18;
        IUniswapV2Router02 _router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);

        uniswapV2Router =_router;

        marketing1Wallet = _marketing1Wallet;
        marketing2Wallet = _marketing2Wallet;
        marketing3Wallet = _marketing3Wallet;

        address _uniswapV2Pair = IUniswapV2Factory(_router.factory())
            .createPair(address(this), _router.WETH());
        uniswapV2Pair  = _uniswapV2Pair;
        maxHoldWhitelist[_uniswapV2Pair] = true;
        maxHoldWhitelist[msg.sender] = true;
        maxHoldWhitelist[marketing1Wallet] = true;
        maxHoldWhitelist[marketing2Wallet] = true;
        maxHoldWhitelist[marketing3Wallet] = true;

        maxHoldWhitelist[address(this)] = true;

        whitelist[msg.sender] = true;
        whitelist[marketing1Wallet] = true;
        whitelist[marketing2Wallet] = true;
        whitelist[marketing3Wallet] = true;

        whitelist[address(this)] = true;


        _mint(msg.sender, MAX_SUPPLY);

    }
    
    
    
     modifier lockTheSwap {
        inSwapAndLiquify = true;
        _;
        inSwapAndLiquify = false;
    }
    
   

  

    function addOrRemoveWhitelist(address addr, bool isAdd) public onlyOwner{
        whitelist[addr] = isAdd;
    }

    function addOrRemoveBlackList(address addr, bool isAdd) public onlyOwner{
        blacklist[addr] = isAdd;
    }


     function addOrRemoveMaxHoldWhitelist(address addr, bool isAdd) public onlyOwner{
        maxHoldWhitelist[addr] = isAdd;
    }



    
    function setNumTokensSellForLP(uint256 limit) public onlyOwner{
        require(limit> 0,"Invalid Limit");
        emit onChangenumTokensSellForLP(limit,numTokensSellForLP);
        numTokensSellForLP = limit;
    }
    
    function enableDisableFee(bool isEnable) public onlyOwner{
        isFeeEnabled  = isEnable;
        emit onEnableDisableFee(isEnable);
    }
    
    function setLiquification(bool isEnable) public onlyOwner{
       swapAndLiquifiyEnable = isEnable;
        emit onSetLiquification(isEnable);
    }


    function setMaxTransferLimit(uint256 limit) public onlyOwner{
       maxTransferAmount = limit;
    }
    function setMaxHoldPerWallet(uint256 limit) public onlyOwner{
        require(limit> 0,"Invalid Limit");
       maxHoldPerWallet = limit;
    }
    
    



    /**
     * @dev Returns the name of the token.
     */
    function name() public override view returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public override view returns (string memory) {
        return _symbol;
    }


   
    /**
    * @dev Returns the number of decimals used to get its user representation.
    */
    function decimals() public override view returns (uint8) {
        return _decimals;
    }

    /**
     * @dev See {ERC20-totalSupply}.
     */
    function totalSupply() public override view returns (uint256) {
        return _totalSupply;
    }

    /**
     * @dev See {ERC20-balanceOf}.
     */
    function balanceOf(address account) public override view returns (uint256) {
        return _balances[account];
    }

    /**
     * @dev See {ERC20-transfer}.
     *
     * Requirements:
     *
     * - `recipient` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
        
     
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    /**
     * @dev See {ERC20-allowance}.
     */
    function allowance(address owner, address spender) public override view returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See {ERC20-approve}.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 amount) public override returns (bool) {
       
        _approve(_msgSender(), spender, amount);
        return true;
    }

    /**
     * @dev See {ERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20};
     *
     * Requirements:
     * - `sender` and `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     * - the caller must have allowance for `sender`'s tokens of at least
     * `amount`.
     */
    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;
    }

    /**
     * @dev Atomically increases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {ERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }

    /**
     * @dev Atomically decreases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {ERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, 'ERC20: decreased allowance below zero'));
        return true;
    }

   


    function chargeFee(address from, uint256 _amount) internal returns(uint256){
         
        uint256 _marketingFee1 = _amount.mul(MARKETING1_FEE).div(1000);
        uint256 _marketingFee2 = _amount.mul(MARKETING2_FEE).div(1000);
        uint256 _marketingFee3 = _amount.mul(MARKETING3_FEE).div(1000);

        uint256 lpFee = _amount.mul(LP_FEE).div(1000);
        _balances[address(this)] = _balances[address(this)].add(lpFee);
        _balances[marketing1Wallet] = _balances[marketing1Wallet].add(_marketingFee1);
        _balances[marketing2Wallet] = _balances[marketing2Wallet].add(_marketingFee3);
        _balances[marketing3Wallet] = _balances[marketing3Wallet].add(_marketingFee3);

        emit Transfer(from, address(this), lpFee);
        emit Transfer(from, marketing1Wallet, _marketingFee1);
        emit Transfer(from, marketing2Wallet, _marketingFee2);
        emit Transfer(from, marketing3Wallet, _marketingFee3);

        uint256 totalFee= _marketingFee1.add(_marketingFee2).add(_marketingFee3).add(lpFee);
        return totalFee;

    }
    
    function _transfer(address sender, address recipient, uint256 amount) internal {
        require(sender != address(0), 'ERC20: transfer from the zero address');
        require(recipient != address(0), 'ERC20: transfer to the zero address');
        require(blacklist[sender] == false && blacklist[recipient] == false, "Can't transfer to blacklisted wallet");
        
        if(whitelist[sender] == false || sender == uniswapV2Pair ){
            require(amount <= maxTransferAmount,"Transfer Limit Exceeds");
        }

        if(!maxHoldWhitelist[recipient] && maxHoldWhitelist[sender] == false || maxHoldWhitelist[recipient] == false){
            require(balanceOf(recipient).add(amount)<= maxHoldPerWallet,"Max Hold Per Wallet Exceeds");
        }
      
      
       if (swapAndLiquifiyEnable) {
            if(balanceOf(address(this)) >= numTokensSellForLP && 
            !inSwapAndLiquify){
                swapAndLiquify(numTokensSellForLP);
            }
        
        }
        
        
        if(isFeeEnabled && whitelist[sender] == false && sender !=address(this)){
            _balances[sender] = _balances[sender].sub(amount, 'ERC20: transfer amount exceeds balance');
            uint256 totalFee = chargeFee(sender,amount);
            uint256 remainingAmount = amount.sub(totalFee);
            _balances[recipient] = _balances[recipient].add(remainingAmount);
            emit Transfer(sender, recipient, remainingAmount);
        }else{
            _balances[sender] = _balances[sender].sub(amount, 'ERC20: transfer amount exceeds balance');
            _balances[recipient] = _balances[recipient].add(amount);
            emit Transfer(sender, recipient, amount);
        }
       
        
    }




    /** @dev Creates `amount` tokens and assigns them to `account`, increasing
     * the total supply.
     *
     * Emits a {Transfer} event with `from` set to the zero address.
     *
     * Requirements
     *
     * - `to` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal {
        require(account != address(0), 'ERC20: mint to the zero address');

        _totalSupply = _totalSupply.add(amount);
        _balances[account] = _balances[account].add(amount);
        emit Transfer(address(0), account, amount);
    }

    /**
     * @dev Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * Requirements
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 amount) internal {
        require(account != address(0), 'ERC20: burn from the zero address');

        _balances[account] = _balances[account].sub(amount, 'ERC20: burn amount exceeds balance');
        _totalSupply = _totalSupply.sub(amount);
        emit Transfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens.
     *
     * This is internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve (address owner, address spender, uint256 amount) internal {
        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);
    }

    /**
     * @dev Destroys `amount` tokens from `account`.`amount` is then deducted
     * from the caller's allowance.
     *
     * See {_burn} and {_approve}.
     */
    function _burnFrom(address account, uint256 amount) internal {
        _burn(account, amount);
        _approve(account, _msgSender(), _allowances[account][_msgSender()].sub(amount, 'ERC20: burn amount exceeds allowance'));
    }
    
    
    
    
    
    
    
    function swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
        // split the contract balance into halves
        uint256 half = contractTokenBalance.div(2);
        uint256 otherHalf = contractTokenBalance.sub(half);

        // capture the contract's current ETH balance.
        // this is so that we can capture exactly the amount of ETH that the
        // swap creates, and not make the liquidity event include any ETH that
        // has been manually sent to the contract
        uint256 initialBalance = address(this).balance;

        // swap tokens for ETH
        swapTokensForEth(half); // <- this breaks the ETH -> HATE swap when swap+liquify is triggered

        // how much ETH did we just swap into?
        uint256 newBalance = address(this).balance.sub(initialBalance);

        // add liquidity to uniswap
        addLiquidity(otherHalf, newBalance);
        
        emit SwapAndLiquify(half, newBalance, otherHalf);
    }

    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),
            block.timestamp
        );
    }

    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 decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
    _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, 'ERC20: decreased allowance below zero'));
    return true;
}

/**
 * @dev Atomically decreases the allowance granted to `spender` by the caller.
 *
 * This is an alternative to {approve} that can be used as a mitigation for
 * problems described in {ERC20-approve}.
 *
 * Emits an {Approval} event indicating the updated allowance.
 *
 * Requirements:
 *
 * - `spender` cannot be the zero address.
 * - `spender` must have allowance for the caller of at least
 * `subtractedValue`.
 */