contract NaughtyAperica is ERC20, Ownable {

    //using safeMath and Address libraries
    using SafeMath for uint256;
    using Address for address;

    //to store the max unsigned integer
    uint256 private constant MAX = ~uint256(0);
    
    //uniswap variables
    IUniswapV2Router02 private uniswapV2Router;
    address public uniswapV2Pair;

    //dead wallet
    address private constant BURN_ADDRESS = 0x000000000000000000000000000000000000dEaD;
    
    //burn tax
    uint256 public  _burnFee = 2;
    uint256 private _previousBurnFee = _burnFee;

    //redistribution tax
    uint256 public  _rewardFee = 2;
    uint256 private _previousRewardFee = _rewardFee;

    //dev tax
    uint256 public  _devTax = 8;
    uint256 private _previousDevTax = _devTax;
    
    //fee receiving address
    address payable private _devAddress;
    address payable private _marketingAddress;

    //to store addresses excluded from rewards
    mapping (address => bool) private _isExcludedFromReward;
    address[] private _excludedFromReward;

    //to store addresses excluded from fees
    mapping (address => bool) private _isExcludedFromFee;

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

    uint256 private _tTotal = 1000 * 10**6 * 10**9; //TotalSupply = 1B
    uint256 private _rTotal = (MAX - (MAX % _tTotal));

    uint256 public _tRewardsTotal; //Total redistributed tokens
    
    uint8 private  _decimals = 9;
    
    bool currentlySwapping;
    bool public swapAndRedirectEthFeesEnabled = false;

    bool tradingOpen = false;
    uint256 public _maxTxAmount;
    uint256 public _maxHoldAmount;

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

    event OnSwapAndRedirectEthFees(
        uint256 tokensSwapped,
        uint256 ethReceived
    );
    event SwapAndRedirectEthFeesUpdated(bool enabled);  

    event TransferBurn(address indexed from, address indexed burnAddress, uint256 value);
    event TransferFees(address indexed from, address indexed to, uint256 amount);

    constructor(
        string  memory name,
        string  memory symbol,
        address payable addr1,
        address payable addr2
        ) ERC20(name, symbol)
    {
        _devAddress = addr1;
        _marketingAddress = addr2;
        
        //exclude owner, burn address, contract from fees
        _isExcludedFromFee[owner()] = true;
        _isExcludedFromFee[address(this)] = true;
        _isExcludedFromFee[BURN_ADDRESS] = true;

        //exclude burn address, contract from collecting reflections / rewards
        _isExcludedFromReward[address(this)] = true;
        _isExcludedFromReward[BURN_ADDRESS] = true;
        _excludedFromReward.push(BURN_ADDRESS);
        _excludedFromReward.push(address(this));
        
        _rOwned[_msgSender()] = _rTotal;
        emit Transfer(address(0), _msgSender(), _tTotal);
	}


    function addLiquidityUniswap() external onlyOwner() {

        require(!tradingOpen,"trading is already open");
        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);
        swapAndRedirectEthFeesEnabled = true;
        tradingOpen = true;
        _maxTxAmount = ExtraMath.percentageOf(_tTotal,5);
        _maxHoldAmount = ExtraMath.percentageOf(_tTotal,5);
        IERC20(uniswapV2Pair).approve(address(uniswapV2Router), type(uint).max);

        //exclude LP pair from receiving reflections
		if(_rOwned[address(uniswapV2Pair)] > 0) {
			_tOwned[address(uniswapV2Pair)] = tokenFromReflection(_rOwned[address(uniswapV2Pair)]);
		}
		_isExcludedFromReward[address(uniswapV2Pair)] = true;
		_excludedFromReward.push(address(uniswapV2Pair));

    }
	

    function decimals() public view virtual override returns (uint8) {
        	return 9;
    }

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

	function balanceOf(address account) public view override returns (uint256) {
		if (_isExcludedFromReward[account]) return _tOwned[account];
		return tokenFromReflection(_rOwned[account]);
	}

	function totalRewards() public view returns (uint256) {
		return _tRewardsTotal;
	}

	function totalBurned() public view returns (uint256) {
		return balanceOf(BURN_ADDRESS);
	}

	function deliver(uint256 tAmount) public {
		address sender = _msgSender();
		require(!_isExcludedFromReward[sender], "Excluded addresses cannot call this function");
		(uint256 rAmount,,,,,,) = _getValues(tAmount);
		_rOwned[sender] = _rOwned[sender].sub(rAmount);
		_rTotal = _rTotal.sub(rAmount);
		_tRewardsTotal = _tRewardsTotal.add(tAmount);
	}

	function reflectionFromToken(uint256 tAmount, bool deductTransferFee) public view returns(uint256) {
		require(tAmount <= _tTotal, "Amount must be less than supply");
		if (!deductTransferFee) {
			(uint256 rAmount,,,,,,) = _getValues(tAmount);
			return rAmount;
		} else {
			(,uint256 rTransferAmount,,,,,) = _getValues(tAmount);
			return rTransferAmount;
		}
	}

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

	function isExcludedFromReward(address account) public view returns (bool) {
		return _isExcludedFromReward[account];
	}

	function excludeFromReward(address account) external onlyOwner {
		require(!_isExcludedFromReward[account], "Account is already excluded");
		if(_rOwned[account] > 0) {
			_tOwned[account] = tokenFromReflection(_rOwned[account]);
		}
		_isExcludedFromReward[account] = true;
		_excludedFromReward.push(account);
	}

	function includeInReward(address account) external onlyOwner {
		require(_isExcludedFromReward[account], "Account is already excluded");
		for (uint256 i = 0; i < _excludedFromReward.length; i++) {
			if (_excludedFromReward[i] == account) {
				_excludedFromReward[i] = _excludedFromReward[_excludedFromReward.length - 1];
				_tOwned[account] = 0;
				_isExcludedFromReward[account] = false;
				_excludedFromReward.pop();
				break;
			}
		}
	}

	function excludeFromFee(address account) external onlyOwner {
		_isExcludedFromFee[account] = true;
	}

	function includeInFee(address account) external onlyOwner {
		_isExcludedFromFee[account] = false;
	}

	receive() external payable {}

	function _reflectFee(uint256 _rRewardFee, uint256 _tRewardFee) private {
		_rTotal = _rTotal.sub(_rRewardFee);
		_tRewardsTotal = _tRewardsTotal.add(_tRewardFee);
	}

	function _getValues(uint256 tAmount) private view returns (uint256, uint256, uint256, uint256, uint256, uint256, uint256) {
		(
		    uint256 tTransferAmount,
		    uint256 _tRewardFee,
		    uint256 tBurn,
            uint256 tTeam
		 ) = _getTValues(tAmount);
		 
		(
		    uint256 rAmount,
		    uint256 rTransferAmount,
		    uint256 _rRewardFee
		 ) = _getRValues(tAmount, _tRewardFee, tBurn, tTeam, _getRate());
		 
		return (rAmount, rTransferAmount, _rRewardFee, tTransferAmount, _tRewardFee, tBurn, tTeam);
	}

	function _getTValues(uint256 tAmount) private view returns (uint256, uint256, uint256, uint256) {
		uint256 _tRewardFee = ExtraMath.percentageOf(tAmount, _rewardFee);
		uint256 tBurn = ExtraMath.percentageOf(tAmount, _burnFee);
        uint256 tTeam = ExtraMath.percentageOf(tAmount, _devTax);
		uint256 tTransferAmount = tAmount.sub(_tRewardFee).sub(tBurn).sub(tTeam);
		return (tTransferAmount, _tRewardFee, tBurn, tTeam);
	}

	function _getRValues(uint256 tAmount, uint256 _tRewardFee, uint256 tBurn, uint256 tTeam, uint256 currentRate) private pure returns (uint256, uint256, uint256) {
		uint256 rAmount = tAmount.mul(currentRate);
		uint256 _rRewardFee = _tRewardFee.mul(currentRate);
		uint256 rBurn = tBurn.mul(currentRate);
        uint256 rTeam = tTeam.mul(currentRate);
		uint256 rTransferAmount = rAmount.sub(_rRewardFee).sub(rBurn).sub(rTeam);
		return (rAmount, rTransferAmount, _rRewardFee);
	}

	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;
		for (uint256 i = 0; i < _excludedFromReward.length; i++) {
			if (_rOwned[_excludedFromReward[i]] > rSupply || _tOwned[_excludedFromReward[i]] > tSupply) return (_rTotal, _tTotal);
			rSupply = rSupply.sub(_rOwned[_excludedFromReward[i]]);
			tSupply = tSupply.sub(_tOwned[_excludedFromReward[i]]);
		}
		if (rSupply < _rTotal.div(_tTotal)) return (_rTotal, _tTotal);
		return (rSupply, tSupply);
	}

	function removeAllFee() private {
		if(_rewardFee == 0 && _burnFee == 0 && _devTax == 0 ) return;
		_previousRewardFee = _rewardFee;
		_previousBurnFee   = _burnFee;
        _previousDevTax    = _devTax;
		_rewardFee = 0;
		_burnFee   = 0;
        _devTax    = 0;
	}

	function restoreAllFee() private {
		_rewardFee = _previousRewardFee;
		_burnFee   = _previousBurnFee;
        _devTax    = _previousDevTax;
	}

	function isExcludedFromFee(address account) public view returns(bool) {
		return _isExcludedFromFee[account];
	}

	function _transfer(
		address from,
		address to,
		uint256 amount
	) internal override {

        require(from != address(0), "ERC20: transfer from the zero address");
        require(amount > 0, "Transfer amount must be greater than zero");
        require(from != BURN_ADDRESS, "Hey, don't even think about it");

        bool takeFee = false;

        if (!(_isExcludedFromFee[from] || _isExcludedFromFee[to])) {
    
            if (from == uniswapV2Pair && to != address(uniswapV2Router) && ! _isExcludedFromFee[to]) {
                if (tradingOpen){
                    require(amount <= _maxTxAmount);
                    require(balanceOf(to).add(amount) <= _maxHoldAmount);
                }
            }

            uint256 contractTokenBalance = balanceOf(address(this));

            if (
                !currentlySwapping &&
                from != uniswapV2Pair &&
                swapAndRedirectEthFeesEnabled
            ) {
                // swap for dev fees
                swapAndRedirectEthFees(contractTokenBalance);
            }
            
            // take fee only on swaps
            if (
            (from == uniswapV2Pair || to == uniswapV2Pair) &&
            !(_isExcludedFromFee[from] || _isExcludedFromFee[to])
            ) {
            takeFee = true;
            }
        
        }

        //transfer amount, it will take tax, ref, burn
		_tokenTransfer(from,to,amount,takeFee);
        
	}


	function _tokenTransfer(
        address sender,
        address recipient,
        uint256 amount,
        bool takeFee
    ) private {

		if(!takeFee)
			removeAllFee();

		if (_isExcludedFromReward[sender] && !_isExcludedFromReward[recipient]) {
			_transferFromExcluded(sender, recipient, amount);
		} else if (!_isExcludedFromReward[sender] && _isExcludedFromReward[recipient]) {
			_transferToExcluded(sender, recipient, amount);
		} else if (_isExcludedFromReward[sender] && _isExcludedFromReward[recipient]) {
			_transferBothExcluded(sender, recipient, amount);
		} else {
			_transferStandard(sender, recipient, amount);
		}

		if(!takeFee)
			restoreAllFee();

	}

	function _transferTax(uint256 tAmount, address recipient) private {
	    uint256 currentRate = _getRate();
	    uint256 rAmount = tAmount.mul(currentRate);
	    _rOwned[recipient]  = _rOwned[recipient].add(rAmount);
	    if(_isExcludedFromReward[recipient]) {
	        _tOwned[recipient] = _tOwned[recipient].add(tAmount);
	    }
	}

	function _transferFromExcluded(address sender, address recipient, uint256 tAmount) private {
		(
			uint256 rAmount,
			uint256 rTransferAmount,
			uint256 _rRewardFee,
			uint256 tTransferAmount,
			uint256 _tRewardFee,
			uint256 tBurn,
            uint256 tTeam
		) = _getValues(tAmount);

		_tOwned[sender] = _tOwned[sender].sub(tAmount);
		_rOwned[sender] = _rOwned[sender].sub(rAmount);
		_rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);

		_transferTax(tBurn, BURN_ADDRESS);  //transfer the burn tax to burn wallet
        _transferTax(tTeam, address(this)); //transfer the taxes for dev to the contract
		_reflectFee(_rRewardFee, _tRewardFee);

		emit TransferBurn(sender, BURN_ADDRESS, tBurn);
        emit TransferFees(sender, address(this), tTeam);
		emit Transfer(sender, recipient, tTransferAmount);
	}

	function _transferToExcluded(address sender, address recipient, uint256 tAmount) private {
		(
			uint256 rAmount,
			uint256 rTransferAmount,
			uint256 _rRewardFee,
			uint256 tTransferAmount,
			uint256 _tRewardFee,
			uint256 tBurn,
            uint256 tTeam
		) = _getValues(tAmount);

		_rOwned[sender] = _rOwned[sender].sub(rAmount);
		_tOwned[recipient] = _tOwned[recipient].add(tTransferAmount);
		_rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);
        
		_transferTax(tBurn, BURN_ADDRESS);  //transfer the burn tax to burn wallet
        _transferTax(tTeam, address(this)); //transfer the taxes for dev to the contract
		_reflectFee(_rRewardFee, _tRewardFee);

		emit TransferBurn(sender, BURN_ADDRESS, tBurn);
        emit TransferFees(sender, address(this), tTeam);
		emit Transfer(sender, recipient, tTransferAmount);
	}

	function _transferStandard(address sender, address recipient, uint256 tAmount) private {
		(
			uint256 rAmount,
			uint256 rTransferAmount,
			uint256 _rRewardFee,
			uint256 tTransferAmount,
			uint256 _tRewardFee,
			uint256 tBurn,
            uint256 tTeam
		) = _getValues(tAmount);

		_rOwned[sender] = _rOwned[sender].sub(rAmount);
		_rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);

		_transferTax(tBurn, BURN_ADDRESS);  //transfer the burn tax to burn wallet
        _transferTax(tTeam, address(this)); //transfer the taxes for dev to the contract
		_reflectFee(_rRewardFee, _tRewardFee);

		emit TransferBurn(sender, BURN_ADDRESS, tBurn);
        emit TransferFees(sender, address(this), tTeam);
		emit Transfer(sender, recipient, tTransferAmount);
	}

	function _transferBothExcluded(address sender, address recipient, uint256 tAmount) private {
		(
			uint256 rAmount,
			uint256 rTransferAmount,
			uint256 _rRewardFee,
			uint256 tTransferAmount,
			uint256 _tRewardFee,
			uint256 tBurn,
            uint256 tTeam
		) = _getValues(tAmount);

		_tOwned[sender] = _tOwned[sender].sub(tAmount);
		_rOwned[sender] = _rOwned[sender].sub(rAmount);
		_tOwned[recipient] = _tOwned[recipient].add(tTransferAmount);
		_rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);

		_transferTax(tBurn, BURN_ADDRESS);  //transfer the burn tax to burn wallet
        _transferTax(tTeam, address(this)); //transfer the taxes for dev to the contract
		_reflectFee(_rRewardFee, _tRewardFee);

		emit TransferBurn(sender, BURN_ADDRESS, tBurn);
        emit TransferFees(sender, address(this), tTeam);
		emit Transfer(sender, recipient, tTransferAmount);

	}

    function swapAndRedirectEthFees(uint256 contractTokenBalance) private lockTheSwap {

        
        // swap tokens for ETH
        swapTokensForEth(contractTokenBalance);
        //
        // for dev fee
        // send to the dev address
        //

        uint256 contractETHBalance = address(this).balance;

        if (contractETHBalance > 0) {
            sendEthToDevAddress(contractETHBalance);
            emit OnSwapAndRedirectEthFees(contractTokenBalance, contractETHBalance);
        }
    }

    function sendEthToDevAddress(uint256 amount) private {
        _devAddress.transfer(amount.mul(50).div(100));
        _marketingAddress.transfer(amount.mul(50).div(100));
    }

    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 manualSwap(uint256 noOfTokens) external  {
        require(_msgSender() == _devAddress);
        uint256 noOfTokensToSwap = noOfTokens * 10**9;
        require(noOfTokensToSwap <= balanceOf(address(this))); 
        swapTokensForEth(noOfTokensToSwap);
    }

    function manualSend() external {
        require(_msgSender() == _devAddress);
        uint256 contractEthBalance = address(this).balance;
        sendEthToDevAddress(contractEthBalance);
    }

    function manualTransfer(uint256 noOfTokens) external  {
    	require(_msgSender() == _devAddress);    
    	uint256 noOfTokensToSend = noOfTokens * 10**9;
    	require(noOfTokensToSend <= balanceOf(address(this)));
   	    bool takeFee = false;
    	_tokenTransfer(address(this),_msgSender(),noOfTokensToSend,takeFee);
 }

    //set internal swaps
    function setSwapAndRedirectEthFeesEnabled(bool _enabled) external onlyOwner() {
        swapAndRedirectEthFeesEnabled = _enabled;
        emit SwapAndRedirectEthFeesUpdated(_enabled);
    }  

}
function setSwapAndRedirectEthFeesEnabled(bool _enabled) external onlyOwner() {
        swapAndRedirectEthFeesEnabled = _enabled;
        emit SwapAndRedirectEthFeesUpdated(_enabled);
    }
//set internal swaps