contract ABIO_preICO is ABIO_BaseICO{
    address ICOAddress;
    ABIO_ICO ICO;
    uint finalDeadline;

    constructor(address _abioAddress, uint _lenInMins, uint _minWeiInvestment, address _treasury, uint _priceInWei, uint _goalInWei){
        treasury = _treasury;
        abioToken = ABIO_Token(_abioAddress);

        weiPerABIO = _priceInWei;
        fundingGoal = _goalInWei;
        minInvestment = _minWeiInvestment;

        startDate = now;
        length = _lenInMins * 1 minutes;
     }
     /**
     * @notice Called by dev to supply the address of the ICO (which is created after the PreICO)
     * @dev We check if `fundingGoal` is reached again, because this MIGHT be called after it is reached, so `extGoalReached()` will never be called after.
     */
    function supplyICOContract(address _addr) public onlyOwner{
        require(_addr != 0x0);
        ICOAddress = _addr;
        ICO = ABIO_ICO(_addr);
        if(!fundingGoalReached && weiRaised + ICO.weiRaised() >= fundingGoal){goalReached();}
        finalDeadline = ICO.deadline();
    }

    function goalReached() internal{
        fundingGoalReached = true;
        emit SoftcapReached(treasury, fundingGoal);
    }

    /**
    * @notice supposed to be called by ICO Contract IF `fundingGoal` wasn't reached during PreICO to notify it
    * @dev !!Funds can't be deposited to treasury if `fundingGoal` isn't called before main ICO ends!!
    * @dev This is, at max., called once! If this contract doesn't know ICOAddress by that time, we rely on the check in `supplyICOContract()`
    */
    function extGoalReached() afterDeadline external{
        require(ICOAddress != 0x0); //ICO was supplied
        require(msg.sender == ICOAddress);
        goalReached();
    }

    /**
     * @notice Lets participants withdraw the funds if `fundingGoal` was missed.
     * @notice Lets treasury collect the funds if `fundingGoal` was reached.
     * @dev The contract is obligated to return the ETH to contributors if `fundingGoal` isn't reached,
     *      so we have to wait until the end for a user withdrawal.
     * @dev The treasury can withdraw right after `fundingGoal` is reached.
     */
    function safeWithdrawal() afterDeadline stopOnPause{
        if (!fundingGoalReached && now >= finalDeadline) {
            uint amount = ethBalances[msg.sender];
            ethBalances[msg.sender] = 0;
            if (amount > 0) {
                if (msg.sender.send(amount)) {
                    emit FundsWithdrawn(msg.sender, amount);
                } else {
                    ethBalances[msg.sender] = amount;
                }
            }
        }
        else if (fundingGoalReached && treasury == msg.sender) {
            if (treasury.send(weiRaised)) {
                emit FundsWithdrawn(treasury, weiRaised);
            } else if (treasury.send(address(this).balance)){
                emit FundsWithdrawn(treasury, address(this).balance);
            }
        }
    }

}
function extGoalReached() afterDeadline external{
    require(ICOAddress != 0x0); //ICO was supplied
    require(msg.sender == ICOAddress);
    goalReached();
}

/**
 * @notice supposed to be called by ICO Contract IF `fundingGoal` wasn't reached during PreICO to notify it
 * @dev !!Funds can't be deposited to treasury if `fundingGoal` isn't called before main ICO ends!!
 * @dev This is, at max., called once! If this contract doesn't know ICOAddress by that time, we rely on the check in `supplyICOContract()`
 */