contract medibitICO is Pausable {
  using SafeMath for uint256;

  mapping(address => uint256) balances;

mapping (address => mapping (address => uint256)) internal allowed;


  //Gas/GWei
  uint constant public minPublicContribAmount = 1 ether;
  

  // The token being sold
  medibitToken public token;
  uint256 constant public tokenDecimals = 18;


  // start and end timestamps where investments are allowed (both inclusive)
  uint256 public startTime; 
  uint256 public endTime; 


  // need to be enabled to allow investor to participate in the ico
  bool public icoEnabled;

  // address where funds are collected
  address public walletOne;

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

  // totalSupply
  uint256 public totalSupply = 50000000000 * (10 ** tokenDecimals);
  uint256 constant public toekensForBTCandBonus = 12500000000 * (10 ** tokenDecimals);
  uint256 constant public toekensForTeam = 5000000000 * (10 ** tokenDecimals);
  uint256 constant public toekensForOthers = 22500000000 * (10 ** tokenDecimals);


  //ICO tokens
  //Is calcluated as: initialICOCap + preSaleCap
  uint256 public icoCap;
  uint256 public icoSoldTokens;
  bool public icoEnded = false;

  address constant public walletTwo = 0x938Ee925D9EFf6698472a19EbAc780667999857B;
  address constant public walletThree = 0x09E72590206d652BD1aCDB3A8e358AeB3f21513A;

  //Sale rates

  uint256 constant public STANDARD_RATE = 1500000;

  event Burn(address indexed from, uint256 value);


  /**
   * 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);



  function medibitICO(address _walletOne) public {
    require(_walletOne != address(0));
    token = createTokenContract();
    
    //send all dao tokens to multiwallet
    uint256 tokensToWallet1 = toekensForBTCandBonus;
    uint256 tokensToWallet2 = toekensForTeam;
    uint256 tokensToWallet3 = toekensForOthers;
    
    walletOne = _walletOne;
    
    token.transfer(walletOne, tokensToWallet1);
    token.transfer(walletTwo, tokensToWallet2);
    token.transfer(walletThree, tokensToWallet3);
  }


  //
  // Token related operations
  //

  // creates the token to be sold.
  // override this method to have crowdsale of a specific mintable token.
  function createTokenContract() internal returns (medibitToken) {
    return new medibitToken();
  }


  // enable token tranferability
  function enableTokenTransferability() external onlyOwner {
    require(token != address(0));
    token.unpause();
  }

  // disable token tranferability
  function disableTokenTransferability() external onlyOwner {
    require(token != address(0));
    token.pause();
  }

  // transfer token to owner account for burn
   function transferUnsoldIcoTokens() external onlyOwner {
    require(token != address(0));
    uint256 unsoldTokens = icoCap.sub(icoSoldTokens);
    token.transfer(walletOne, unsoldTokens);
   }

  //
  // ICO related operations
  //

  // set multisign wallet
  function setwalletOne(address _walletOne) external onlyOwner{
    // need to be set before the ico start
    require(!icoEnabled || now < startTime);
    require(_walletOne != address(0));
    walletOne = _walletOne;
  }


  // set contribution dates
  function setContributionDates(uint64 _startTime, uint64 _endTime) external onlyOwner{
    require(!icoEnabled);
    require(_startTime >= now);
    require(_endTime >= _startTime);
    startTime = _startTime;
    endTime = _endTime;
  }


  // enable ICO, need to be true to actually start ico
  // multisign wallet need to be set, because once ico started, invested funds is transfered to this address
  // once ico is enabled, following parameters can not be changed anymore:
  // startTime, endTime, soldPreSaleTokens
  function enableICO() external onlyOwner{
    icoEnabled = true;
    icoCap = totalSupply;
  }

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

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

    uint256 weiAmount = msg.value;
    uint256 returnWeiAmount;

    // calculate token amount to be created
    uint rate = getRate();
    assert(rate > 0);
    uint256 tokens = weiAmount.mul(rate);

    uint256 newIcoSoldTokens = icoSoldTokens.add(tokens);

    if (newIcoSoldTokens > icoCap) {
        newIcoSoldTokens = icoCap;
        tokens = icoCap.sub(icoSoldTokens);
        uint256 newWeiAmount = tokens.div(rate);
        returnWeiAmount = weiAmount.sub(newWeiAmount);
        weiAmount = newWeiAmount;
    }

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

    token.transfer(beneficiary, tokens);
    icoSoldTokens = newIcoSoldTokens;
    if (returnWeiAmount > 0){
        msg.sender.transfer(returnWeiAmount);
    }

    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 {
    walletOne.transfer(address(this).balance);
  }



  // @return true if the transaction can buy tokens
  function validPurchase() internal constant returns (bool) {
    bool withinPeriod = now >= startTime && now <= endTime;
    bool nonMinimumPurchase;
    bool icoTokensAvailable = icoSoldTokens < icoCap;
 
    nonMinimumPurchase = msg.value >= minPublicContribAmount;
    

    return !icoEnded && icoEnabled && withinPeriod && nonMinimumPurchase && icoTokensAvailable;
  }



  // end ico by owner, not really needed in normal situation
  function endIco() external onlyOwner {
    icoEnded = true;
    // send unsold tokens to multi-sign wallet
    uint256 unsoldTokens = icoCap.sub(icoSoldTokens);
    token.transfer(walletOne, unsoldTokens);
  }

  // @return true if crowdsale event has ended
  function hasEnded() public constant returns (bool) {
    return (icoEnded || icoSoldTokens >= icoCap || now > endTime);
  }


  function getRate() public constant returns(uint){
    require(now >= startTime);
      return STANDARD_RATE;

  }

  // drain all eth for owner in an emergency situation
  function drain() external onlyOwner {
    owner.transfer(address(this).balance);
  }

}
function hasEnded() public constant returns (bool) {
  return (icoEnded || icoSoldTokens >= icoCap || now > endTime);
}

// @return true if crowdsale event has ended