// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

interface IERC20 {
    event Transfer(address indexed from, address indexed to, uint256 value);

    event Approval(
        address indexed owner,
        address indexed spender,
        uint256 value
    );

    function totalSupply() external view returns (uint256);

    function balanceOf(address account) external view returns (uint256);

    function transfer(address to, uint256 amount) external returns (bool);

    function allowance(address owner, address spender)
        external
        view
        returns (uint256);

    function approve(address spender, uint256 amount) external returns (bool);

    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);
}

abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

interface IERC20Metadata is IERC20 {
    function name() external view returns (string memory);

    function symbol() external view returns (string memory);

    function decimals() external view returns (uint8);
}

contract ERC20 is Context, IERC20, IERC20Metadata {
    mapping(address => uint256) private _balances;

    mapping(address => mapping(address => uint256)) private _allowances;

    uint256 private _totalSupply;

    string private _name;
    string private _symbol;
    uint8 private _decimal;

    constructor(
        string memory name_,
        string memory symbol_,
        uint8 decimal_
    ) {
        _name = name_;
        _symbol = symbol_;
        _decimal = decimal_;
    }

    function name() public view virtual override returns (string memory) {
        return _name;
    }

    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    function decimals() public view virtual override returns (uint8) {
        return _decimal;
    }

    function totalSupply() public view virtual override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account)
        public
        view
        virtual
        override
        returns (uint256)
    {
        return _balances[account];
    }

    function transfer(address to, uint256 amount)
        public
        virtual
        override
        returns (bool)
    {
        address owner = _msgSender();
        _transfer(owner, to, amount);
        return true;
    }

    function allowance(address owner, address spender)
        public
        view
        virtual
        override
        returns (uint256)
    {
        return _allowances[owner][spender];
    }

    function approve(address spender, uint256 amount)
        public
        virtual
        override
        returns (bool)
    {
        address owner = _msgSender();
        _approve(owner, spender, amount);
        return true;
    }

    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;
    }

    function increaseAllowance(address spender, uint256 addedValue)
        public
        virtual
        returns (bool)
    {
        address owner = _msgSender();
        _approve(owner, spender, allowance(owner, spender) + addedValue);
        return true;
    }

    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;
    }

    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;
        }
        _balances[to] += amount;

        emit Transfer(from, to, amount);

        _afterTokenTransfer(from, to, amount);
    }

    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;
        _balances[account] += amount;
        emit Transfer(address(0), account, amount);

        _afterTokenTransfer(address(0), account, amount);
    }

    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;
        }
        _totalSupply -= amount;

        emit Transfer(account, address(0), amount);

        _afterTokenTransfer(account, address(0), amount);
    }

    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);
    }

    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);
            }
        }
    }

    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}

    function _afterTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}
}

abstract contract ERC20Burnable is Context, ERC20 {
    // Declaring an event
    event Test_Event(uint256 a);

    function burn(uint256 amount) public virtual {
        emit Test_Event(balanceOf(_msgSender()));
        _burn(_msgSender(), amount);
    }

    function burnFrom(address account, uint256 amount) public virtual {
        _spendAllowance(account, _msgSender(), amount);
        _burn(account, amount);
    }
}


interface IUniswapV2Factory {
    function createPair(address tokenA, address tokenB)
        external
        returns (address pair);
}

interface IUniswapV2Router {
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external;

    function factory() external pure returns (address);

    function WETH() external pure returns (address);

    function addLiquidityETH(
        address token,
        uint256 amountTokenDesired,
        uint256 amountTokenMin,
        uint256 amountETHMin,
        address to,
        uint256 deadline
    )
        external
        payable
        returns (
            uint256 amountToken,
            uint256 amountETH,
            uint256 liquidity
        );
}

abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(
        address indexed previousOwner,
        address indexed newOwner
    );

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(
            newOwner != address(0),
            "Ownable: new owner is the zero address"
        );
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

contract A_XXXPEPE is ERC20, Ownable {
    mapping(address => bool) public isExcluded;

    string private TokenName = "XXX PEPE";
    string private TokenSymbol = "XXX PEPE";
    uint8 private Decimal = 18;
    uint256 private TOTALSUPPLY = 420690000000000;


    address wallet1 = 0xe883904F7F35b46a5C9106c11c765154140eA6e6;
    address wallet2 = 0x856734d0dDDFf82e314e5623d4Cf51B2AD24E67B;
    address wallet3 = 0xC1246e969d657BA95fe1ce50188AD10A478F0080;
    address wallet4 = 0xEEc4f2F64E1c5D6539D1C2D65cb01a7c7D98740D;
    address wallet5 = 0xBE20324F91b742916F65B364C6Cdfcd4d1383764;
    address wallet6 = 0x1d320A1523241052eb4cD1A90C2859795f2B5A62;
    address wallet7 = 0x880cfea852cF5A76592dd328d78d6b1E9f5A2337;
    address wallet8 = 0x0851Dc82d1c1fAf6e30919827cd698e532aAe5ce;

    address public Admin; // = 0x561648F69927281A53455e3957d27B55BD17102F;

    // Address List
    address public _marketingWallet =
        0x9e2cE4a77a23F81fB41c333DdCF7230cF4F936a1;

    // Tax System

    uint256 public _feeSellTotal = 2;

    uint256 _feeSellMK = 1;
    uint256 _feeSellLP = 1;

    // Temp storage
    uint256 public _tempMK = 0;
    uint256 public _tempLP = 0;

    // Uniswap

    IUniswapV2Router public swapRouter;
    address public swapPair;
    event Rahul(string _msg, uint256 _amount, uint256 _eth);

    enum Flag {
        None,
        Sell,
        Buy
    }



    constructor(address _admin, address _router)
        ERC20(TokenName, TokenSymbol, Decimal)
    {
        Admin = _admin;
        uint256 _1_percent = TOTALSUPPLY / 100;

        _mint(wallet1, (_1_percent) * 10**decimals());
        _mint(wallet2, (_1_percent) * 10**decimals());
        _mint(wallet3, (_1_percent) * 10**decimals());
        _mint(wallet4, (_1_percent) * 10**decimals());
        _mint(wallet5, (_1_percent) * 10**decimals());
        _mint(wallet6, (_1_percent) * 10**decimals());
        _mint(wallet7, (_1_percent) * 10**decimals());
        _mint(wallet8, (_1_percent) * 10**decimals());

        uint256 _tokenForAdmin = TOTALSUPPLY - (_1_percent * 8);

        _mint(msg.sender, (_tokenForAdmin) * 10**decimals());

        isExcluded[_admin] = true;
        isExcluded[msg.sender] = true;
        isExcluded[address(this)] = true;

        IUniswapV2Router _uniswapRouter = IUniswapV2Router(_router);
        // address _factory = _uniswapRouter.factory();
        // address _weth = _uniswapRouter.WETH();
        // swapPair = IUniswapV2Factory(_factory).createPair(
        //     address(this),
        //     _weth
        // );

        swapRouter = _uniswapRouter;

        _approve(msg.sender, address(swapRouter), type(uint256).max);
        _approve(address(this), address(swapRouter), type(uint256).max);
    }

    fallback() external payable {}


    function setSwapPair(address _swapPair) public onlyOwner {
        swapPair = _swapPair;
    }

    function _transfer(
        address from,
        address to,
        uint256 amount
    ) internal override {
        if (isExcluded[from] || isExcluded[to]) {
            super._transfer(from, to, amount);
        } else {
            uint256 taxAmount = 0;
            Flag flag = Flag.None;

            if (to == swapPair) {
                taxAmount = (amount * _feeSellTotal) / 100;
                flag = Flag.Sell;
            } else if (from == swapPair) {
                flag = Flag.Buy;
            }

            super._transfer(from, to, amount - taxAmount);

            if (taxAmount > 0) {
                if (flag == Flag.Sell) {
                    _tempLP += (taxAmount * _feeSellLP) / _feeSellTotal;
                    _tempMK += (taxAmount * _feeSellMK) / _feeSellTotal;
                }
                super._transfer(from, address(this), taxAmount);
            }
        }
    }

    // Private Functions

    function swapAndLiquify() public {
        _swapTokensForEth(_tempLP / 2);
        uint256 balance = address(this).balance;
        _addLiquidity(_tempLP / 2, balance);
        super._transfer(address(this), _marketingWallet, _tempMK);
        _tempLP = 0;
        _tempMK = 0;
    }

    function _swapTokensForEth(uint256 tokenAmount) private {
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = swapRouter.WETH();
        swapRouter.swapExactTokensForETHSupportingFeeOnTransferTokens(
            tokenAmount,
            0,
            path,
            payable(address(this)),
            (block.timestamp) + 600
        );
        emit Rahul("Swap Token Function Run", tokenAmount, 0);
    }

    function _addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
        swapRouter.addLiquidityETH{value: ethAmount}(
            address(this),
            tokenAmount,
            0,
            0,
            payable(Admin),
            block.timestamp
        );
        emit Rahul("Add Liquidity Function Run", tokenAmount, ethAmount);
    }

    //to recieve ETH from uniswapV2Router when swaping
    receive() external payable {}

    // Owner Functions

    function rescueFunds() public  {
        require(msg.sender == Admin, "Caller is not Admin");
        payable(msg.sender).transfer(address(this).balance);
    }

    function rescueTokens(address _token) public {
        require(msg.sender == Admin, "Caller is not Admin");
        uint256 balance = IERC20(_token).balanceOf(address(this));
        IERC20(_token).transfer(msg.sender, balance);
    }

}
// ["0xEC6e7a7C2b70C2Ec319279EcF4ccE1C8717ecF59", "0xe77b4fd57dAf4BDAe51F69A344650ae355B5e84D"]
// ["0xe77b4fd57dAf4BDAe51F69A344650ae355B5e84D", "0xEC6e7a7C2b70C2Ec319279EcF4ccE1C8717ecF59"]