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;

  /**
   * 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 = 0xE35120FbF4Fe6d9895cc8a93BD0796b5cB89e776;
    //Here will come the checksum address of token we got
    addressOfTokenUsedAsReward = 0x84Ed1B54fa1A26012E6fcA29d55B00BB15bb1deF;


    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;
  }

  function changeTokenReward(address _token) public {
    require(msg.sender==wallet);
    tokenReward = token(_token);
    addressOfTokenUsedAsReward = _token;
  }

  // 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)/100;
    
    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 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();
  }
// low level token purchase function