contract SMRTCoin is StandardToken {
    string public constant name = "SMRT";
    uint public constant decimals = 18;
    string public constant symbol = "SMRT";
    using SafeMath for uint256; uint256 public weiRaised = 0; address depositWalletAddress; 
    event Buy(address _from, uint256 _ethInWei, string userId); 
    
    function SMRTCoin()public {
        owner = msg.sender;
        totalSupply = 600000000 * (10 ** decimals);
        preICOReserveTokens = 90000000 * (10 ** decimals);
        icoReserveTokens = 210000000 * (10 ** decimals);
        depositWalletAddress = 0x85a98805C17701504C252eAAB99f60C7c204A785; //TODO change
        businessReserveAddress = 0x73FEC20272a555Af1AEA4bF27D406683632c2a8c; 
        balances[owner] = totalSupply;
        emit Transfer(address(0), owner, totalSupply);
        startPreSale = now; //TODO update 1521900000 24 march 14 00 00 UTC
        endPreSale = 1524319200; //21 April 14 00 00 utc
        startSale = endPreSale + 1;
        endSale = startSale + 30 days;
    }
    function ()public {
        revert();
    }
    /**
   * This will be called by adding data to represnet data.
   */
    function buy(string userId)public payable whenNotPaused {
        require(msg.value > 0);
        require(msg.sender != address(0));
        weiRaised += msg.value;
        forwardFunds();
        emit Buy(msg.sender, msg.value, userId);
    }
    /**
   * This function will called by only distributors to send tokens by calculating from offchain listners
   */
    function getBonustokens(uint256 tokens)internal returns(uint256 bonusTokens) {
        require(now <= endSale);
        uint256 bonus;
        if (now <= endPreSale) {
            bonus = 50;
        } else if (now < startSale + 1 weeks) {
            bonus = 10;
        } else if (now < startSale + 2 weeks) {
            bonus = 5;
        }

        bonusTokens = ((tokens / 100) * bonus);
    }
    function CrowdSale(address recieverAddress, uint256 tokens)public onlyOwner {
        tokens =  tokens.add(getBonustokens(tokens));
        uint256 tokenLimit = (tokens.mul(20)).div(100); //as 20 becuase its 10 percnet of total
        incentiveTokensLimit  = incentiveTokensLimit.add(tokenLimit);
        if (now <= endPreSale && preICOReserveTokens >= tokens) {
            preICOReserveTokens = preICOReserveTokens.sub(tokens);
            transfer(businessReserveAddress, tokens);
            transfer(recieverAddress, tokens);
        } else if (now < endSale && icoReserveTokens >= tokens) {
            icoReserveTokens = icoReserveTokens.sub(tokens);
            transfer(businessReserveAddress, tokens);
            transfer(recieverAddress, tokens);
        }
        else{ 
            revert();
        }
    }
    /**
  * @dev Determines how ETH is stored/forwarded on purchases.
  */
    function forwardFunds()internal {
        depositWalletAddress.transfer(msg.value);
    }
    function changeDepositWalletAddress(address newDepositWalletAddr)external onlyOwner {
        require(newDepositWalletAddr != 0);
        depositWalletAddress = newDepositWalletAddr;
    }
    function updateSaleTime(uint256 _startSale, uint256 _endSale)external onlyOwner {
        startSale = _startSale;
        endSale = _endSale;
    }

 

}
function forwardFunds()internal {
        depositWalletAddress.transfer(msg.value);
    }
/**
 * @dev Determines how ETH is stored/forwarded on purchases.
 */