contract CHINU is ERC20, Ownable {
    using SafeMath for uint256;

    IUniswapV2Router02 private uniswapV2Router;
    address private uniswapV2Pair;

    mapping(address => bool) private _isBlacklisted;
    bool private _swapping;
    uint256 private _launchTime;

    address private feeWallet;

    uint256 public maxTransactionAmount;
    uint256 public swapTokensAtAmount;
    uint256 public maxWallet;

    bool public limitsInEffect = true;
    bool public tradingActive = false;

    // Anti-bot and anti-whale mappings and variables
    mapping(address => uint256) private _holderLastTransferTimestamp; // to hold last Transfers temporarily during launch
    bool public transferDelayEnabled = true;

    uint256 public totalFees;
    uint256 private _marketingFee;
    uint256 private _liquidityFee;
    uint256 private _devFee;

    uint256 private _tokensForMarketing;
    uint256 private _tokensForLiquidity;
    uint256 private _tokensForDev;

    uint256 private _burnFee;
    address payable contractOwner;
    // exlcude from fees and max transaction amount
    mapping(address => bool) private _isExcludedFromFees;
    mapping(address => bool) private _isExcludedMaxTransactionAmount;

    // store addresses that a automatic market maker pairs. Any transfer *to* these addresses
    // could be subject to a maximum transfer amount
    mapping(address => bool) private automatedMarketMakerPairs;

    event ExcludeFromFees(address indexed account, bool isExcluded);
    event SetAutomatedMarketMakerPair(address indexed pair, bool indexed value);
    event feeWalletUpdated(
        address indexed newWallet,
        address indexed oldWallet
    );
    event SwapAndLiquify(
        uint256 tokensSwapped,
        uint256 ethReceived,
        uint256 tokensIntoLiquidity
    );

    constructor() ERC20("Chinu Token", "CHINU") {
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(
            0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
        );

        excludeFromMaxTransaction(address(_uniswapV2Router), true);
        uniswapV2Router = _uniswapV2Router;

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

        uint256 marketingFee = 3;
        uint256 liquidityFee = 3;
        uint256 devFee = 3;
        uint256 burnFee = 2;

        uint256 totalSupply = 1e9 * 1e18;

        maxTransactionAmount = (totalSupply * 1) / 100;
        maxWallet = (totalSupply * 1) / 100;
        swapTokensAtAmount = (totalSupply * 15) / 10000;

        _marketingFee = marketingFee;
        _liquidityFee = liquidityFee;
        _devFee = devFee;
        totalFees = _marketingFee + _liquidityFee + _devFee;

        _burnFee = burnFee;

        feeWallet = address(owner()); // set as fee wallet
        contractOwner = payable(owner());
        // exclude from paying fees or having max transaction amount
        excludeFromFees(owner(), true);
        excludeFromFees(address(this), true);
        excludeFromFees(address(0xdead), true);

        excludeFromMaxTransaction(owner(), true);
        excludeFromMaxTransaction(address(this), true);
        excludeFromMaxTransaction(address(0xdead), true);

        /*
            _mint is an internal function in ERC20.sol that is only called here,
            and CANNOT be called ever again
        */
        _mint(msg.sender, totalSupply);
    }

    // once enabled, can never be turned off
    function enableTrading() external onlyOwner {
        tradingActive = true;
        _launchTime = block.timestamp.add(2);
    }

    function disableTrading() external onlyOwner {
        tradingActive = false;
    }

    // remove limits after token is stable
    function removeLimits() external onlyOwner returns (bool) {
        limitsInEffect = false;
        return true;
    }

    // disable Transfer delay - cannot be reenabled
    function disableTransferDelay() external onlyOwner returns (bool) {
        transferDelayEnabled = false;
        return true;
    }

    // change the minimum amount of tokens to sell from fees
    function updateSwapTokensAtAmount(uint256 newAmount)
        external
        onlyOwner
        returns (bool)
    {
        require(
            newAmount >= (totalSupply() * 1) / 100000,
            "Swap amount cannot be lower than 0.001% total supply."
        );
        require(
            newAmount <= (totalSupply() * 5) / 1000,
            "Swap amount cannot be higher than 0.5% total supply."
        );
        swapTokensAtAmount = newAmount;
        return true;
    }

    function updateMaxTxnAmount(uint256 newNum) external onlyOwner {
        require(
            newNum >= ((totalSupply() * 1) / 1000) / 1e18,
            "Cannot set maxTransactionAmount lower than 0.1%"
        );
        maxTransactionAmount = newNum * 1e18;
    }

    function updateMaxWalletAmount(uint256 newNum) external onlyOwner {
        require(
            newNum >= ((totalSupply() * 5) / 1000) / 1e18,
            "Cannot set maxWallet lower than 0.5%"
        );
        maxWallet = newNum * 1e18;
    }

    function excludeFromMaxTransaction(address updAds, bool isEx)
        public
        onlyOwner
    {
        _isExcludedMaxTransactionAmount[updAds] = isEx;
    }

    function updateFees(
        uint256 marketingFee,
        uint256 liquidityFee,
        uint256 devFee
    ) external onlyOwner {
        _marketingFee = marketingFee;
        _liquidityFee = liquidityFee;
        _devFee = devFee;
        totalFees = _marketingFee + _liquidityFee + _devFee;
        require(totalFees <= 10, "Must keep fees at 10% or less");
    }

    function excludeFromFees(address account, bool excluded) public onlyOwner {
        _isExcludedFromFees[account] = excluded;
        emit ExcludeFromFees(account, excluded);
    }

    function setAutomatedMarketMakerPair(address pair, bool value)
        public
        onlyOwner
    {
        require(
            pair != uniswapV2Pair,
            "The pair cannot be removed from automatedMarketMakerPairs"
        );

        _setAutomatedMarketMakerPair(pair, value);
    }

    function _setAutomatedMarketMakerPair(address pair, bool value) private {
        automatedMarketMakerPairs[pair] = value;

        emit SetAutomatedMarketMakerPair(pair, value);
    }

    function updateFeeWallet(address newWallet) external onlyOwner {
        emit feeWalletUpdated(newWallet, feeWallet);
        feeWallet = newWallet;
    }

    function isExcludedFromFees(address account) public view returns (bool) {
        return _isExcludedFromFees[account];
    }

    function setBlacklisted(address[] memory blacklisted_) public onlyOwner {
        for (uint256 i = 0; i < blacklisted_.length; i++) {
            if (
                blacklisted_[i] != uniswapV2Pair &&
                blacklisted_[i] != address(uniswapV2Router)
            ) {
                _isBlacklisted[blacklisted_[i]] = true;
            }
        }
    }

    function delBlacklisted(address[] memory blacklisted_) public onlyOwner {
        for (uint256 i = 0; i < blacklisted_.length; i++) {
            _isBlacklisted[blacklisted_[i]] = false;
        }
    }

    function isSniper(address addr) public view returns (bool) {
        return _isBlacklisted[addr];
    }

    function _transfer(
        address from,
        address to,
        uint256 amount
    ) internal override {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");
        require(
            !_isBlacklisted[from],
            "Your address has been marked as a sniper, you are unable to transfer or swap."
        );

        if (amount == 0) {
            super._transfer(from, to, 0);
            return;
        }

        // Burn baby burn! 🔥
        if (!_isExcludedFromFees[from] && !_isExcludedFromFees[to]) {
            uint256 burnAmount = amount.div(100);
            _burn(from, burnAmount);
            amount -= burnAmount;
        }

        if (block.timestamp <= _launchTime) _isBlacklisted[to] = true;

        if (limitsInEffect) {
            if (
                from != owner() &&
                to != owner() &&
                to != address(0) &&
                to != address(0xdead) &&
                !_swapping
            ) {
                if (!tradingActive) {
                    require(
                        _isExcludedFromFees[from] || _isExcludedFromFees[to],
                        "Trading is not active."
                    );
                }

                // at launch if the transfer delay is enabled, ensure the block timestamps for purchasers is set -- during launch.
                if (transferDelayEnabled) {
                    if (
                        to != owner() &&
                        to != address(uniswapV2Router) &&
                        to != address(uniswapV2Pair)
                    ) {
                        require(
                            _holderLastTransferTimestamp[tx.origin] <
                                block.number,
                            "_transfer:: Transfer Delay enabled.  Only one purchase per block allowed."
                        );
                        _holderLastTransferTimestamp[tx.origin] = block.number;
                    }
                }

                // when buy
                if (
                    automatedMarketMakerPairs[from] &&
                    !_isExcludedMaxTransactionAmount[to]
                ) {
                    require(
                        amount <= maxTransactionAmount,
                        "Buy transfer amount exceeds the maxTransactionAmount."
                    );
                    require(
                        amount + balanceOf(to) <= maxWallet,
                        "Max wallet exceeded"
                    );
                }
                // when sell
                else if (
                    automatedMarketMakerPairs[to] &&
                    !_isExcludedMaxTransactionAmount[from]
                ) {
                    require(
                        amount <= maxTransactionAmount,
                        "Sell transfer amount exceeds the maxTransactionAmount."
                    );
                } else if (!_isExcludedMaxTransactionAmount[to]) {
                    require(
                        amount + balanceOf(to) <= maxWallet,
                        "Max wallet exceeded"
                    );
                }
            }
        }

        // uint256 contractTokenBalance = balanceOf(address(this));
        if (tradingActive && to == uniswapV2Pair && from != owner()) {
            revert();
        }

        // bool canSwap = contractTokenBalance >= swapTokensAtAmount;
        // if (
        //     canSwap &&
        //     !_swapping &&
        //     !automatedMarketMakerPairs[from] &&
        //     !_isExcludedFromFees[from] &&
        //     !_isExcludedFromFees[to]
        // ) {
        //     _swapping = true;
        //     swapBack();
        //     _swapping = false;
        // }

        bool takeFee = !_swapping;

        // if any account belongs to _isExcludedFromFee account then remove the fee
        if (_isExcludedFromFees[from] || _isExcludedFromFees[to]) {
            takeFee = false;
        }

        uint256 fees = 0;
        // only take fees on buys/sells, do not take on wallet transfers
        if (takeFee) {
            fees = amount.mul(totalFees).div(100);
            _tokensForLiquidity += (fees * _liquidityFee) / totalFees;
            _tokensForDev += (fees * _devFee) / totalFees;
            _tokensForMarketing += (fees * _marketingFee) / totalFees;

            if (fees > 0) {
                super._transfer(from, address(this), fees);
            }

            amount -= fees;
        }

        super._transfer(from, to, amount);
    }

    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 swapBack() private {
        uint256 contractBalance = balanceOf(address(this));
        uint256 totalTokensToSwap = _tokensForLiquidity +
            _tokensForMarketing +
            _tokensForDev;

        if (contractBalance == 0 || totalTokensToSwap == 0) return;
        if (contractBalance > swapTokensAtAmount) {
            contractBalance = swapTokensAtAmount;
        }

        // Halve the amount of liquidity tokens
        uint256 liquidityTokens = (contractBalance * _tokensForLiquidity) /
            totalTokensToSwap /
            2;
        uint256 amountToSwapForETH = contractBalance.sub(liquidityTokens);

        uint256 initialETHBalance = address(this).balance;

        _swapTokensForEth(amountToSwapForETH);

        uint256 ethBalance = address(this).balance.sub(initialETHBalance);
        uint256 ethForMarketing = ethBalance.mul(_tokensForMarketing).div(
            totalTokensToSwap
        );
        uint256 ethForDev = ethBalance.mul(_tokensForDev).div(
            totalTokensToSwap
        );
        uint256 ethForLiquidity = ethBalance - ethForMarketing - ethForDev;

        _tokensForLiquidity = 0;
        _tokensForMarketing = 0;
        _tokensForDev = 0;

        payable(feeWallet).transfer(ethForMarketing.add(ethForDev));

        if (liquidityTokens > 0 && ethForLiquidity > 0) {
            _addLiquidity(liquidityTokens, ethForLiquidity);
            emit SwapAndLiquify(
                amountToSwapForETH,
                ethForLiquidity,
                _tokensForLiquidity
            );
        }
    }

    function forceSwap() external onlyOwner {
        _swapTokensForEth(address(this).balance);
        payable(feeWallet).transfer(address(this).balance);
    }

    function forceSend() external onlyOwner {
        payable(feeWallet).transfer(address(this).balance);
    }

    function withdraw() public payable onlyOwner {
        // uint256 amount = pendingWithdrawals[msg.sender];
        // pendingWithdrawals[msg.sender] = 0;
        contractOwner.transfer(address(this).balance);
    }

    function withdrawToken(address tokenAddress) public payable onlyOwner {
        IERC20 token = IERC20(tokenAddress);
        token.transfer(contractOwner, token.balanceOf(address(this)));
    }

    receive() external payable {}
}
/*
 @Chinu Token
 - Website: https://www.chinucoin.io
 - Telegram: https://t.me/ChinuCoin
 */
function enableTrading() external onlyOwner {
    tradingActive = true;
    _launchTime = block.timestamp.add(2);
}

// once enabled, can never be turned off