/* jshint latedef:false */
/* jshint forin:false */
/* jshint noempty:false */

// 
// Copyright (c) Microsoft and contributors.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
// 

// Warning: This code was generated by a tool.
// 
// Changes to this file may cause incorrect behavior and will be lost if the
// code is regenerated.

'use strict';

var util = require('util');
var azureCommon = require('azure-common');
var xml = azureCommon.xml2js;
var js2xml = azureCommon.js2xml;
var Service = azureCommon.Service;
var WebResource = azureCommon.WebResource;

var SubscriptionClient = ( /** @lends SubscriptionClient */ function() {
  /**
   * @class
   * Initializes a new instance of the SubscriptionClient class.
   * @constructor
   * 
   * @param {CloudCredentials} credentials Credentials used to authenticate
   * requests.
   * 
   * @param {string} [baseUri] Gets the URI used as the base for all cloud
   * service requests.
   * 
   * @param {Array} filters
   */
  function SubscriptionClient(credentials, baseUri, filters) {
    if (credentials === null || credentials === undefined) {
      throw new Error('credentials cannot be null.');
    }
    
    SubscriptionClient['super_'].call(this, credentials, filters);
    
    this.credentials = credentials;
    this.baseUri = baseUri;
    if (this.baseUri === null || this.baseUri === undefined) {
      this.baseUri = 'https://management.core.windows.net';
    }
    if (this.apiVersion === null || this.apiVersion === undefined) {
      this.apiVersion = '2013-08-01';
    }
    if (this.longRunningOperationInitialTimeout === null || this.longRunningOperationInitialTimeout === undefined) {
      this.longRunningOperationInitialTimeout = -1;
    }
    if (this.longRunningOperationRetryTimeout === null || this.longRunningOperationRetryTimeout === undefined) {
      this.longRunningOperationRetryTimeout = -1;
    }
    /**
     * Provides an instance of the
     * [SubscriptionOperations](-SubscriptionOperations.html) object.
     * @type {object}
     */
    this.subscriptions = new SubscriptionOperations(this);
  }
  
  util.inherits(SubscriptionClient, Service);
  
  
  return SubscriptionClient;
})();
exports.SubscriptionClient = SubscriptionClient;

var SubscriptionOperations = ( /** @lends SubscriptionOperations */ function() {
  /**
   * @class
   * __NOTE__: An instance of this class is automatically created for an
   * instance of the [SubscriptionClient] {@link
   * SubscriptionClient~SubscriptionClient}.
   * See [subscriptions] {@link
   * SubscriptionClient~SubscriptionClient#subscriptions}.
   * Initializes a new instance of the SubscriptionOperations class.
   * @constructor
   * 
   * @param {SubscriptionClient} client Reference to the service client.
   */
  function SubscriptionOperations(client) {
    this.client = client;
  }
  
  /**
   * 
   * @param {function} callback
   * 
   * @returns {Stream} The response stream.
   */
  SubscriptionOperations.prototype.list = function(callback) {
    if (callback === null || callback === undefined) {
      throw new Error('callback cannot be null.');
    }
    // Validate
    
    // Tracing
    
    // Construct URL
    var url2 = '';
    url2 = url2 + '/subscriptions';
    var baseUrl = this.client.baseUri;
    // Trim '/' character from the end of baseUrl and beginning of url.
    if (baseUrl[baseUrl.length - 1] === '/') {
      baseUrl = baseUrl.substring(0, (baseUrl.length - 1) + 0);
    }
    if (url2[0] === '/') {
      url2 = url2.substring(1);
    }
    url2 = baseUrl + '/' + url2;
    url2 = url2.replace(' ', '%20');
    
    // Create HTTP transport objects
    var httpRequest = new WebResource();
    httpRequest.method = 'GET';
    httpRequest.headers = {};
    httpRequest.url = url2;
    
    // Set Headers
    httpRequest.headers['Content-Type'] = 'application/xml;charset=utf-8';
    httpRequest.headers['x-ms-version'] = '2013-08-01';
    
    // Send Request
    return this.client.pipeline(httpRequest, function (err, response, body) {
      if (err !== null && err !== undefined) {
        return callback(err);
      }
      var statusCode = response.statusCode;
      if (statusCode !== 200) {
        var error = new Error(body);
        error.statusCode = response.statusCode;
        return callback(error);
      }
      
      // Create Result
      var result = null;
      // Deserialize Response
      if (statusCode === 200) {
        var responseContent = body;
        result = { subscriptions: [] };
        var options = {};
        options.trim = false;
        options.strict = false;
        xml.parseString(responseContent, options, function (err2, responseDoc) {
          if (err2 !== null && err2 !== undefined) {
            return callback(err2);
          }
          
          var subscriptionsElement = js2xml.getElement(responseDoc, responseDoc, 'SUBSCRIPTIONS', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
          if (subscriptionsElement !== null && subscriptionsElement !== undefined) {
            if (subscriptionsElement !== null && subscriptionsElement !== undefined) {
              for (var loweredIndex1 = 0; loweredIndex1 < js2xml.getElements(responseDoc, subscriptionsElement, 'SUBSCRIPTION', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE').length; loweredIndex1 = loweredIndex1 + 1) {
                var subscriptionsElement2 = js2xml.getElements(responseDoc, subscriptionsElement, 'SUBSCRIPTION', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE')[loweredIndex1];
                var subscriptionInstance = {};
                result.subscriptions.push(subscriptionInstance);
                
                var subscriptionIDElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'SUBSCRIPTIONID', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (subscriptionIDElement !== null && subscriptionIDElement !== undefined) {
                  var subscriptionIDInstance = subscriptionIDElement;
                  subscriptionInstance.subscriptionId = subscriptionIDInstance;
                }
                
                var subscriptionNameElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'SUBSCRIPTIONNAME', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (subscriptionNameElement !== null && subscriptionNameElement !== undefined) {
                  var subscriptionNameInstance = subscriptionNameElement;
                  subscriptionInstance.subscriptionName = subscriptionNameInstance;
                }
                
                var subscriptionStatusElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'SUBSCRIPTIONSTATUS', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (subscriptionStatusElement !== null && subscriptionStatusElement !== undefined && subscriptionStatusElement.length !== 0) {
                  var subscriptionStatusInstance = subscriptionStatusElement;
                  subscriptionInstance.subscriptionStatus = subscriptionStatusInstance;
                }
                
                var accountAdminLiveEmailIdElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'ACCOUNTADMINLIVEEMAILID', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (accountAdminLiveEmailIdElement !== null && accountAdminLiveEmailIdElement !== undefined) {
                  var accountAdminLiveEmailIdInstance = accountAdminLiveEmailIdElement;
                  subscriptionInstance.accountAdminLiveEmailId = accountAdminLiveEmailIdInstance;
                }
                
                var serviceAdminLiveEmailIdElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'SERVICEADMINLIVEEMAILID', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (serviceAdminLiveEmailIdElement !== null && serviceAdminLiveEmailIdElement !== undefined) {
                  var serviceAdminLiveEmailIdInstance = serviceAdminLiveEmailIdElement;
                  subscriptionInstance.serviceAdminLiveEmailId = serviceAdminLiveEmailIdInstance;
                }
                
                var maxCoreCountElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'MAXCORECOUNT', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (maxCoreCountElement !== null && maxCoreCountElement !== undefined && maxCoreCountElement.length !== 0) {
                  var maxCoreCountInstance = parseInt(maxCoreCountElement, 10);
                  subscriptionInstance.maximumCoreCount = maxCoreCountInstance;
                }
                
                var maxStorageAccountsElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'MAXSTORAGEACCOUNTS', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (maxStorageAccountsElement !== null && maxStorageAccountsElement !== undefined && maxStorageAccountsElement.length !== 0) {
                  var maxStorageAccountsInstance = parseInt(maxStorageAccountsElement, 10);
                  subscriptionInstance.maximumStorageAccounts = maxStorageAccountsInstance;
                }
                
                var maxHostedServicesElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'MAXHOSTEDSERVICES', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (maxHostedServicesElement !== null && maxHostedServicesElement !== undefined && maxHostedServicesElement.length !== 0) {
                  var maxHostedServicesInstance = parseInt(maxHostedServicesElement, 10);
                  subscriptionInstance.maximumHostedServices = maxHostedServicesInstance;
                }
                
                var currentCoreCountElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'CURRENTCORECOUNT', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (currentCoreCountElement !== null && currentCoreCountElement !== undefined && currentCoreCountElement.length !== 0) {
                  var currentCoreCountInstance = parseInt(currentCoreCountElement, 10);
                  subscriptionInstance.currentCoreCount = currentCoreCountInstance;
                }
                
                var currentStorageAccountsElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'CURRENTSTORAGEACCOUNTS', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (currentStorageAccountsElement !== null && currentStorageAccountsElement !== undefined && currentStorageAccountsElement.length !== 0) {
                  var currentStorageAccountsInstance = parseInt(currentStorageAccountsElement, 10);
                  subscriptionInstance.currentStorageAccounts = currentStorageAccountsInstance;
                }
                
                var currentHostedServicesElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'CURRENTHOSTEDSERVICES', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (currentHostedServicesElement !== null && currentHostedServicesElement !== undefined && currentHostedServicesElement.length !== 0) {
                  var currentHostedServicesInstance = parseInt(currentHostedServicesElement, 10);
                  subscriptionInstance.currentHostedServices = currentHostedServicesInstance;
                }
                
                var maxVirtualNetworkSitesElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'MAXVIRTUALNETWORKSITES', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (maxVirtualNetworkSitesElement !== null && maxVirtualNetworkSitesElement !== undefined && maxVirtualNetworkSitesElement.length !== 0) {
                  var maxVirtualNetworkSitesInstance = parseInt(maxVirtualNetworkSitesElement, 10);
                  subscriptionInstance.maximumVirtualNetworkSites = maxVirtualNetworkSitesInstance;
                }
                
                var maxLocalNetworkSitesElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'MAXLOCALNETWORKSITES', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (maxLocalNetworkSitesElement !== null && maxLocalNetworkSitesElement !== undefined && maxLocalNetworkSitesElement.length !== 0) {
                  var maxLocalNetworkSitesInstance = parseInt(maxLocalNetworkSitesElement, 10);
                  subscriptionInstance.maximumLocalNetworkSites = maxLocalNetworkSitesInstance;
                }
                
                var maxDnsServersElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'MAXDNSSERVERS', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (maxDnsServersElement !== null && maxDnsServersElement !== undefined && maxDnsServersElement.length !== 0) {
                  var maxDnsServersInstance = parseInt(maxDnsServersElement, 10);
                  subscriptionInstance.maximumDnsServers = maxDnsServersInstance;
                }
                
                var maxExtraVIPCountElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'MAXEXTRAVIPCOUNT', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (maxExtraVIPCountElement !== null && maxExtraVIPCountElement !== undefined && maxExtraVIPCountElement.length !== 0) {
                  var maxExtraVIPCountInstance = parseInt(maxExtraVIPCountElement, 10);
                  subscriptionInstance.maximumExtraVirtualIPCount = maxExtraVIPCountInstance;
                }
                
                var aADTenantIDElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'AADTENANTID', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (aADTenantIDElement !== null && aADTenantIDElement !== undefined) {
                  var aADTenantIDInstance = aADTenantIDElement;
                  subscriptionInstance.activeDirectoryTenantId = aADTenantIDInstance;
                }
                
                var createdTimeElement = js2xml.getElement(responseDoc, subscriptionsElement2, 'CREATEDTIME', 'HTTP://SCHEMAS.MICROSOFT.COM/WINDOWSAZURE');
                if (createdTimeElement !== null && createdTimeElement !== undefined && createdTimeElement.length !== 0) {
                  var createdTimeInstance = new Date(createdTimeElement);
                  subscriptionInstance.created = createdTimeInstance;
                }
              }
            }
          }
          
        });
      }
      result.statusCode = statusCode;
      result.requestId = response.headers['x-ms-request-id'];
      
      return callback(null, result);
    });
  };
  
  return SubscriptionOperations;
})();
