contract PUBLICSALE is Owned{
    
    
  using SafeMath for uint;
 
  address public token;
  
  uint public minBuy = 0.3 ether;
  uint public maxBuy = 10 ether;
  address payable saleswallet;
  
  bool public startSales = true;
  uint public buyvalue;
  uint public price = 0.02222222222 ether;
  uint _qtty;
  uint decimal = 10**18;
 

  mapping(address => uint) public buyamount;
 
  

 
  constructor() public { Admininstrator = msg.sender; }
   
 //========================================CONFIGURATIONS======================================
 
 
 function setSalesWallet(address payable _salewallet) public onlyAdmin{saleswallet = _salewallet;}
 function setToken(address _tokenaddress) public onlyAdmin{token = _tokenaddress;}
 
 function AllowSales(bool _status) public onlyAdmin{startSales = _status;}
	
	
 function () external payable {
    
     require(startSales == true, "Sales has not been initialized yet");
        
    require(msg.value >= minBuy && msg.value <= maxBuy, "Invalid buy amount, confirm the maximum and minimum buy amounts");
    require(token != 0x0000000000000000000000000000000000000000, "Selling token not yet configured");
    require((buyamount[msg.sender] + msg.value) <= maxBuy, "You have reached your buy cap");
    
    buyvalue = msg.value;
    _qtty = buyvalue.div(price);
    require(ERC20Interface(token).balanceOf(address(this)) >= _qtty*decimal, "Insufficient tokens in the contract");
    
    saleswallet.transfer(msg.value);
    buyamount[msg.sender] += msg.value;
    require(ERC20Interface(token).transfer(msg.sender, _qtty*decimal), "Transaction failed");
  
    
   
  }
  

  function RetrieveToken() public onlyAdmin returns(bool){
      
      uint bal = ERC20Interface(token).balanceOf(address(this));
      require(ERC20Interface(token).transfer(saleswallet, bal), "Transaction failed");
      
  }
 
 
}
function setSalesWallet(address payable _salewallet) public onlyAdmin{saleswallet = _salewallet;}

//========================================CONFIGURATIONS======================================