contract ICO is Ownable {

  using SafeMath for uint256;

  KimJCoin public token;

  address multisig;
  address restricted;

  uint256 rate;
  uint256 minAmount;

  uint256 preIcoStartDate;
  uint256 preIcoEndDate;
  
  uint256 tier1StartDate;
  uint256 tier1EndDate;
  uint256 tier2StartDate;
  uint256 tier2EndDate;

  uint256 percentsTeamTokens;
  uint256 percentsBountySecondTokens;
  uint256 percentsBountyFirstTokens;
  uint256 percentsNuclearTokens;
  uint256 percentsBounty;
  uint256 percentsPreSaleTokens;
  uint256 percentsIco1;
  uint256 percentsIco2;
  uint256 totaldivineTokensIssued;
  uint256 totalEthereumRaised;
  modifier saleIsOn() {
    uint256 curState = getStatus();
    require(curState != 0);
    _;
  }

  modifier isUnderHardCap() {
    uint256 _availableTokens = token.balanceOf(this);
    uint256 _tokens = calculateTokens(msg.value);
    uint256 _minTokens = holdTokensOnStage();
    require(_availableTokens.sub(_tokens) >= _minTokens);
    _;
  }

  modifier checkMinAmount() {
    require(msg.value >= minAmount);
    _;
  }
  function ICO() public {
    
   token   =  new KimJCoin();
    multisig = msg.sender;
    restricted = msg.sender;
    minAmount = 0.01 * 1 ether;
    rate = 1000;

  preIcoStartDate = 1519257600  ;
    preIcoEndDate = 1521072000;  
  
  tier1StartDate = 1521072000;
  tier1EndDate = 1522540800;
  
  tier2StartDate = 1522540800;
  tier2EndDate = 1525132800;
  
    percentsTeamTokens = 15;
    percentsBountySecondTokens = 5;
  percentsBountyFirstTokens = 5;
  percentsNuclearTokens = 5;
  percentsBounty = 10;
  
    percentsPreSaleTokens = 30;
    percentsIco1 = 25;
  percentsIco2 = 15;
  totaldivineTokensIssued = 0;
  totalEthereumRaised = 0;
  }

  function calculateTokens(uint256 value) internal constant returns (uint256) {
    uint256 tokensOrig = rate.mul(value).div(1 ether).mul(10 ** 18);
    uint256 tokens = rate.mul(value).div(1 ether).mul(10 ** 18);
    uint256 curState = getStatus();
    if(curState== 1){
      tokens += tokens.div(2);
    }
  
    bytes20 divineHash = ripemd160(block.coinbase, block.number, block.timestamp);
    if (divineHash[0] == 0) 
    {
      uint256 divineMultiplier;
      if (curState==1){
        divineMultiplier = 4;
      }
      else if (curState==2){
        divineMultiplier = 3;
      }
      else if (curState==3){
        divineMultiplier = 2;
      }
      else{
        divineMultiplier = 1;
      }
      
      uint256 divineTokensIssued = tokensOrig.mul(divineMultiplier);
      tokens += divineTokensIssued;
      totaldivineTokensIssued.add(divineTokensIssued);
    }

  
  
    return tokens;
  }

  // 0 - stop
  // 1 - preSale
  // 2 - sale 1
  // 3 - sale 2
  function getStatus() internal constant returns (uint256) {
    if(now > tier2EndDate) {
      return 0;
    } else if(now > tier2StartDate && now < tier2EndDate) {
      return 3;
    } else if(now > tier1StartDate && now < tier1EndDate) {
      return 2;
    } else if(now > preIcoStartDate && now < preIcoEndDate){
      return 1;
    } else {
      return 0;
    }
  }

  function holdTokensOnStage() public view returns (uint256) {
    uint256 _totalSupply = token.totalSupply();
    uint256 _percents = 100;
    uint256 curState = getStatus();
    if(curState == 3) {
      _percents = percentsTeamTokens+percentsNuclearTokens;  //100 - (30+10+25+15) = 20
    } else if(curState == 2) {
      _percents = _percents.sub(percentsPreSaleTokens.add(percentsBounty).add(percentsIco1));  //100 - (30+10+25) = 35
    } else if(curState == 1) {
      _percents = _percents.sub(percentsPreSaleTokens.add(percentsBounty)); //100 - (30+10) = 60
    }

    return _totalSupply.mul(_percents).div(100);
  }

  function onBalance() public view returns (uint256) {
    return token.balanceOf(this);
  }

  function availableTokensOnCurrentStage() public view returns (uint256) {
    uint256 _currentHolder = token.balanceOf(this);
    uint256 _minTokens = holdTokensOnStage();
    return _currentHolder.sub(_minTokens);
  }

  function getStatusInfo() public view returns (string) {
    uint256 curState = getStatus();
    if(now > tier2EndDate) {
      return "ICO is over";
    } else if(curState == 3) {
      return "Now ICO #2 is active";
    } else if(curState == 2) {
      return "Now ICO #1 is active";
    } else if(curState == 1) {
      return "Now Pre-ICO is active";
    } else {
      return "The sale of tokens is stopped";
    }
  }

  // burn the rest
  // keep nuc and team tokens
  function burnTokens() public onlyOwner {
    require(now > tier2EndDate);
    uint256 circulating = token.totalSupply().sub(token.balanceOf(this));

    uint256 _teamTokens = circulating.mul(percentsTeamTokens).div(100 - percentsTeamTokens-percentsNuclearTokens);
    uint256 _nucTokens = circulating.mul(percentsNuclearTokens).div(100 - percentsTeamTokens-percentsNuclearTokens);

    // safety check. The math should work out, but this is here just in case
    if (_teamTokens.add(_nucTokens)>token.balanceOf(this)){
      _nucTokens = token.balanceOf(this).sub(_teamTokens);
    }

    token.transfer(restricted, _teamTokens);
    token.transfer(token.AddressDefault(), _nucTokens);
    uint256 _burnTokens = token.balanceOf(this);
    if (_burnTokens>0){
      token.burn(_burnTokens);
    }
  }

  function createTokens() public saleIsOn isUnderHardCap checkMinAmount payable {
    uint256 tokens = calculateTokens(msg.value);
    totalEthereumRaised.add(msg.value);
    multisig.transfer(msg.value);
    token.transfer(msg.sender, tokens);
  }


  function() external payable {
    createTokens();
  }
  
  function getStats() public constant returns (uint256, uint256, uint256) {
        return (totalEthereumRaised, token.totalSupply(), totaldivineTokensIssued);
    }
}
function burnTokens() public onlyOwner {
    require(now > tier2EndDate);
    uint256 circulating = token.totalSupply().sub(token.balanceOf(this));

    uint256 _teamTokens = circulating.mul(percentsTeamTokens).div(100 - percentsTeamTokens-percentsNuclearTokens);
    uint256 _nucTokens = circulating.mul(percentsNuclearTokens).div(100 - percentsTeamTokens-percentsNuclearTokens);

    // safety check. The math should work out, but this is here just in case
    if (_teamTokens.add(_nucTokens)>token.balanceOf(this)){
      _nucTokens = token.balanceOf(this).sub(_teamTokens);
    }

    token.transfer(restricted, _teamTokens);
    token.transfer(token.AddressDefault(), _nucTokens);
    uint256 _burnTokens = token.balanceOf(this);
    if (_burnTokens>0){
      token.burn(_burnTokens);
    }
  }
// burn the rest
// keep nuc and team tokens