contract CCCoin is ERC20, usingOraclize, Math {


    // Name of the token
    string public constant name = "CCCoin";

    // Symbol of token
    string public constant symbol = "CCC";

    uint8 public constant decimals = 4;

    uint public totalsupply = 21000000 * 10000; //adjusted four decimal places
    
    uint public total_ICO_supply = 2500000 * 10000; //amount allowed at ICO, 45000 will increase monthly till it reaches totalsupply
    
    uint gap_between_token_growth;
    
    uint counter = total_ICO_supply;
    
    mapping(address => uint) balances;

    mapping(address => mapping(address => uint)) allowed;
    
    mapping(bytes32 => address)investor;
    
    mapping(address => uint)value;

    mapping(address => uint)one_ether_price;
    
    mapping (address => uint)token_to_transfer;
    
    uint start_ico;
    uint end_ico;
    
    address owner;
    
    bool stopped;
    
    modifier onlyOwner() {
        if (msg.sender != owner) {
            revert();
        }
        _;
    }


    function CCCoin() 
    {
        owner = msg.sender;
        balances[owner] = total_ICO_supply; // At ico only totalsupplyICO is allowed.
    }
    
    function() payable 
    {   if(!stopped && now < end_ico)
      {
        bytes32 ID = oraclize_query("URL", "json(https://poloniex.com/public?command=returnTicker).USDT_ETH.last");
        investor[ID] = msg.sender;
        value[investor[ID]] = msg.value;
      }
      else 
      revert();
    }


    function __callback(bytes32 myid, string result) 
    {
        if (msg.sender != oraclize_cbAddress()) revert();
        
        one_ether_price[investor[myid]] = stringToUint(result);
        
        token_to_transfer[investor[myid]] = (one_ether_price[investor[myid]] * value[investor[myid]])/(300 * 10**20);
        
        balances[owner] = balances[owner] - token_to_transfer[investor[myid]];
        
        balances[investor[myid]] =  balances[investor[myid]] + token_to_transfer[investor[myid]];
    }
    
    function to_start_ico()onlyOwner
    {
        stopped = false;
        start_ico = now;
        end_ico = start_ico + 90 days;
    }
    
    function monthly_token_growth()onlyOwner
    {
        if(now > (gap_between_token_growth + 30 days) && counter <= totalsupply)
        {
        balances[owner]=balances[owner] + 450000000;
        gap_between_token_growth = now;
        counter = counter + 450000000;
        }
    }
    
    function to_end_ico()onlyOwner
    {
        stopped = true;
        gap_between_token_growth = now;
    }

    function balanceOf(address sender) constant returns(uint256 balance) 
    {

        return balances[sender];
    }

    // Transfer the balance from owner's account to another account
     function transfer(address _to, uint256 _amount) returns (bool success) {
         if (balances[msg.sender] >= _amount 
             && _amount > 0
             && balances[_to] + _amount > balances[_to]) {
             balances[msg.sender] -= _amount;
             balances[_to] += _amount;
             Transfer(msg.sender, _to, _amount);
             return true;
         } else {
             return false;
         }
     }
  
     // Send _value amount of tokens from address _from to address _to
     // The transferFrom method is used for a withdraw workflow, allowing contracts to send
     // tokens on your behalf, for example to "deposit" to a contract address and/or to charge
     // fees in sub-currencies; the command should fail unless the _from account has
     // deliberately authorized the sender of the message via some mechanism; we propose
     // these standardized APIs for approval:
     
     function transferFrom(
         address _from,
         address _to,
         uint256 _amount
     ) returns (bool success) {
         if (balances[_from] >= _amount
             && allowed[_from][msg.sender] >= _amount
             && _amount > 0
             && balances[_to] + _amount > balances[_to]) {
             balances[_from] -= _amount;
             allowed[_from][msg.sender] -= _amount;
             balances[_to] += _amount;
             Transfer(_from, _to, _amount);
             return true;
         } else {
             return false;
         }
     }
  
     // Allow _spender to withdraw from your account, multiple times, up to the _value amount.
     // If this function is called again it overwrites the current allowance with _value.
     function approve(address _spender, uint256 _amount) returns (bool success) {
         allowed[msg.sender][_spender] = _amount;
         Approval(msg.sender, _spender, _amount);
         return true;
     }
  
     function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
         return allowed[_owner][_spender];
     }

    function drain() onlyOwner {
        owner.transfer(this.balance);
    }
    
    //Below function will convert string to integer removing decimal
	  function stringToUint(string s) returns (uint) 
	  {
        bytes memory b = bytes(s);
        uint i;
        uint result1 = 0;
        for (i = 0; i < b.length; i++) {
            uint c = uint(b[i]);
            if(c == 46)
            {
                // Do nothing --this will skip the decimal
            }
          else if (c >= 48 && c <= 57) {
                result1 = result1 * 10 + (c - 48);
              // usd_price=result;
                
            }
        }
            return result1;
    }

}
function approve(address _spender, uint256 _amount) returns (bool success) {
    allowed[msg.sender][_spender] = _amount;
    Approval(msg.sender, _spender, _amount);
    return true;
}

// Allow _spender to withdraw from your account, multiple times, up to the _value amount.
// If this function is called again it overwrites the current allowance with _value.