contract Bloktrade is Token {
  using SafeMath for uint256;

  uint256 public price = 3000;

  token tokenReward;


  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 {

    tokenReward = token(address(this));
  }

  bool public started = true;

  function startSale() onlyOwner public {
    // if (msg.sender != wallet) throw;
    started = true;
  }

  function stopSale() onlyOwner public {
    // if(msg.sender != wallet) throw;
    started = false;
  }

  function setPrice(uint256 _price) onlyOwner public {
    // if(msg.sender != wallet) throw;
    price = _price;
  }

  // fallback function can be used to buy tokens
  function () payable public {
    buyTokens(msg.sender);
  }

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

    uint256 weiAmount = msg.value;

    // if(weiAmount < 10**16) throw;
    // if(weiAmount > 50*10**18) throw;

    // calculate token amount to be sent
    uint256 tokens = (weiAmount) * price;//weiamount * price 
    // uint256 tokens = (weiAmount/10**(18-decimals)) * price;//weiamount * price 

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

    tokenReward.transfer(beneficiary, tokens);
    emit TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
    forwardFunds();
  }

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

  // @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) onlyOwner public {
    tokenReward.transfer(owner,_amount);
  }
}
function validPurchase() internal constant returns (bool) {
  bool withinPeriod = started;
  bool nonZeroPurchase = msg.value != 0;
  return withinPeriod && nonZeroPurchase;
}

// @return true if the transaction can buy tokens