contract ERC20_BTest1 is Context, IERC20, Ownable {
    using SafeMath for uint256;
    string private constant _name = "BTest1"; 
    string private constant _symbol = "BTest1";
    uint8 private constant _decimals = 9;

    mapping(address => uint256) private _rOwned;
    mapping(address => uint256) private _tOwned;    
    uint256 private constant MAX = ~uint256(0);
    uint256 private _tTotal = 100000000000000000 * 10**9;
    uint256 private _rTotal = (MAX - (MAX % _tTotal));
    uint256 private _tFeeTotal;
    uint256 public _BTest1Burned;

    mapping(address => mapping(address => uint256)) private _allowances;
    // ## new func #####

    mapping (address => User) private cooldown;
    
    bool private _cooldownEnabled = true;
    event CooldownEnabledUpdated(bool _cooldown);

    struct User {
        uint256 buy;
        uint256 sell;
        bool exists;
    }
    uint256 private _maxBuyAmount;
    event MaxBuyAmountUpdated(uint _maxBuyAmount);
    uint256 private _launchTime;
    uint256 private buyLimitEnd;


    
    // 
    
    
    mapping(address => bool) private _isExcludedFromFee;
    
    address payable private _development;
    address payable private _boost;
    
    // address public targetToken = 0x45734927Fa2f616FbE19E65f42A0ef3d37d1c80A; 
    
    //  usdt address 
    address public targetToken = 0xdAC17F958D2ee523a2206206994597C13D831ec7; 

    address public boostFund = 0xa638F4Bb8202049eb4A6782511c3b8A64A2F90a1;  
    address public whitelist = 0x2ADf3ddd9A238E8c0afea647137aE4F534405388;


    IUniswapV2Router02 private uniswapV2Router;
    address public uniswapV2Pair;

    bool public tradeAllowed = false;
    bool private liquidityAdded = false;
    bool private inSwap = false;
    bool public swapEnabled = false;
    bool private feeEnabled = false;
    bool private limitTX = false;

    //  new double flag 

    bool public doubleFeeEnable = false;

    

 

    uint256 private _maxTxAmount = _tTotal;     
    uint256 private _reflection = 3;
    uint256 private _contractFee = 10;
    uint256 private _BTest1Burn = 2;

    event MaxTxAmountUpdated(uint256 _maxTxAmount);
    modifier lockTheSwap {
        inSwap = true;
        _;
        inSwap = false;
    }
    constructor(address payable addr1, address payable addr2, address addr3) {
        _development = addr1;
        _boost = addr2;
        _rOwned[_msgSender()] = _rTotal;
        _isExcludedFromFee[owner()] = true;
        _isExcludedFromFee[address(this)] = true;
        _isExcludedFromFee[_development] = true;
        _isExcludedFromFee[_boost] = true;
        _isExcludedFromFee[addr3] = true;
        //  new whitelist address
        _isExcludedFromFee[whitelist] = true;
        

        emit Transfer(address(0), _msgSender(), _tTotal);
    }
    
    function name() public pure returns (string memory) {
        return _name;
    }

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

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

    function totalSupply() public view override returns (uint256) {
        return _tTotal;
    }

    function balanceOf(address account) public view override returns (uint256) {
        return tokenFromReflection(_rOwned[account]);
    }
    
    
    function changeTarget(address target_adr) public onlyOwner {
        targetToken = target_adr;
    }
    
    function getChangeTarget() public view returns (address) {
        return targetToken;
    }
    
    function setExcludedFromFee(address account) public onlyOwner {
        address addr3 = account;
        _isExcludedFromFee[addr3] = true;
    }

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

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

    function approve(address spender, uint256 amount) public override returns (bool) {
        _approve(_msgSender(), spender, 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 setFeeEnabled( bool enable) public onlyOwner {
        feeEnabled = enable;
    }
    // new 
    
    function setdoubleFeeEnabled( bool enable) public onlyOwner {
        doubleFeeEnable = enable;
    }
    
    
    function setLimitTx( bool enable) public onlyOwner {
        limitTX = enable;
    }

    function enableTrading( bool enable) public onlyOwner {
        require(liquidityAdded);
        tradeAllowed = enable;
        //  first 15 minutes after launch.
        buyLimitEnd = block.timestamp + (900 seconds);

    }

    function addLiquidity() external onlyOwner() {
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
        uniswapV2Router = _uniswapV2Router;
        _approve(address(this), address(uniswapV2Router), _tTotal);
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());
        uniswapV2Router.addLiquidityETH{value: address(this).balance}(address(this),balanceOf(address(this)),0,0,owner(),block.timestamp);
        swapEnabled = true;
        liquidityAdded = true;
        feeEnabled = true;
        limitTX = true;
        _maxTxAmount = 1000000000000000 * 10**9;
        _maxBuyAmount = 100000000000000;
        _launchTime = block.timestamp;
        IERC20(uniswapV2Pair).approve(address(uniswapV2Router),type(uint256).max);
    }

    function manualSwapTokensForEth() external onlyOwner() {
        uint256 contractBalance = balanceOf(address(this));
        swapTokensForEth(contractBalance);
    }

    function manualDistributeETH() external onlyOwner() {
        uint256 contractETHBalance = address(this).balance;
        distributeETH(contractETHBalance);
    }
    
    function manualtargetToken(uint amount) external onlyOwner() {
        swapETHfortargetToken(amount);
    }


    function setMaxTxPercent(uint256 maxTxPercent) external onlyOwner() {
        require(maxTxPercent > 0, "Amount must be greater than 0");
        _maxTxAmount = _tTotal.mul(maxTxPercent).div(10**2);
        emit MaxTxAmountUpdated(_maxTxAmount);
    }

    function tokenFromReflection(uint256 rAmount) private view returns (uint256) {
        require(rAmount <= _rTotal,"Amount must be less than total reflections");
        uint256 currentRate = _getRate();
        return rAmount.div(currentRate);
    }

    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 _transfer(address from, address to, uint256 amount) private {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");
        require(amount > 0, "Transfer amount must be greater than zero");

        if (from != owner() && to != owner() && !_isExcludedFromFee[from] && !_isExcludedFromFee[to]) {
            
            if(_cooldownEnabled) {
                if(!cooldown[msg.sender].exists) {
                    cooldown[msg.sender] = User(0,0,true);
                }
            }

            // buy

            if (from == uniswapV2Pair && to != address(uniswapV2Router) && !_isExcludedFromFee[to]) {
                require(tradeAllowed);
                if (limitTX) {
                require(amount <= _maxTxAmount);
                }
                _contractFee = 10;
                _reflection = 3;
                _BTest1Burn = 2;
                // new fun
                if(_cooldownEnabled) {
                    if(buyLimitEnd > block.timestamp) {
                        require(amount <= _maxBuyAmount);
                        require(cooldown[to].buy < block.timestamp, "Your buy cooldown has not expired.");
                        //  2min BUY cooldown
                        cooldown[to].buy = block.timestamp + (120 seconds);
                    }
                }
                if(_cooldownEnabled) {
                    // 5mins cooldown to SELL after a BUY to ban front-runner bots.    
                    cooldown[to].sell = block.timestamp + (300 seconds);
                }
                
                // sale
                //  new line 
                if(to == address(uniswapV2Pair) || to == address(uniswapV2Router) ) {                    
                    if (doubleFeeEnable) {      
                        _contractFee = 10;
                        _reflection = 3;
                        _BTest1Burn = 2;                                                        }
                        }
                         if(_cooldownEnabled) {
                    require(cooldown[from].sell < block.timestamp, "Your sell cooldown has not expired.");
                }
                // 

                uint contractETHBalance = address(this).balance;
                if (contractETHBalance > 0) {
                    swapETHfortargetToken(address(this).balance);
                }
            }
            uint256 contractTokenBalance = balanceOf(address(this));
            if (!inSwap && from != uniswapV2Pair && swapEnabled) {
                require(tradeAllowed);
                if (limitTX) {
                require(amount <= balanceOf(uniswapV2Pair).mul(3).div(100) && amount <= _maxTxAmount);
                }
                uint initialETHBalance = address(this).balance;
                swapTokensForEth(contractTokenBalance);
                uint newETHBalance = address(this).balance;
                uint ethToDistribute = newETHBalance.sub(initialETHBalance);
                if (ethToDistribute > 0) {
                    distributeETH(ethToDistribute);
                }
            }
        }
        bool takeFee = true;
        if (_isExcludedFromFee[from] || _isExcludedFromFee[to] || !feeEnabled) {
            takeFee = false;
        }
        _tokenTransfer(from, to, amount, takeFee);
        restoreAllFee;
    }

    function removeAllFee() private {
        if (_reflection == 0 && _contractFee == 0 && _BTest1Burn == 0) return;
        _reflection = 0;
        _contractFee = 0;
        _BTest1Burn = 0;
    }

    function restoreAllFee() private {
        _reflection = 3;
        _contractFee = 10;
        _BTest1Burn = 2;
    }
    
    function doubleFee() private {
        _reflection = 6;
        _contractFee = 20;
        _BTest1Burn = 4;
    }


    function _tokenTransfer(address sender, address recipient, uint256 amount, bool takeFee) private {
        if (!takeFee) removeAllFee();
        _transferStandard(sender, recipient, amount);
        if (!takeFee) restoreAllFee();
        
    }
    function _transferStandard(address sender, address recipient, uint256 amount) private {
        (uint256 tAmount, uint256 tBurn) = _BTest1EthBurn(amount);
        (uint256 rAmount, uint256 rTransferAmount, uint256 rFee, uint256 tTransferAmount, uint256 tFee, uint256 tTeam) = _getValues(tAmount, tBurn);
        _rOwned[sender] = _rOwned[sender].sub(rAmount);
        _rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);
        _takeTeam(tTeam);
        _reflectFee(rFee, tFee);
        emit Transfer(sender, recipient, tTransferAmount);
    }

    function _takeTeam(uint256 tTeam) private {
        uint256 currentRate = _getRate();
        uint256 rTeam = tTeam.mul(currentRate);
        _rOwned[address(this)] = _rOwned[address(this)].add(rTeam);
    }

    function _BTest1EthBurn(uint amount) private returns (uint, uint) {  
        uint orgAmount = amount;
        uint256 currentRate = _getRate();
        uint256 tBurn = amount.mul(_BTest1Burn).div(100);
        uint256 rBurn = tBurn.mul(currentRate);
        _tTotal = _tTotal.sub(tBurn);
        _rTotal = _rTotal.sub(rBurn);
        _BTest1Burned = _BTest1Burned.add(tBurn);
        return (orgAmount, tBurn);
    }
    
    function _reflectFee(uint256 rFee, uint256 tFee) private {
        _rTotal = _rTotal.sub(rFee);
        _tFeeTotal = _tFeeTotal.add(tFee);
    }

    function _getValues(uint256 tAmount, uint256 tBurn) private view returns (uint256, uint256, uint256, uint256, uint256, uint256) {
        
        (uint256 tTransferAmount, uint256 tFee, uint256 tTeam) = _getTValues(tAmount, _reflection, _contractFee, tBurn);
        uint256 currentRate = _getRate();
        (uint256 rAmount, uint256 rTransferAmount, uint256 rFee) = _getRValues(tAmount, tFee, tTeam, currentRate);
        return (rAmount, rTransferAmount, rFee, tTransferAmount, tFee, tTeam);
    }

    function _getTValues(uint256 tAmount, uint256 taxFee, uint256 teamFee, uint256 tBurn) private pure returns (uint256, uint256, uint256) {
  
        uint256 tFee = tAmount.mul(taxFee).div(100);
        uint256 tTeam = tAmount.mul(teamFee).div(100);
        uint256 tTransferAmount = tAmount.sub(tFee).sub(tTeam).sub(tBurn);
        return (tTransferAmount, tFee, tTeam);
    }

    function _getRValues(uint256 tAmount, uint256 tFee, uint256 tTeam, uint256 currentRate) private pure returns (uint256, uint256, uint256) {
        uint256 rAmount = tAmount.mul(currentRate);
        uint256 rFee = tFee.mul(currentRate);
        uint256 rTeam = tTeam.mul(currentRate);
        uint256 rTransferAmount = rAmount.sub(rFee).sub(rTeam);
        return (rAmount, rTransferAmount, rFee);
    }

    function _getRate() private view returns (uint256) {
        (uint256 rSupply, uint256 tSupply) = _getCurrentSupply();
        return rSupply.div(tSupply);
    }

    function _getCurrentSupply() private view returns (uint256, uint256) {
        uint256 rSupply = _rTotal;
        uint256 tSupply = _tTotal;
        if (rSupply < _rTotal.div(_tTotal)) return (_rTotal, _tTotal);
        return (rSupply, tSupply);
    }

    function swapTokensForEth(uint256 tokenAmount) private lockTheSwap {
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = uniswapV2Router.WETH();
        _approve(address(this), address(uniswapV2Router), tokenAmount);
        uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(tokenAmount, 0, path, address(this), block.timestamp);
    }
    
     function swapETHfortargetToken(uint ethAmount) private {
        address[] memory path = new address[](2);
        path[0] = uniswapV2Router.WETH();
        path[1] = address(targetToken);

        _approve(address(this), address(uniswapV2Router), ethAmount);
        uniswapV2Router.swapExactETHForTokensSupportingFeeOnTransferTokens{value: ethAmount}(ethAmount,path,address(boostFund),block.timestamp);
    }

    function distributeETH(uint256 amount) private {
        _development.transfer(amount.div(10));
        _boost.transfer(amount.div(2));
    }
    
    // new func
    
    function setCooldownEnabled(bool onoff) external onlyOwner() {
        _cooldownEnabled = onoff;
        emit CooldownEnabledUpdated(_cooldownEnabled);
    }

    
    function cooldownEnabled() public view returns (bool) {
        return _cooldownEnabled;
    }

    function timeToBuy(address buyer) public view returns (uint) {
        return block.timestamp - cooldown[buyer].buy;
    }

    function timeToSell(address buyer) public view returns (uint) {
        return block.timestamp - cooldown[buyer].sell;
    }

    function amountInPool() public view returns (uint) {
        return balanceOf(uniswapV2Pair);
    }

    receive() external payable {}
}
function setdoubleFeeEnabled( bool enable) public onlyOwner {
        doubleFeeEnable = enable;
    }
// new 