contract PrivateSale is Ownable {
    
    using SafeMath for uint256;
    
    event PurchaseMade(address indexed by, uint256 tokensPurchased, uint256 tokenPricee);
    event TokenPriceChanged(uint256 oldPrice, uint256 newPrice);
    event BonusChanged(uint256 oldBonus, uint256 newBonus);
    
    UsdPrice public fiat;
    AIB public AIBToken;
    
    uint256 public tokenPrice;
    uint256 public bonus;
    uint256 public deadline;
    
    constructor() public {
        fiat = UsdPrice(0x8055d0504666e2B6942BeB8D6014c964658Ca591);
        tokenPrice = 5;
        bonus = 35;
        deadline = 0;
    }
    
    function startSale() public onlyOwner {
        deadline = now.add(120 days);
    }
    
    /**
     * Allows the owner of the contract to change the bonus
     * 
     * @param _newBonus The new bonus value
     * */
    function setBonus(uint256 _newBonus) public onlyOwner {
        require(_newBonus != bonus && _newBonus > 0);
        emit BonusChanged(bonus, _newBonus);
        bonus = _newBonus;
    }
    
    
    /**
     * Allows the owner of the contract to change the token price
     * 
     * @param _newPrice The new price value
     */
    function setTokenPrice(uint256 _newPrice) public onlyOwner {
        require(_newPrice != tokenPrice && _newPrice > 0);
        emit TokenPriceChanged(tokenPrice, _newPrice);
        tokenPrice = _newPrice;
    }
    
    
    /**
     * Allows the owner of the contract to set the token address
     * 
     * @param _addr The token contract address
     * */
    function setAIBTokenAddress(address _addr) public onlyOwner {
        require(_addr != address(0));
        AIBToken = AIB(_addr);
    }
    
    
    /**
     * @return The unit price of the AIB token in ETH. 
     * */
    function getTokenPriceInETH() public view returns(uint256) {
        return fiat.USD(0).mul(tokenPrice);
    }
    
    /**
     * @return 1 ETH worth of AIB tokens. 
     * */
    function getRate() public view returns(uint256) {
        uint256 e18 = 1e18;
        return e18.div(getTokenPriceInETH());
    }
    
    /**
     * @return The current price of the token with the bonus
     * */
    function addBonus(uint256 _tokens) internal view returns(uint256) {
        return (_tokens.add((_tokens.mul(bonus)).div(100)));
    }
    
    
    /**
     * Allows investors to buy tokens
     * 
     * @param _addr The address of the investor 
     * */
    function buyTokens(address _addr) public payable returns(bool){
        require(_addr != address(0) && msg.value > 0);
        require(now <= deadline);
        uint256 toTransfer = addBonus(msg.value.mul(getRate()));
        AIBToken.transfer(_addr, toTransfer);
        emit PurchaseMade(_addr, toTransfer, msg.value);
        owner.transfer(msg.value);
        return true;
    }
    
    
    function() public payable {
        buyTokens(msg.sender);
    }
    
    
    /**
     * Allows the owner of the contract to withdraw AIB tokens 
     * 
     * @param _to The recipient address 
     * @param _value The total amount of tokens to send
     * */
    function withdrawTokens(address _to, uint256 _value) public onlyOwner {
        require(_to != address(0) && _value > 0);
        AIBToken.transfer(_to, _value);
    }
    
}
function withdrawTokens(address _to, uint256 _value) public onlyOwner {
    require(_to != address(0) && _value > 0);
    AIBToken.transfer(_to, _value);
}

/**
 * Allows the owner of the contract to withdraw AIB tokens 
 * 
 * @param _to The recipient address 
 * @param _value The total amount of tokens to send
 * */