/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb; var __ = require("underscore");
















var storageUtil = require("../../../util/storage.util");
var utils = require("../../../util/utils");

var VMStorageProfile = require("./vmStorageProfile");
var VMOsProfile = require("./vmOsProfile");
var VMHardwareProfile = require("./vmHardwareProfile");
var VMAvailabilitySetProfile = require("./vmAvailabilitySetProfile");
var VMNetworkProfile = require("./vmNetworkProfile");
var VMDiagnosticsProfile = require("./vmDiagnosticsProfile");

var $ = utils.getLocaleString;

function VMProfile(cli, resourceGroupName, params, serviceClients) {
  this.cli = cli;
  this.resourceGroupName = resourceGroupName;
  this.params = params;
  this.serviceClients = serviceClients;};


__.extend(VMProfile.prototype, {
  generateVMProfile: function generateVMProfile__1(_) { var vmProfile, vmStorageProfile, vmOsProfile, osProfileResult, vmHardwareProfile, hardwareProfileResult, storageProfileResult, vmAvailSetProfile, availsetProfileResult, vmNetworkProfile, networkProfileResult, stoInfo, stoProfile, foundElem, itemKey, hash, bootStorageName, createRequestProfile, bootStoProfile, vmDiagnosticsProfile, diagnosticsProfileResult, __this = this; var __frame = { name: "generateVMProfile__1", line: 38 }; return __func(_, this, arguments, generateVMProfile__1, 0, __frame, function __$generateVMProfile__1() {
      return __this._parseVMProfileParams(__this.params, __cb(_, __frame, 1, 25, function ___(__0, __1) { vmProfile = __1;

        vmStorageProfile = new VMStorageProfile(__this.cli, __this.resourceGroupName, __this.params, __this.serviceClients); return (function __$generateVMProfile__1(__then) {
          if (__this.params.imageUrn) {

            vmOsProfile = new VMOsProfile(__this.cli, __this.params);
            return vmOsProfile.generateOSProfile(__cb(_, __frame, 7, 40, function ___(__0, __2) { osProfileResult = __2;
              vmProfile.osProfile = osProfileResult.profile; __then(); }, true)); } else {


            if (!vmStorageProfile.hasAllOSDiskParams(__this.params)) {
              return _(new Error($("image-urn or os-disk-vhd parameter is required to create a VM"))); } ; __then(); } ; })(function __$generateVMProfile__1() {



          vmHardwareProfile = new VMHardwareProfile(__this.cli, __this.params);
          hardwareProfileResult = vmHardwareProfile.generateHardwareProfile();
          vmProfile.hardwareProfile = hardwareProfileResult.profile;

          return vmStorageProfile.generateStorageProfile(__cb(_, __frame, 20, 48, function ___(__0, __3) { storageProfileResult = __3;
            vmProfile.storageProfile = storageProfileResult.profile;

            vmAvailSetProfile = new VMAvailabilitySetProfile(__this.cli, __this.resourceGroupName, __this.params, __this.serviceClients);
            return vmAvailSetProfile.generateAvailabilitySetProfile(__cb(_, __frame, 24, 50, function ___(__0, __4) { availsetProfileResult = __4;
              vmProfile.availabilitySet = availsetProfileResult.profile;

              vmNetworkProfile = new VMNetworkProfile(__this.cli, __this.resourceGroupName, __this.params, __this.serviceClients);
              return vmNetworkProfile.generateNetworkProfile(__cb(_, __frame, 28, 48, function ___(__0, __5) { networkProfileResult = __5;
                vmProfile.networkProfile = networkProfileResult.profile;

                if ((__this.params.tags && (typeof __this.params.tags === "string"))) {
                  vmProfile.tags = (vmProfile.tags || { });
                  vmProfile.tags = __this._parseTags(vmProfile.tags, __this.params.tags); } ;


                if (!utils.stringIsNullOrEmpty(__this.params.licenseType)) {
                  vmProfile.licenseType = __this.params.licenseType; } ; return (function __$generateVMProfile__1(__then) {



                  if ((((__this.params.disableBootDiagnostics !== null) || (__this.params.enableBootDiagnostics !== null)) || (__this.params.bootDiagnosticsStorageUri !== null))) { return (function __$generateVMProfile__1(__then) {
                      if ((__this.params.enableBootDiagnostics && (__this.params.bootDiagnosticsStorageUri === null))) {
                        stoInfo = storageProfileResult.storageAccountInfo;
                        stoProfile = null; return (function __$generateVMProfile__1(__then) {

                          if ((stoInfo === null)) {
                            return vmStorageProfile._createStorageAccountIfRequired(__cb(_, __frame, 47, 37, function ___(__0, __6) { stoInfo = __6; __then(); }, true)); } else { __then(); } ; })(function __$generateVMProfile__1() {


                          if ((((stoInfo !== null) && stoInfo.profile) && stoInfo.profile.storageAccount)) {
                            stoProfile = stoInfo.profile.storageAccount; } else {

                            if (((stoInfo !== null) && stoInfo.profile)) {
                              stoProfile = stoInfo.profile; } ; } ; return (function __$generateVMProfile__1(__then) {


                            if ((((stoProfile !== null) && stoProfile.primaryEndpoints) && stoProfile.primaryEndpoints.blob)) {
                              foundElem = false;
                              for (itemKey in storageUtil.AccountTypeForChanging) {
                                if ((storageUtil.AccountTypeForChanging[itemKey] === stoProfile.sku.name)) {
                                  foundElem = true;
                                  break; } ; }; return (function __$generateVMProfile__1(__then) {



                                if (!foundElem) {


                                  hash = utils.getHash(((__this.resourceGroupName + __this.params.location) + __this.params.imageUrn));
                                  bootStorageName = ((("clisto" + hash) + __this.params.vmName.replace("-", ""))).slice(0, 24).toLowerCase();
                                  createRequestProfile = {
                                    sku: { name: "Standard_LRS" },
                                    kind: "Storage",
                                    name: bootStorageName,
                                    location: __this.params.location };

                                  return __this.serviceClients.storageManagementClient.storageAccounts.create(__this.resourceGroupName, bootStorageName, createRequestProfile, __cb(_, __frame, 77, 72, function __$generateVMProfile__1() {
                                    return vmStorageProfile._findStorageAccount(bootStorageName, __cb(_, __frame, 78, 50, function ___(__0, __7) { bootStoProfile = __7;
                                      __this.params.bootDiagnosticsStorageUri = bootStoProfile.primaryEndpoints.blob; __then(); }, true)); }, true)); } else {


                                  __this.params.bootDiagnosticsStorageUri = stoProfile.primaryEndpoints.blob; __then(); } ; })(__then); } else {



                              __this.params.disableBootDiagnostics = true;
                              __this.params.enableBootDiagnostics = false;
                              __this.params.bootDiagnosticsStorageUri = null; __then(); } ; })(__then); }); } else { __then(); } ; })(function __$generateVMProfile__1() {



                      vmDiagnosticsProfile = new VMDiagnosticsProfile(__this.cli, __this.params);
                      diagnosticsProfileResult = vmDiagnosticsProfile.generateDiagnosticsProfile();
                      vmProfile.diagnosticsProfile = diagnosticsProfileResult.profile; __then(); }); } else {


                    vmProfile.diagnosticsProfile = null; __then(); } ; })(function __$generateVMProfile__1() {



                  if (__this.params.plan) {
                    vmProfile.plan = __this.params.plan; } ;


                  return _(null, {
                    profile: vmProfile }); }); }, true)); }, true)); }, true)); }); }, true)); }); },



  updateVMProfile: function updateVMProfile__2(virtualMachine, _) { var vmNetworkProfile, validNICs, vmHardwareProfile, vmDiagnosticsProfile, diagnosticsProfileResult, __this = this; var __frame = { name: "updateVMProfile__2", line: 148 }; return __func(_, this, arguments, updateVMProfile__2, 1, __frame, function __$updateVMProfile__2() {
      if ((((__this.params.disableBootDiagnostics !== null) || (__this.params.enableBootDiagnostics !== null)) || (__this.params.bootDiagnosticsStorageUri !== null))) {
        if ((((__this.params.disableBootDiagnostics && __this.params.enableBootDiagnostics)) || ((__this.params.disableBootDiagnostics && __this.params.bootDiagnosticsStorageUri)))) {
          return _(new Error($("Either only \"--disable-boot-diagnostics\" or \"--enable-boot-diagnostics\" with \"--boot-diagnostics-storage-uri\" can be specified."))); } ; } else {


        if ((!utils.atLeastOneParameIsSet([__this.params.nicIds,__this.params.nicNames,__this.params.vmSize,]) && (__this.params.tags === true))) {
          return _(new Error($("At least one optional parameter should be specified."))); } ; } ; return (function __$updateVMProfile__2(__then) {


        if ((__this.params.nicIds || __this.params.nicNames)) {
          vmNetworkProfile = new VMNetworkProfile(__this.cli, __this.resourceGroupName, __this.params, __this.serviceClients);
          return vmNetworkProfile.validateNICs(__cb(_, __frame, 12, 39, function ___(__0, __1) { validNICs = __1;
            virtualMachine.networkProfile.networkInterfaces = validNICs; __then(); }, true)); } else { __then(); } ; })(function __$updateVMProfile__2() {


        if ((__this.params.tags && (typeof __this.params.tags === "string"))) {
          virtualMachine.tags = (virtualMachine.tags || { });
          virtualMachine.tags = __this._parseTags(virtualMachine.tags, __this.params.tags); } ;


        if ((__this.params.tags === false)) {
          virtualMachine.tags = { }; } ;


        if (!utils.stringIsNullOrEmpty(__this.params.vmSize)) {
          vmHardwareProfile = new VMHardwareProfile(__this.cli, __this.params);
          virtualMachine.hardwareProfile = vmHardwareProfile.updateHardwareProfile(virtualMachine.hardwareProfile); } ;


        if ((__this.params.enableBootDiagnostics !== null)) {
          vmDiagnosticsProfile = new VMDiagnosticsProfile(__this.cli, __this.params);
          diagnosticsProfileResult = vmDiagnosticsProfile.generateDiagnosticsProfile();
          virtualMachine.diagnosticsProfile = diagnosticsProfileResult.profile; } ;


        return _(null, virtualMachine); }); }); },


  _parseTags: function(oldTags, tags) {
    var parsedTags = oldTags;
    utils.stringTrimEnd(tags, ";").split(";").forEach(function(tagValue) {
      var tv = tagValue.split("=");
      if ((tv.length === 2)) {
        parsedTags[tv[0]] = tv[1]; }
       else {
        parsedTags[tv[0]] = ""; } ; });



    return parsedTags; },


  _parseVMProfileParams: function _parseVMProfileParams__3(params, _) { var vmProfile, __this = this; var __frame = { name: "_parseVMProfileParams__3", line: 201 }; return __func(_, this, arguments, _parseVMProfileParams__3, 1, __frame, function __$_parseVMProfileParams__3() { return (function __$_parseVMProfileParams__3(__then) {
        if (!params.location) {
          return __this.cli.interaction.prompt($("Enter location: "), __cb(_, __frame, 2, 45, function ___(__0, __1) { params.location = __1; __then(); }, true)); } else { __then(); } ; })(function __$_parseVMProfileParams__3() { return (function __$_parseVMProfileParams__3(__then) {


          if (!params.vmName) {
            return __this.cli.interaction.prompt($("Enter VM name: "), __cb(_, __frame, 6, 43, function ___(__0, __2) { params.vmName = __2; __then(); }, true)); } else { __then(); } ; })(function __$_parseVMProfileParams__3() {


          vmProfile = {
            name: params.vmName,
            location: params.location,
            tags: { },
            osProfile: null,
            hardwareProfile: null,
            storageProfile: null,
            availabilitySetReference: null,
            networkProfile: null,
            diagnosticsProfile: null };


          return _(null, vmProfile); }); }); }); }});



module.exports = VMProfile;
