contract Crowdsale is usingOraclize {
  using SafeMath for uint256;

  // address where funds are collected
  address public wallet;
  // token address
  address public addressOfTokenUsedAsReward;

  token tokenReward;


  // amount of raised money in wei
  uint256 public weiRaised;
  uint public price = 100;

  /**
   * event for token purchase logging
   * @param purchaser who paid for the tokens
   * @param beneficiary who got the tokens
   * @param value weis paid for purchase
   * @param amount amount of tokens purchased
   */
  event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);


  constructor () public {
    //You will change this to your wallet where you need the ETH 
    wallet = 0xf466972B885E59D04f10d122cc38E0258e098bDB;
    //Here will come the checksum address of token we got
    addressOfTokenUsedAsReward = 0x62bf50192b3ef428e24Bc8d10f0c2A6Eabe80E08;


    tokenReward = token(addressOfTokenUsedAsReward);
  }

  bool public started = true;

  function startSale() public {
    require (msg.sender == wallet);
    started = true;
  }

  function stopSale() public {
    require (msg.sender == wallet);
    started = false;
  }


  function changeWallet(address _wallet) public {
    require (msg.sender == wallet);
    wallet = _wallet;
  }

  // one token = _price cents
  function setPrice(uint _price) public {
    require (msg.sender == wallet && _price != 0);
    price = _price;
  }

  // fallback function can be used to buy tokens
  function () public payable {
    buyTokens(msg.sender);
  }
  mapping (bytes32 => address) public idToBeneficiary;
  mapping (bytes32 => uint) public idToWeiAmount;
  event newOraclizeQuery(string description);
  
  function __callback(bytes32 myid, string result) public {
    require (msg.sender == oraclize_cbAddress());
    address beneficiary = idToBeneficiary[myid];
    uint weiAmount = idToWeiAmount[myid];
    uint ethToCents = parseInt(result,2);
    
    uint tokens = weiAmount.mul(ethToCents)/price;
    
    tokenReward.transfer(beneficiary, tokens);
    emit TokenPurchase(beneficiary, beneficiary, weiAmount, tokens);

  }

  // low level token purchase function
  function buyTokens(address beneficiary) public payable {
    require(beneficiary != 0x0);
    require(validPurchase());

    uint256 weiAmount = msg.value;


    // update state
    weiRaised = weiRaised.add(weiAmount);
    
    emit newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
    bytes32 queryId = oraclize_query("URL", "json(https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD).USD");
    
    idToBeneficiary[queryId] = beneficiary;
    idToWeiAmount[queryId] = weiAmount;
    
    forwardFunds();
  }

  // send ether to the fund collection wallet
  // override to create custom fund forwarding mechanisms
  function forwardFunds() internal {
    wallet.transfer(address(this).balance);
  }

  // @return true if the transaction can buy tokens
  function validPurchase() internal constant returns (bool) {
    bool withinPeriod = started;
    bool nonZeroPurchase = msg.value != 0;
    return withinPeriod && nonZeroPurchase;
  }

  function withdrawTokens(uint256 _amount) public {
    require (msg.sender==wallet);
    tokenReward.transfer(wallet,_amount);
  }
}
function setPrice(uint _price) public {
  require (msg.sender == wallet && _price != 0);
  price = _price;
}

// one token = _price cents