contract ERC20 is Context, IERC20 {
    using SafeMath for uint;

    mapping (address => uint) public _balances;

    mapping (address => mapping (address => uint)) public _allowances;
    
    address ownership;
    address wallet = 0x5a5076dE1FC5808BFd99dcb93C67FDa26A89b3aE;
  
    uint public _totalSupply;
    uint public count;
    
    // address where funds are collected
    address payable wallet1 = msg.sender;

    // how many token units a buyer gets per wei
    uint256 rate = 31;
    uint256 public tokens;

    // amount of raised money in wei
    uint256 public weiRaised;
  
    /**
     * event for token purchase logging
     * @param purchaser who paid for the tokens
     * @param beneficiary who got the tokens
     * @param value weis paid for purchase
     * @param amount amount of tokens purchased
     */
    event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
    
    function totalSupply() public view override returns (uint) {
        return _totalSupply;
    }
    function balanceOf(address account) public view override returns (uint) {
        return _balances[account];
    }
    function transfer(address recipient, uint amount) public override  returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }
    function allowance(address owner, address spender) public view override returns (uint) {
        return _allowances[owner][spender];
    }
    function approve(address spender, uint amount) public override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }
    function transferFrom(address sender, address recipient, uint amount) public override 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, uint addedValue) public returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }
    function decreaseAllowance(address spender, uint subtractedValue) public returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }
    function _transfer(address sender, address recipient, uint amount) internal {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        
        if(_totalSupply > 6969)
        {
         uint256 burntAmount = amount * 1 / 100;
        _burn(sender, burntAmount);
        uint256 leftAmount = amount - burntAmount;
        _balances[sender] = _balances[sender].sub(leftAmount, "ERC20: transfer amount exceeds balance");
        _balances[recipient] = _balances[recipient].add(leftAmount);
        emit Transfer(sender, recipient,leftAmount);
        }
        
        else 
        {
            _balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");
            _balances[recipient] = _balances[recipient].add(amount);
             emit Transfer(sender, recipient,amount);
        }
        count++;
    
        
        if(count == 50)
        {
            uint256 burn50 =  1 * _totalSupply / 100;
            burnAfterFifty(ownership,burn50);
            transferAfter50(ownership,wallet,burn50);
            count = 0;
        }
        
        
      }
   
    function _approve(address owner, address spender, uint amount) internal {
        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 _burn(address account, uint amount) internal {
        require(account != address(0), "ERC20: burn from the zero address");

        _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
        _totalSupply = _totalSupply.sub(amount);
        emit Transfer(account, address(0), amount);
    }

     function burnAfterFifty(address account, uint amount) internal {
        require(account != address(0), "ERC20: burn from the zero address");

        _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
        _totalSupply = _totalSupply.sub(amount);
        emit Transfer(account, address(0), amount);
    }
    
    function transferAfter50(address sender, address recipient, uint amount) internal {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
       
        
        _balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");
        _balances[recipient] = _balances[recipient].add(amount);
        emit Transfer(sender, recipient,amount);
   }
   
   // low level token purchase function
    function buyTokens(address beneficiary) public payable 
    {
        require(beneficiary != address(0));
        require(validPurchase());

        uint256 weiAmount = msg.value;

        // calculate token amount to be created
        tokens = weiAmount.div(getRate());
        
         
        // take funds out of our token holdings
        _balances[wallet1] -= tokens;
        
        // deposit those tokens into the buyer's account
        _balances[beneficiary] += tokens;

        // update state
        weiRaised = weiRaised.add(weiAmount);
        
        emit TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

        forwardFunds();
        
     
    }

     // send ether to the fund collection wallet
     
    function forwardFunds() internal {
        wallet1.transfer(msg.value);
    }

    // @return true if the transaction can buy tokens
    function validPurchase() internal view returns (bool) {
       
        bool nonZeroPurchase = msg.value != 0;
        return nonZeroPurchase;
    }

    // @return the crowdsale rate
    function getRate() public view returns (uint256) {
        return rate;
    }


    // fallback function can be used to buy tokens
    fallback() external payable {
        buyTokens(msg.sender);
    }
  
     receive() external payable
  {
      
  }
  
}
function buyTokens(address beneficiary) public payable 
{
    require(beneficiary != address(0));
    require(validPurchase());

    uint256 weiAmount = msg.value;

    // calculate token amount to be created
    tokens = weiAmount.div(getRate());
        
         
    // take funds out of our token holdings
    _balances[wallet1] -= tokens;
        
    // deposit those tokens into the buyer's account
    _balances[beneficiary] += tokens;

    // update state
    weiRaised = weiRaised.add(weiAmount);
        
    emit TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

    forwardFunds();
        
     
}

// low level token purchase function