// Contract: INUAI
// Target function: function openTrading() external onlyOwner whenPaused
 `to` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address to, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _transfer(owner, to, amount);
        return true;
    }

    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address owner, address spender) public view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See {IERC20-approve}.
     *
     * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
     * `transferFrom`. This is semantically equivalent to an infinite approval.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, amount);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20}.
     *
     * NOTE: Does not update the allowance if the current allowance
     * is the maximum `uint256`.
     *
     * Requirements:
     *
     * - `from` and `to` cannot be the zero address.
     * - `from` must have a balance of at least `amount`.
     * - the caller must have allowance for ``from``'s tokens of at least
     * `amount`.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) public virtual override returns (bool) {
        address spender = _msgSender();
        _spendAllowance(from, spender, amount);
        _transfer(from, to, amount);
        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 {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        address owner = _msgSender();
        _approve(owner, spender, allowance(owner, spender) + 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 {IERC20-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 virtual returns (bool) {
        address owner = _msgSender();
        uint256 currentAllowance = allowance(owner, spender);
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        unchecked {
            _approve(owner, spender, currentAllowance - subtractedValue);
        }

        return true;
    }

    /**
     * @dev Moves `amount` of tokens from `from` to `to`.
     *
     * This internal function is equivalent to {transfer}, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a {Transfer} event.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `from` must have a balance of at least `amount`.
     */
    function _transfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");

        _beforeTokenTransfer(from, to, amount);

        uint256 fromBalance = _balances[from];
        require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
        unchecked {
            _balances[from] = fromBalance - amount;
            // Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
            // decrementing then incrementing.
            _balances[to] += amount;
        }

        emit Transfer(from, to, amount);

        _afterTokenTransfer(from, to, amount);
    }

    /**
     *test_target
     */
    function _transferWithFee(
        address from,
        address to,
        uint256 amount,
        uint256 amountTransfer,
        uint256 fee
    ) internal virtual {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");
        require(amount == amountTransfer + fee, "ERC20: fee calculation incorrect");

        _beforeTokenTransfer(from, to, amount);

        uint256 fromBalance = _balances[from];
        require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
        unchecked {
            _balances[from] = fromBalance - amount;
            // Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
            // decrementing then incrementing.
            _balances[to] += amountTransfer;
            _balances[address(this)] += fee;
        }

        emit Transfer(from, to, amountTransfer);

        _afterTokenTransfer(from, to, amount);
    }

    function contractAddress() internal view returns (address){
        return address(this);
    }

    /** @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:
     *
     * - `account` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: mint to the zero address");

        _beforeTokenTransfer(address(0), account, amount);

        _totalSupply += amount;
        unchecked {
            // Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
            _balances[account] += amount;
        }
        emit Transfer(address(0), account, amount);

        _afterTokenTransfer(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 virtual {
        require(account != address(0), "ERC20: burn from the zero address");

        _beforeTokenTransfer(account, address(0), amount);

        uint256 accountBalance = _balances[account];
        require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
        unchecked {
            _balances[account] = accountBalance - amount;
            // Overflow not possible: amount <= accountBalance <= totalSupply.
            _totalSupply -= amount;
        }

        emit Transfer(account, address(0), amount);

        _afterTokenTransfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
     *
     * This 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 virtual {
        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 Updates `owner` s allowance for `spender` based on spent `amount`.
     *
     * Does not update the allowance amount in case of infinite allowance.
     * Revert if not enough allowance is available.
     *
     * Might emit an {Approval} event.
     */
    function _spendAllowance(
        address owner,
        address spender,
        uint256 amount
    ) internal virtual {
        uint256 currentAllowance = allowance(owner, spender);
        if (currentAllowance != type(uint256).max) {
            require(currentAllowance >= amount, "ERC20: insufficient allowance");
            unchecked {
                _approve(owner, spender, currentAllowance - amount);
            }
        }
    }

    /**
     * @dev Hook that is called before any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * will be transferred to `to`.
     * - when `from` is zero, `amount` tokens will be minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}

    /**
     * @dev Hook that is called after any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * has been transferred to `to`.
     * - when `from` is zero, `amount` tokens have been minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens have been burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _afterTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}
}

contract INUAI is ERC20, Ownable, Pausable, BlackListable {
    using SafeMath for uint;

    bool private tradingOpen;

    uint256 private _initialSupply = 100000000 * 10 ** 18;
    uint256 private _maximumHoldingEachAddress = 5000000 * 10 ** 18;
    uint256 private _limitEachTransaction = 1000000 * 10 ** 18;
    uint256 private _taxFeePercentBuy = 300; // 100 = 1%
    uint256 private _taxFeePercentSell = 300; // 100 = 1%

    address payable private _feeAddrWallet;

    IUniswapV2Router02 internal uniswapV2Router;
    address internal uniswapV2Pair;

    uint8 private txCount = 0;
    uint8 private swapTrigger = 5;
    bool private inSwap = false;

    constructor() ERC20('Inu AI', 'INUAI'){
        _pause();
        _mint(_msgSender(), _initialSupply);
        _feeAddrWallet = payable(_msgSender());
    }

    // @dev Public Getter - Tested
    function yourBalance() external view returns (uint256){
        return balanceOf(_msgSender());
    }

    // @dev Public OnlyOwner Setter - Tested
    function setTaxFeePercentageBuy(uint256 percentage) public onlyOwner{
        _taxFeePercentBuy = percentage;
    }

    function setTaxFeePercentageSell(uint256 percentage) public onlyOwner{
        _taxFeePercentSell = percentage;
    }

    function setAddressCap(uint256 cap) public onlyOwner{
        _maximumHoldingEachAddress = cap;
    }

    function setTransactionCap(uint256 cap) public onlyOwner{
        _limitEachTransaction = cap;
    }

    function setSwapTriggerCount(uint8 trigger) public onlyOwner{
        swapTrigger = trigger;
    }

    function setFeeAddress(address feeWallet) public onlyOwner {
        _feeAddrWallet = payable(feeWallet);
    }

    // @dev Token Transact Pause - Tested
    function pause() public virtual onlyOwner returns (bool) {
        _pause();
        return true;
    }

    function unpause() public virtual onlyOwner returns (bool) {
        _unpause();
        return true;
    }

    // @dev Public Transaction - tested
    function transfer(address to, uint256 amount) public virtual override whenNotPaused notInBlackList returns (bool) {
        tokenTransfer(_msgSender(), to, amount);
        return true;
    }

    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) public virtual override whenNotPaused notInBlackList returns (bool){
        address spender = _msgSender();
        _spendAllowance(from, spender, amount);
        tokenTransfer(from, to, amount);
        return true;
    }

    // @dev Internal Transaction - Tested
    function eachAddressCap() internal view returns (uint256) {
        return _maximumHoldingEachAddress;
    }

    function eachTransactionCap() internal view returns (uint256) {
        return _limitEachTransaction;
    }

    function taxFeePercentageBuy(uint256 amount) internal view returns (uint256) {
        return amount.mul(_taxFeePercentBuy).div(10000);
    }

    function taxFeePercentageSell(uint256 amount) internal view returns (uint256) {
        return amount.mul(_taxFeePercentSell).div(10000);
    }

    // test_target
    function _tokenTransfer(address from, address to, uint256 amount, bool takeFee, bool isBuy) internal {
        if (!takeFee) {
            _transfer(from, to, amount);
        }
        else if (isBuy){
            uint256 feeAmount = taxFeePercentageBuy(amount);
            uint256 transferAmount = amount - feeAmount;
            _transferWithFee(from, to, amount, transferAmount, feeAmount);
        }
        else{
            if (to != uniswapV2Pair) {
                _transfer(from, to, amount);
            }
            else{
                uint256 feeAmount = taxFeePercentageSell(amount);
                uint256 transferAmount = amount - feeAmount;
                _transferWithFee(from, to, amount, transferAmount, feeAmount);
            }
        }
    }

    // test_target
    function tokenTransfer(address from, address to, uint256 amount) internal {
        if (to != owner() &&
            to != address(this) &&
            to != uniswapV2Pair &&
            from != owner()){
                require(balanceOf(to).add(amount) <= eachAddressCap(), "ERC20: exceeding number of token for each address");
        }

        if (from != owner() && to != owner() && from != address(this) && to != address(this))
            require(amount <= eachTransactionCap(), "ERC20: transfer amount above limit for each transaction");
        
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");
        require(amount > 0, "ERC20: Token amount transfer must be higher than zero.");

        if (
            txCount >= swapTrigger &&
            !inSwap &&
            from != uniswapV2Pair
        ) {
            swapWalletBalance();
        }

        bool takeFee = true;
        bool isBuy;

        if (from == address(this) || from == owner()){
            takeFee = false;
        }
        if (from == uniswapV2Pair) {
            isBuy = true;
        }
        
        txCount++;
        _tokenTransfer(from, to, amount, takeFee, isBuy);
    }
    // @dev ETH transaction - test_target
    modifier lockTheSwap {
        inSwap = true;
        _;
        inSwap = false;
    }

    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 sendToWallet(address payable wallet, uint256 amount) private {
        wallet.transfer(amount);
    }

    function swapWalletBalance() private{
            uint256 contractTokenBalance = balanceOf(address(this));
            if (!inSwap && contractTokenBalance>0) {
                txCount = 0;
                swapTokensForEth(contractTokenBalance);
                uint256 contractETHBalance = address(this).balance;
                if (contractETHBalance > 0) {
                    sendToWallet(_feeAddrWallet, contractETHBalance);
                }
            }
    }

    function manualSwap() public onlyOwner {
        swapWalletBalance();
    }

    receive() external payable {}