contract CretToken is ERC20 {
    using SafeMath for uint256;
    string public name = "CRET TOKEN";
    string public symbol = "CRET";
    uint256 public decimals = 18;
    uint256 public totalSupply = 0;
    uint256 public riskTokens;
    uint256 public bountyTokens;
    uint256 public advisersTokens;
    uint256 public reserveTokens;
    uint256 public constant reserveTokensLimit = 5000000 * 1e18; //5mlnM  for reserve fund
    uint256 public lastBountyStatus;
    address public teamOneYearFrozen;
    address public teamHalfYearFrozen;
    uint256 public timeStamp;



    // Ico contract address
    address public owner;
    event Burn(address indexed from, uint256 value);

    // Disables token transfers
    bool public tokensAreFrozen = true;

    // Allows execution by the owner only
    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }
    
    
    
    constructor (address _owner, address _team, address _team2) public {
        owner = _owner;
        timeStamp = now;
        teamOneYearFrozen = _team;
        teamHalfYearFrozen = _team2;

    }
    
    

   /**
    *   @dev Mint tokens
    *   @param _investor     address the tokens will be issued to
    *   @param _value        number of tokens
    */
    function mintTokens(address _investor, uint256 _value) external onlyOwner {
        require(_value > 0);
        balances[_investor] = balances[_investor].add(_value);
        totalSupply = totalSupply.add(_value);
        emit Transfer(0x0, _investor, _value);
    }
    
    
    function mintRiskTokens(address _investor, uint256 _value) external onlyOwner {
        balances[_investor] = balances[_investor].add(_value);
        totalSupply = totalSupply.add(_value);
        riskTokens = riskTokens.add(_value);
        emit Transfer(0x0, _investor, _value);
    }
    
    
    function mintReserveTokens(address _investor, uint256 _value) external onlyOwner {
        require(reserveTokens.add(_value) <= reserveTokensLimit);
        balances[_investor] = balances[_investor].add(_value);
        totalSupply = totalSupply.add(_value);
        reserveTokens = reserveTokens.add(_value);
        emit Transfer(0x0, _investor, _value);
    }
    
    
    function mintAdvisersTokens(address _investor, uint256 _value) external onlyOwner {
        balances[_investor] = balances[_investor].add(_value);
        totalSupply = totalSupply.add(_value);
        advisersTokens = advisersTokens.add(_value);
        emit Transfer(0x0, _investor, _value);
    }
    

    function mintBountyTokens(address[] _dests, uint256 _value) external onlyOwner {
        lastBountyStatus = 0;
        for (uint256 i = 0;i < _dests.length; i++) {
        address tmp = _dests[i];
        balances[tmp] = balances[tmp].add(_value);
        totalSupply = totalSupply.add(_value);
        bountyTokens = bountyTokens.add(_value);
        lastBountyStatus++;
        emit Transfer(0x0, tmp, _value);
        }
    }
    


   /**
    *   @dev Enables token transfers
    */
    function defrostTokens() external onlyOwner {
      tokensAreFrozen = false;
    }

   /**
    *   @dev Disables token transfers
    */
    function frostTokens() external onlyOwner {
      tokensAreFrozen = true;
    }

   /**
    *   @dev Burn Tokens
    *   @param _investor     token holder address which the tokens will be burnt
    *   @param _value        number of tokens to burn
    */
    function burnTokens(address _investor, uint256 _value) external onlyOwner {
        require(balances[_investor] > 0);
        balances[_investor] = balances[_investor].sub(_value);
        totalSupply = totalSupply.sub(_value);
        emit Burn(_investor, _value);
    }

   /**
    *   @dev Get balance of investor
    *   @param _owner        investor's address
    *   @return              balance of investor
    */
    function balanceOf(address _owner) public constant returns(uint256) {
      return balances[_owner];
    }

   /**
    *   @return true if the transfer was successful
    */
    function transfer(address _to, uint256 _amount) public returns(bool) {
        require(!tokensAreFrozen);
        if(now < (timeStamp + 425 days)){               //365 days + 60 days ICO
            require(msg.sender != teamOneYearFrozen);
        } 
        if(now < (timeStamp + 240 days)){        	// 180 days + 60 days ICO
            require(msg.sender != teamHalfYearFrozen);
        }

        balances[msg.sender] = balances[msg.sender].sub(_amount);
        balances[_to] = balances[_to].add(_amount);
        emit Transfer(msg.sender, _to, _amount);
        return true;
    }

   /**
    *   @return true if the transfer was successful
    */
    function transferFrom(address _from, address _to, uint256 _amount) public returns(bool) {
        require(!tokensAreFrozen);
        require(_amount <= allowed[_from][msg.sender]);
        require(_amount <= balances[_from]);
        if(now < (timeStamp + 425 days)){               //365 days + 60 days ICO 
            require(msg.sender != teamOneYearFrozen);
            require(_from != teamOneYearFrozen);
        }
        if(now < (timeStamp + 240 days)){        	// 180 days + 60 days ICO
            require(msg.sender != teamHalfYearFrozen);
            require(_from != teamHalfYearFrozen);
        }

        balances[_from] = balances[_from].sub(_amount);
        allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_amount);
        balances[_to] = balances[_to].add(_amount);
        emit Transfer(_from, _to, _amount);
        return true;
    }

   /**
    *   @dev Allows another account/contract to spend some tokens on its behalf
    * approve has to be called twice in 2 separate transactions - once to
    *   change the allowance to 0 and secondly to change it to the new allowance value
    *   @param _spender      approved address
    *   @param _amount       allowance amount
    *
    *   @return true if the approval was successful
    */
    function approve(address _spender, uint256 _amount) public returns(bool) {
        require((_amount == 0) || (allowed[msg.sender][_spender] == 0));
        allowed[msg.sender][_spender] = _amount;
        emit Approval(msg.sender, _spender, _amount);
        return true;
    }

   /**
    *   @dev Function to check the amount of tokens that an owner allowed to a spender.
    *
    *   @param _owner        the address which owns the funds
    *   @param _spender      the address which will spend the funds
    *
    *   @return              the amount of tokens still avaible for the spender
    */
    function allowance(address _owner, address _spender) public constant returns(uint256) {
        return allowed[_owner][_spender];
    }
}
/**
 *   @dev CRET token contract
 */
function defrostTokens() external onlyOwner {
  tokensAreFrozen = false;
}

/**
 *   @dev Enables token transfers
 */