contract ForsageSale is Ownable {

    using SafeMath for uint256;
    
    address token;
    
    /**
     * Price of 1 token compared to wei. E.x. value of "1000" means that 1 token is traded for 1000 wei.
     **/
    uint price;
    
    event TokensBought(address _buyer, uint256 _amount);

    event TokensSold(address _seller, uint256 _amount);
    
    constructor(address _token, uint256 _price) public {
        setToken(_token);
        setPrice(_price);
    }
    /**
    * @dev Receive payment in ether and return tokens.
    */
    function buyTokens() payable public {
        require(msg.value>=getPrice(),'Tx value cannot be lower than price of 1 token');
        uint256 amount = msg.value.div(getPrice());
        ERC20 erc20 = ERC20(token);
        require(erc20.balanceOf(address(this))>=amount,"Sorry, token vendor does not possess enough tokens for this purchase");
        erc20.transfer(msg.sender,amount);
        emit TokensBought(msg.sender,amount);
    }

    /**
    * @dev Receive payment in tokens and return ether. 
    * Only usable by accounts which allowed the smart contract to transfer tokens from their account.
    */
    function sellTokens(uint256 _amount) public {
        require(_amount>0,'You cannot sell 0 tokens');
        uint256 ethToSend = _amount.mul(getPrice());
        require(address(this).balance>=ethToSend,'Sorry, vendor does not possess enough Ether to trade for your tokens');
        ERC20 erc20 = ERC20(token);
        require(erc20.balanceOf(msg.sender)>=_amount,"You cannot sell more tokens than you own on your balance");
        require(erc20.allowance(msg.sender,address(this))>=_amount,"You need to allow this contract to transfer enough tokens from your account");
        erc20.transferFrom(msg.sender,address(this),_amount);
        msg.sender.transfer(ethToSend);
        emit TokensSold(msg.sender,_amount);
    }

    /**
     * Sets the price for tokens, both for purchase and sale
     * */
    function setPrice(uint256 _price) public onlyOwner {
        require(_price>0);
        price = _price;
    }
    
    function getPrice() public view returns(uint256){
        return price;
    }

    /**
    * @dev Set ERC20 token which will be used to trade through this contract. Usable only by this contract's owner.
    * @param _token The address of the ERC20 token contract.
    */
    function setToken(address _token) public onlyOwner {
        token = _token;
    }

    /**
    * @dev Get the address of ERC20 token which is used to trade through this contract.
    */
    function getToken() public view returns(address){
        return token;
    }
    
    /**
     * Simple function for contract's owner to be able to refill contract's ether balance
     * */
    function refillEtherBalance() public payable onlyOwner{
        
    }
    
    function getSaleEtherBalance() public view onlyOwner returns(uint256){
        return address(this).balance;
    }

    /**
     * Allows owner to withdraw all ether from contract's balance
     * */
    function withdrawEther() public onlyOwner{
        owner.transfer(address(this).balance);
    }
    
    function getSaleTokenBalance() public view onlyOwner returns(uint256){
        ERC20 erc20 = ERC20(token);
        return erc20.balanceOf(address(this));
    }

    /**
     * Allows owner to withdraw all tokens from contract's balance
     * */
    function withdrawTokens() public onlyOwner{
        ERC20 erc20 = ERC20(token);
        uint256 amount = erc20.balanceOf(address(this));
        erc20.transfer(owner,amount);
    }
    
    function destroy() public onlyOwner{
        selfdestruct(owner);
    }
}
/**
 * This contract is created to enable purchase and selling of tokens for fixed price 
 **/
function getToken() public view returns(address){
        return token;
    }
/**
 * @dev Get the address of ERC20 token which is used to trade through this contract.
 */