/*********************************************
 *  Copyright (c) 2014 AnsibleWorks, Inc.
 *
 * License.js
 *
 * View license info found in /api/vi/config/
 *
 *****************************************/

'use strict';

angular.module('License', ['RestServices', 'Utilities', 'FormGenerator', 'PromptDialog'])
   .factory('ViewLicense', ['$location', '$rootScope', 'GenerateForm', 'Rest', 'Alert', 'GetBasePath', 'ProcessErrors',
       'FormatDate', 'Prompt', 'Empty',
   function($location, $rootScope, GenerateForm, Rest, Alert, GetBasePath, ProcessErrors, FormatDate, Prompt, Empty) {
   return function() {
   
   var defaultUrl=GetBasePath('config');
   var generator = GenerateForm;
   
   var form = {
       name: 'license',
       well: false,
       forceListeners: true,
       fields: {
           license_status: {
               type: 'custom',
               control: '<div class=\"license-status\" ng-class=\"status_color\"><i class="fa fa-circle"></i> \{\{ license_status \}\}</span></div>',
               readonly: true,
               section: 'License'
               },
           license_key: {
               label: 'Key',
               type: 'textarea',
               'class': 'modal-input-xlarge',
               readonly: true,
               section: 'License'
               },
           license_date: {
               label: 'Expires On',
               type: 'text',
               readonly: true,
               section: 'License'
               },
           time_remaining: {
               label: 'Time Left',
               type: 'text',
               readonly: true,
               section: 'License'
               },
           available_instances: {
               label: 'Available',
               type: 'text',
               readonly: true,
               section: 'Managed Hosts'
               },
           current_instances: {
               label: 'Used',
               type: 'text',
               readonly: true,
               section: 'Managed Hosts'
               },
           free_instances: {
               label: 'Remaining',
               type: 'text',
               readonly: true,
               section: 'Managed Hosts',
               controlNGClass: 'free_instances_class',
               labelNGClass: 'free_instances_class'
               },
           company_name: {
               label: 'Company',
               type: 'text',
               readonly: true,
               section: 'Contact Info'
               },
           contact_name: {
               label: 'Contact',
               type: 'text',
               readonly: true,
               section: 'Contact Info'
               },
            contact_email: {
               label: 'Contact Email',
               type: 'text',
               readonly: true,
               section: 'Contact Info'
               }
            }
        };

   var base = $location.path().replace(/^\//,'').split('/')[0]; 
  
                
   // Retrieve detail record and prepopulate the form
   Rest.setUrl(defaultUrl); 
   Rest.get()
       .success( function(data, status, headers, config) {
           
           for (var fld in form.fields) {
               if (fld != 'time_remaining' && fld != 'license_status') {
                   if (Empty(data['license_info'][fld])) {
                      delete form.fields[fld];
                   }
               }
           }

           if (data['license_info']['is_aws'] || Empty(data['license_info']['license_date'])) {
               delete form.fields['license_date'];
               delete form.fields['time_remaining'];
           }

           var scope = generator.inject(form, { mode: 'edit', modal: true, related: false});
           generator.reset();
           
           scope.formModalAction = function() {
               $('#form-modal').modal("hide");
               }
           
           scope.formModalActionLabel = 'OK';
           scope.formModalCancelShow = false;
           scope.formModalInfo = 'Purchase/Extend License';
           scope.formModalHeader = 'Tower License';

           //$('#form-modal .btn-success').removeClass('btn-success').addClass('btn-none');
           //$('#form-modal').addClass('skinny-modal');
          
           
           // Respond to license button
           scope.formModalInfoAction = function() {
               Prompt({
                   hdr: 'Tower Licensing',
                   body: "<p>Ansible Tower licenses can be purchased or extended by visiting <a id=\"license-link\" " +
                       "href=\"http://store.ansible.com\" target=\"_blank\">" +
                       "the Ansible online store</a>. Would you like to purchase or extend your license now?</p>",
                   'class': 'btn-primary', 
                   action: function() { 
                       var href = $('#license-link').attr('href'); 
                       window.open(href, 'storeWindow'); 
                       }
                   });
               }

           for (var fld in form.fields) {
               if (!Empty(data['license_info'][fld])) {
                   scope[fld] = data['license_info'][fld];
               }
           }
           
           if (scope['license_date']) {
               var dt = new Date(parseInt(scope['license_date']));
               if (dt.getFullYear() == '1970') {
                   // date was passed in seconds rather than milliseconds
                   dt = new Date(parseInt(scope['license_date']) * 1000);
                   scope['time_remaining'] = scope['time_remaining'] + '000';
               } 
               scope['license_date'] = FormatDate(dt);

               var days = parseInt(scope['time_remaining'] / 86400000);
               var remainder = scope['time_remaining'] - (days * 86400000);
               var hours = parseInt(remainder / 3600000);
               remainder = remainder - (hours * 3600000);
               var minutes = parseInt(remainder / 60000);
               remainder = remainder - (minutes * 60000);
               var seconds = parseInt(remainder / 1000);
               scope['time_remaining'] = days + ' days ' + ('0' + hours).slice(-2) + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2); 
           }

           if (parseInt(scope['free_instances']) <= 0) {
               scope['free_instances_class'] = 'field-failure';
           }
           else {
               scope['free_instances_class'] = 'field-success';
           }

           var license = data['license_info'];
           if (license['valid_key'] !== undefined && license['valid_key'] == false) {
               scope['license_status'] = 'Invalid';
               scope['status_color'] = 'license-invalid';
           }
           else if (license['demo'] !== undefined && license['demo'] == true) {
               scope['license_status'] = 'Demo';
               scope['status_color'] = 'license-demo';
           }
           else if (license['date_expired'] !== undefined && license['date_expired'] == true) {
               scope['license_status'] = 'Expired';
               scope['status_color'] = 'license-expired';
           }
           else if (license['date_warning'] !== undefined && license['date_warning'] == true) {
               scope['license_status'] = 'Expiration Warning';
               scope['status_color'] = 'license-warning';
           }
           else if (license['free_instances'] !== undefined && parseInt(license['free_instances']) <= 0) {
               scope['license_status'] = 'No available managed hosts';
               scope['status_color'] = 'license-invalid';
           }
           else {
               scope['license_status'] = 'Valid';
               scope['status_color'] = 'license-valid';
           }

           })
       .error( function(data, status, headers, config) {
           ProcessErrors($rootScope, data, status, form,
               { hdr: 'Error!', msg: 'Failed to retrieve license. GET status: ' + status });
           });
   }
   }]);
