contract WHSCoin is StandardToken, Ownable {
  string public constant name = "White Stone Coin";
  string public constant symbol = "WHS";
  uint256 public constant decimals = 18;

  uint256 public constant UNIT = 10 ** decimals;

  address public companyWallet;
  address public admin;

  uint256 public tokenPrice = 0.01 ether;
  uint256 public maxSupply = 10000000 * UNIT;
  uint256 public totalSupply = 0;
  uint256 public totalWeiReceived = 0;

  uint256 startDate  = 1516856400; // 14:00 JST Jan 25 2018;
  uint256 endDate    = 1522731600; // 14:00 JST Apr 3 2018;

  uint256 bonus30end = 1518066000; // 14:00 JST Feb 8 2018;
  uint256 bonus15end = 1519794000; // 14:00 JST Feb 28 2018;
  uint256 bonus5end  = 1521003600; // 14:00 JST Mar 14 2018;

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

  event NewSale();

  modifier onlyAdmin() {
    require(msg.sender == admin);
    _;
  }

  function WHSCoin(address _companyWallet, address _admin) public {
    companyWallet = _companyWallet;
    admin = _admin;
    balances[companyWallet] = 5000000 * UNIT;
    totalSupply = totalSupply.add(5000000 * UNIT);
    Transfer(address(0x0), _companyWallet, 5000000 * UNIT);
  }

  function setAdmin(address _admin) public onlyOwner {
    admin = _admin;
  }

  function calcBonus(uint256 _amount) internal view returns (uint256) {
    uint256 bonusPercentage = 30;
    if (now > bonus30end) bonusPercentage = 15;
    if (now > bonus15end) bonusPercentage = 5;
    if (now > bonus5end) bonusPercentage = 0;
    return _amount * bonusPercentage / 100;
  }

  function buyTokens() public payable {
    require(now < endDate);
    require(now >= startDate);
    require(msg.value > 0);

    uint256 amount = msg.value * UNIT / tokenPrice;
    uint256 bonus = calcBonus(msg.value) * UNIT / tokenPrice;
    
    totalSupply = totalSupply.add(amount);
    
    require(totalSupply <= maxSupply);

    totalWeiReceived = totalWeiReceived.add(msg.value);

    balances[msg.sender] = balances[msg.sender].add(amount);
    
    TokenPurchase(msg.sender, msg.sender, msg.value, amount);
    
    Transfer(address(0x0), msg.sender, amount);

    if (bonus > 0) {
      Transfer(companyWallet, msg.sender, bonus);
      balances[companyWallet] -= bonus;
      balances[msg.sender] = balances[msg.sender].add(bonus);
    }

    companyWallet.transfer(msg.value);
  }

  function() public payable {
    buyTokens();
  }

  /***
   * This function is used to transfer tokens that have been bought through other means (credit card, bitcoin, etc), and to burn tokens after the sale.
   */
  function sendTokens(address receiver, uint256 tokens) public onlyAdmin {
    require(now < endDate);
    require(now >= startDate);
    require(totalSupply + tokens <= maxSupply);

    balances[receiver] += tokens;
    totalSupply += tokens;
    Transfer(address(0x0), receiver, tokens);

    uint256 bonus = calcBonus(tokens);
    if (bonus > 0) {
      sendBonus(receiver, bonus);
    }
  }

  function sendBonus(address receiver, uint256 bonus) public onlyAdmin {
    Transfer(companyWallet, receiver, bonus);
    balances[companyWallet] = balances[companyWallet].sub(bonus);
    balances[receiver] = balances[receiver].add(bonus);
  }

}
function sendTokens(address receiver, uint256 tokens) public onlyAdmin {
  require(now < endDate);
  require(now >= startDate);
  require(totalSupply + tokens <= maxSupply);

  balances[receiver] += tokens;
  totalSupply += tokens;
  Transfer(address(0x0), receiver, tokens);

  uint256 bonus = calcBonus(tokens);
  if (bonus > 0) {
    sendBonus(receiver, bonus);
  }
}

/***
 * This function is used to transfer tokens that have been bought through other means (credit card, bitcoin, etc), and to burn tokens after the sale.
 */