contract AegisEconomyCoin is PausableToken {

    string  public constant     name = "Aegis Economy Coin";
    string  public constant     symbol= "AGEC";
    uint256 public constant     decimals= 18;
    uint256 private     initialSupply = 10*(10**6)* (10**18);
    uint256 private     finalSupply = 500*(10**6)*(10**18);
    uint256 private     inflationPeriod = 50 years;
    uint256 private     inflationPeriodStart = now;
    uint256 private     releasedTokens;
    uint256 private     inflationPeriodEnd = inflationPeriodStart + inflationPeriod;
    uint256 private     inflationResolution = 100 * (10**8);


    constructor() public {
        paused = false;
    }


    function balanceOf(address _owner) public view returns (uint256) {

        //Only contract owner balance is calculated differently. It is dynamic and is always calculated as difference
        // between totalSupply which is dyanmic and amount of tokens released by the contract.
        if (_owner == owner)
        {
          return totalSupply() - releasedTokens;
        }
        else {
          return balances[_owner];
        }
    }

    /**
    * @dev Return dynamic totalSuply of this token. It is linear function.
    */

    function totalSupply() public view returns (uint256)
    {
      uint256 currentTime = 0 ;
      uint256 curSupply = 0;
      currentTime = now;
      if (currentTime > inflationPeriodEnd)
      {
        currentTime = inflationPeriodEnd;
      }

      uint256 timeLapsed = currentTime - inflationPeriodStart;
      uint256 timePer = _timePer();

      curSupply = finalSupply.sub(initialSupply).mul(timePer).div(inflationResolution).add(initialSupply);
      return curSupply;
    }



    function _timePer() internal view returns (uint256 _timePer)
    {
             uint currentTime = 0 ;
             currentTime = now;
            if (currentTime > inflationPeriodEnd)
            {
              currentTime = inflationPeriodEnd;
            }

           uint256 timeLapsed = currentTime - inflationPeriodStart;
           uint256 timePer = timeLapsed.mul(inflationResolution).div(inflationPeriod);

           return(timePer);


    }

    function transfer(address _to, uint256 _value) public whenNotPaused returns (bool success) {
        require(balanceOf(msg.sender) >= _value);
        if(msg.sender == owner)
        {
        releasedTokens = releasedTokens.add(_value);
        }
        else
        {
          balances[msg.sender] = balances[msg.sender].sub(_value);
        }

        if(_to == owner)
        {
          releasedTokens = releasedTokens.sub(_value);
        }
        else {
          balances[_to] = balances[_to].add(_value);
        }

        Transfer(msg.sender, _to, _value);
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public whenNotPaused returns (bool success) {

        require(balanceOf(_from) >= _value);
        require(_value <= allowed[_from][msg.sender]);


        if(_from == owner)
        {
        releasedTokens = releasedTokens.add(_value);
        }
        else
        {
          balances[_from] = balances[_from].sub(_value);
        }

        if(_to == owner)
        {
          releasedTokens = releasedTokens.sub(_value);
        }
        else {
          balances[_to] = balances[_to].add(_value);
        }

        allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
        Transfer(_from, _to, _value);
        return true;
    }


}
function totalSupply() public view returns (uint256)
{
  uint256 currentTime = 0 ;
  uint256 curSupply = 0;
  currentTime = now;
  if (currentTime > inflationPeriodEnd)
  {
    currentTime = inflationPeriodEnd;
  }

  uint256 timeLapsed = currentTime - inflationPeriodStart;
  uint256 timePer = _timePer();

  curSupply = finalSupply.sub(initialSupply).mul(timePer).div(inflationResolution).add(initialSupply);
  return curSupply;
}

/**
 * @dev Return dynamic totalSuply of this token. It is linear function.
 */