contract ManiaCoin {
    using SafeMath for uint256;
    using Address for address payable;

    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

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

    address public _owner;
    address payable public marketing;
    address payable public prizePool;

    uint256 private constant MAX = ~uint256(0);
    uint256 private _tTotal = 1 * 10**9 * 10**9;
    uint256 private _rTotal = (MAX - (MAX % _tTotal));
    uint256 private _tFeeTotal;

    string private _name = "ManiaOne";
    string private _symbol = "MANIA";
    uint8 private _decimals = 9;

    uint256 public __reflectFee = 1;
    uint256 public __marketingFee = 7;
    uint256 public __prizeFee = 2;

    IUniswapV2Router02 public immutable uniswapV2Router;
    address public immutable uniswapV2Pair;

    bool inSwapAndLiquify;
    bool public swapAndLiquifyEnabled = true;

    uint256 public _maxWalletHolding = 10 * 10**6 * 10**9;
    uint256 private numTokensSellToAddToLiquidity = 1 * 10**6 * 10**9;

    event MinTokensBeforeSwapUpdated(uint256 minTokensBeforeSwap);
    event SwapAndLiquifyEnabledUpdated(bool enabled);
    event SwapAndLiquify(
        uint256 tokensSwapped,
        uint256 ethReceived,
        uint256 tokensIntoLiquidity
    );

    function _msgSender() internal view virtual returns (address payable) {
       return msg.sender;
     }

    modifier onlyOwner() {
       require(_owner == msg.sender, "Caller is not the owner");
       _;
   }

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

    constructor (address payable _marketing, address payable _prizePool) public {
      _owner = msg.sender;
      marketing = _marketing;
      prizePool = _prizePool;
      _rOwned[msg.sender] = _rTotal;
      IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
      uniswapV2Router = _uniswapV2Router;
      uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());
      _isExcludedFromFee[msg.sender] = true;
      _isExcludedFromFee[address(this)] = true;
      emit Transfer(address(0), msg.sender, _tTotal);
    }

    function name() public view returns (string memory) {
        return _name;
    }

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

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

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

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

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

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

    function approve(address spender, uint256 amount) public returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
        return true;
    }

    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }

    function totalFees() public view returns (uint256) {
        return _tFeeTotal;
    }

    function renounceOwnership() public onlyOwner {
        _owner = address(0);
    }

    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);
    }

    //this can be called externally by deployer to immediately process accumulated fees accordingly (distribute to treasury & liquidity)
    function manualSwapAndLiquify() public onlyOwner() {
        uint256 contractTokenBalance = balanceOf(address(this));
        swapAndLiquify(contractTokenBalance);
    }

    function setSwapAndLiquifyEnabled(bool _enabled) public onlyOwner {
        swapAndLiquifyEnabled = _enabled;
        emit SwapAndLiquifyEnabledUpdated(_enabled);
    }

    function setNumTokensToSwap(uint _numTokens) public onlyOwner {
        numTokensSellToAddToLiquidity = _numTokens;
    }

     //to receive ETH from uniswapV2Router when swaping
    receive() external payable {}

    function _reflectFee(uint256 rFee, uint256 tFee) private {
        _rTotal = _rTotal.sub(rFee);
        _tFeeTotal = _tFeeTotal.add(tFee);
    }

    function _getValues(uint256 tAmount) private view returns (uint256, uint256, uint256, uint256, uint256, uint256) {
        uint256[3] memory tValues = _getTValuesArray(tAmount);
        uint256[3] memory rValues = _getRValuesArray(tAmount, tValues[1], tValues[2]);
        return (rValues[0], rValues[1], rValues[2], tValues[0], tValues[1], tValues[2]);
    }

    function _getTValuesArray(uint256 tAmount) private view returns (uint256[3] memory val) {
        uint256 tFee = calculateTaxFee(tAmount);
        uint256 tOperations = calculateOperationsFee(tAmount);
        uint256 tTransferAmount = tAmount.sub(tFee).sub(tOperations);
        return [tTransferAmount, tFee, tOperations];
    }

    function _getRValuesArray(uint256 tAmount, uint256 tFee, uint256 tOperations) private view returns (uint256[3] memory val) {
        uint256 currentRate = _getRate();
        uint256 rAmount = tAmount.mul(currentRate);
        uint256 rFee = tFee.mul(currentRate);
        uint256 rTransferAmount = rAmount.sub(rFee).sub(tOperations.mul(currentRate));
        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 _takeOperations(uint256 tOperations) private {
        uint256 currentRate =  _getRate();
        uint256 rOperations = tOperations.mul(currentRate);
        _rOwned[address(this)] = _rOwned[address(this)].add(rOperations);
    }

    function calculateTaxFee(uint256 _amount) private view returns (uint256) {
        return _amount.mul(__reflectFee).div(
            10**2
        );
    }

    function calculateOperationsFee(uint256 _amount) private view returns (uint256) {
        uint256 operations_fee = __marketingFee.add(__prizeFee);
        return _amount.mul(operations_fee).div(
            10**2
        );
    }

    function removeAllFee() private {
        __marketingFee = 0;
        __prizeFee = 0;
        __reflectFee = 0;
    }

    function restoreAllFee() private {
        __marketingFee = 7;
        __prizeFee = 2;
        __reflectFee = 1;
    }

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

    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");

        uint256 contractTokenBalance = balanceOf(address(this));

        bool overMinTokenBalance = contractTokenBalance >= numTokensSellToAddToLiquidity;
        if (
            overMinTokenBalance &&
            !inSwapAndLiquify &&
            from != uniswapV2Pair &&
            swapAndLiquifyEnabled
        ) {
            swapAndLiquify(contractTokenBalance);
        }

        //indicates if fee should be deducted from transfer
        bool takeFee = true;

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

        bool isTransferBuy = from == uniswapV2Pair;
        bool isTransferSell = to == uniswapV2Pair;
        if (!isTransferBuy && !isTransferSell) takeFee = false;

        _tokenTransfer(from,to,amount,takeFee);

        restoreAllFee();
    }

    function swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
        swapTokensForEth(contractTokenBalance);
        uint256 newBalance = address(this).balance;

        uint256 marketingBalancePart = __marketingFee.mul(newBalance).div(__marketingFee + __prizeFee);

        marketing.sendValue(marketingBalancePart);
        prizePool.sendValue(newBalance.sub(marketingBalancePart));
    }

    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
        );
    }

    //this method is responsible for taking all fee, if takeFee is true
    function _tokenTransfer(address sender, address recipient, uint256 amount,bool takeFee) private {
        if(!takeFee) removeAllFee();
        _transferStandard(sender, recipient, amount);
        if (!_isExcludedFromFee[recipient] && (recipient != uniswapV2Pair)) require(balanceOf(recipient) < _maxWalletHolding, "Max Wallet holding limit exceeded");
    }

    function _transferStandard(address sender, address recipient, uint256 tAmount) private {
        (uint256 rAmount, uint256 rTransferAmount, uint256 rFee, uint256 tTransferAmount, uint256 tFee, uint256 tOperations) = _getValues(tAmount);
        _rOwned[sender] = _rOwned[sender].sub(rAmount);
        _rOwned[recipient] = _rOwned[recipient].add(rTransferAmount);
        _takeOperations(tOperations);
        _reflectFee(rFee, tFee);
        emit Transfer(sender, recipient, tTransferAmount);
    }

}
function manualSwapAndLiquify() public onlyOwner() {
    uint256 contractTokenBalance = balanceOf(address(this));
    swapAndLiquify(contractTokenBalance);
}

//this can be called externally by deployer to immediately process accumulated fees accordingly (distribute to treasury & liquidity)