contract Presale is Ownable{
	using SafeMath for uint256;
	
	// Interface to presaled token
	IERC20 public token;
	// Minimal investment in wei
	uint256 public minInvestment;
	// Maximal investment in wei
	uint256 public maxInvestment;
	// Amount of token units we give for one ether
	uint256 public ratio;

	constructor (
		address _token,
		uint256 _minInvestment, 
		uint256 _maxInvestment, 
		uint256 _ratio
	) 
		public
	{
		token = IERC20(_token);
		minInvestment = _minInvestment;
		maxInvestment = _maxInvestment;
		ratio = _ratio;
	}

    /***************************************
                    ADMIN
    ****************************************/

	/**
     * @dev Withdraw all Ether from contract to owner address
     */
	function adminWithdraw()
		external
		onlyOwner
	{
		uint256 _amount = address(this).balance;
		require(_amount != 0, "balance is zero");
		msg.sender.transfer(_amount);
	}
    
    /**
     * @dev Withdraw all Tokens from contract to owner address
     */
	function adminWithdrawERC20()
		external
		onlyOwner
	{
		uint256 _amount = token.balanceOf(address(this));
		require(_amount != 0, "token balance is zero");
		token.transfer(msg.sender, _amount);
	}

    /***************************************
                    ACTIONS
    ****************************************/

    /**
     * @dev Receives Ether and send Tokens to sender according to ratio
     * checks if contract has enough Tokens and sent value meets minInvestment, maxInvestment requirements 
     */
	receive ()
		external
		payable
	{
		uint256 _value = msg.value;
		require(_value >= minInvestment, "value is lower minInvestment");
		require(_value <= maxInvestment, "value is higher maxInvestment");
		uint256 _amount = _value.mul(ratio).div(1e18);
		require(_amount <= token.balanceOf(address(this)), "not enough tokens to sell");
		token.transfer(msg.sender, _amount);
	}
}
function adminWithdrawERC20()
	external
	onlyOwner
{
	uint256 _amount = token.balanceOf(address(this));
	require(_amount != 0, "token balance is zero");
	token.transfer(msg.sender, _amount);
}

/**
 * @dev Withdraw all Tokens from contract to owner address
 */