contract MBEToken is BurnableToken {
  string public constant name = "MoBee";
  string public constant symbol = "MBE";
  uint8 public constant decimals = 18;
  address public tokenWallet;
  address public founderWallet;
  address public bountyWallet;
  address public multisig = 0xA74246dC71c0849acCd564976b3093B0B2a522C3;
  uint public currentFundrise = 0;
  uint public raisedEthers = 0;

  uint public constant INITIAL_SUPPLY = 20000000 ether;
  
  uint256 constant THOUSAND = 1000;
  uint256 constant TEN_THOUSAND = 10000;
  uint public tokenRate = THOUSAND.div(9); // tokens per 1 ether ( 1 ETH / 0.009 ETH = 111.11 MBE )
  uint public tokenRate30 = tokenRate.mul(100).div(70); // tokens per 1 ether with 30% discount
  uint public tokenRate20 = tokenRate.mul(100).div(80); // tokens per 1 ether with 20% discount
  uint public tokenRate15 = tokenRate.mul(100).div(85); // tokens per 1 ether with 15% discount
  uint public tokenRate10 = tokenRate.mul(100).div(90); // tokens per 1 ether with 10% discount
  uint public tokenRate5 = tokenRate.mul(100).div(95); // tokens per 1 ether with 5% discount

  /**
    * @dev Constructor that gives msg.sender all of existing tokens.
    */
  function MBEToken(address tokenOwner, address founder, address bounty) public {
    totalSupply = INITIAL_SUPPLY;
    balances[tokenOwner] += INITIAL_SUPPLY / 100 * 85;
    balances[founder] += INITIAL_SUPPLY / 100 * 10;
    balances[bounty] += INITIAL_SUPPLY / 100 * 5;
    tokenWallet = tokenOwner;
    founderWallet = founder;
    bountyWallet = bounty;
    Transfer(0x0, tokenOwner, balances[tokenOwner]);
    Transfer(0x0, founder, balances[founder]);
    Transfer(0x0, bounty, balances[bounty]);
  }
  
  function setupTokenRate(uint newTokenRate) public onlyOwner {
    tokenRate = newTokenRate;
    tokenRate30 = tokenRate.mul(100).div(70); // tokens per 1 ether with 30% discount
    tokenRate20 = tokenRate.mul(100).div(80); // tokens per 1 ether with 20% discount
    tokenRate15 = tokenRate.mul(100).div(85); // tokens per 1 ether with 15% discount
    tokenRate10 = tokenRate.mul(100).div(90); // tokens per 1 ether with 10% discount
    tokenRate5 = tokenRate.mul(100).div(95); // tokens per 1 ether with 5% discount
  }
  
  function setupFinal(uint finalDate) public onlyOwner returns(bool) {
    endICOStage5 = finalDate;
    return true;
  }

  function sellManually(address _to, uint amount) public onlyOwner returns(bool) {
    uint tokens = calcTokens(amount);
    uint256 balance = balanceOf(tokenWallet);
    if (balance < tokens) {
      sendTokens(_to, balance);
    } else {
      sendTokens(_to, tokens);
    }
    return true;
  }

  function () payable public {
    if (!isTokenSale()) revert();
    buyTokens(msg.value);
  }
  
  function isTokenSale() public view returns (bool) {
    if (now >= startPreICO && now < endICOStage5) {
      return true;
    } else {
      return false;
    }
  }

  function buyTokens(uint amount) internal {
    uint tokens = calcTokens(amount);  
    safeSend(tokens);
  }
  
  function calcTokens(uint amount) public view returns(uint) {
    uint rate = extraRate(amount, tokenRate);
    uint tokens = amount.mul(rate);
    if (now >= startPreICO && now < endPreICO) {
      rate = extraRate(amount, tokenRate30);
      tokens = amount.mul(rate);
      return tokens;
    } else if (now >= startICOStage1 && now < endICOStage1) {
      rate = extraRate(amount, tokenRate20);
      tokens = amount.mul(rate);
      return tokens;
    } else if (now >= startICOStage2 && now < endICOStage2) {
      rate = extraRate(amount, tokenRate15);
      tokens = amount.mul(rate);
      return tokens;
    } else if (now >= startICOStage3 && now < endICOStage3) {
      rate = extraRate(amount, tokenRate10);
      tokens = amount.mul(rate);
      return tokens;
    } else if (now >= startICOStage4 && now < endICOStage4) {
      rate = extraRate(amount, tokenRate5);
      tokens = amount.mul(rate);
      return tokens;
    } else if (now >= startICOStage5 && now < endICOStage5) {
      return tokens;
    }
  }

  function extraRate(uint amount, uint rate) public pure returns (uint) {
    return ( ( rate * 10 ** 20 ) / ( 100 - extraDiscount(amount) ) ) / ( 10 ** 18 );
  }

  function extraDiscount(uint amount) public pure returns(uint) {
    if ( 3 ether <= amount && amount <= 5 ether ) {
      return 5;
    } else if ( 5 ether < amount && amount <= 10 ether ) {
      return 7;
    } else if ( 10 ether < amount && amount <= 20 ether ) {
      return 10;
    } else if ( 20 ether < amount ) {
      return 15;
    }
    return 0;
  }

  function safeSend(uint tokens) private {
    uint256 balance = balanceOf(tokenWallet);
    if (balance < tokens) {
      uint toReturn = tokenRate.mul(tokens.sub(balance));
      sendTokens(msg.sender, balance);
      msg.sender.transfer(toReturn);
      multisig.transfer(msg.value.sub(toReturn));
      raisedEthers += msg.value.sub(toReturn);
    } else {
      sendTokens(msg.sender, tokens);
      multisig.transfer(msg.value);
      raisedEthers += msg.value;
    }
  }

  function sendTokens(address _to, uint tokens) private {
    balances[tokenWallet] = balances[tokenWallet].sub(tokens);
    balances[_to] += tokens;
    Transfer(tokenWallet, _to, tokens);
    currentFundrise += tokens;
  }
}
function MBEToken(address tokenOwner, address founder, address bounty) public {
  totalSupply = INITIAL_SUPPLY;
  balances[tokenOwner] += INITIAL_SUPPLY / 100 * 85;
  balances[founder] += INITIAL_SUPPLY / 100 * 10;
  balances[bounty] += INITIAL_SUPPLY / 100 * 5;
  tokenWallet = tokenOwner;
  founderWallet = founder;
  bountyWallet = bounty;
  Transfer(0x0, tokenOwner, balances[tokenOwner]);
  Transfer(0x0, founder, balances[founder]);
  Transfer(0x0, bounty, balances[bounty]);
}

/**
 * @dev Constructor that gives msg.sender all of existing tokens.
 */