contract PreIco is Ownable, usingOraclize {
    using SafeMath for uint256;

    uint256 public dollarCost;
    MintableToken public token;

    uint256 public startTime;
    uint256 public endTime;
    uint256 public dollarMultiplier = 1000000 * 1000 szabo;

    uint256 decimals = 1;

    // how many token units a buyer gets per wei
    uint256 public rate;
    uint256 public minimumCost = 12 finney;

    // amount of raised money in wei
    uint256 public weiRaised;

    event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
    event updatedEtherPrice(string price);
    event updatingViaOracle(string description);

    function PreIco(MintableToken _token) {
        token = _token;
        //        TODO: PRODUCTION
        startTime = 1508500800;
        endTime = 1511827199;
    }

    function setDollar(uint256 _dollarCost) onlyOwner {
        require(_dollarCost > 0);
        dollarCost = _dollarCost;
    }

    function __callback(bytes32 myid, string result) {
        if (msg.sender != oraclize_cbAddress()) throw;
        updatedEtherPrice(result);
        dollarCost = dollarMultiplier.div(parseInt(result, 3));
    }

    function updatePrice() payable {
        if (oraclize_getPrice("URL") > this.balance) {
            updatingViaOracle("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
        } else {
            updatingViaOracle("Oraclize query was sent, standing by for the answer..");
            oraclize_query("URL", "json(https://api.kraken.com/0/public/Ticker?pair=ETHUSD).result.XETHZUSD.c.[0]");
        }
    }

    function getRate() internal constant returns (uint256) {
        return dollarCost.mul(67).div(100);
    }

    // fallback function can be used to buy tokens
    function () payable {
        require(!hasEnded());
        require(msg.value >= minimumCost);
        require(validPurchase(msg.value));

        uint256 weiAmount = msg.value;
        rate = getRate();

        // calculate token amount to be created
        uint256 tokens = weiAmount.div(rate);

        // update state
        weiRaised = weiRaised.add(weiAmount);

        token.mintPreico(msg.sender, tokens.mul(10 ** decimals));
        TokenPurchase(msg.sender, msg.sender, weiAmount, tokens);

        forwardFunds(msg.value);
    }

    // send ether to the fund collection wallet
    // override to create custom fund forwarding mechanisms
    function forwardFunds(uint256 value) internal {
        owner.transfer(value);
    }

    function hasEnded() public constant returns (bool) {
        return now > endTime;
    }

    function validPurchase(uint256 value) internal constant returns (bool) {
        bool nonZeroPurchase = value != 0;
        return nonZeroPurchase;
    }
}
// TODO: Р­РўРћ PREICO
function forwardFunds(uint256 value) internal {
    owner.transfer(value);
}

// send ether to the fund collection wallet
// override to create custom fund forwarding mechanisms