'use strict';

// FeatureMappers controller
angular.module('featureMappers').controller('FeatureMappersController', ['$scope', '$stateParams', '$location', 'Authentication', 'FeatureMappers',
    function ($scope, $stateParams, $location, Authentication, FeatureMappers) {
      $scope.authentication = Authentication;
      $scope.Pattern1 = '';
      $scope.Pattern2 = '';
      /*
      $scope.PlacementPattern1 = "{\"placement_waiver\":\"!Y\"}";
      $scope.PlacementPattern2 = "{\"placement_exception\":\"!Y\"'}";
      */
      $scope.PlacementPattern1 = '';
      $scope.PlacementPattern2 = '';
      $scope.sortType = ['address.city', 'address.state', 'address.country']; // set the default sort type
      $scope.sortReverse = false;  // set the default sort order

      // input arrays
      $scope.featureMapper = new FeatureMappers({
        aic_version: '',
        clli: '',
        cluster: '',
        contrail_version: '',
        date_end: '',
        date_start: '',
        myLogins_access: '',
        placement_exception: '',
        placement_waiver: '',
        site_type: '',
        site_description: '',
        zone: '',
        address: '',
        compute_features: [],
        networks: [],
        storages: [],
        support_models: [],
        tools: [],
        workloads: []
      });

      // Create new FeatureMapper
      $scope.create = function (isValid) {
        $scope.error = null;

        if (!isValid) {
          $scope.$broadcast('show-errors-check-validity', 'featureMapperForm');
          return false;
        }

        // Create new address object
        var featureMapper = new FeatureMappers({
          aic_version: this.featureMapper.aic_version,
          clli: this.featureMapper.clli,
          cluster: this.featureMapper.cluster,
          contrail_version: this.featureMapper.contrail_version,
          date_end: this.featureMapper.date_end,
          date_start: this.featureMapper.date_start,
          myLogins_access: this.featureMapper.myLogins_access,
          placement_exception: this.featureMapper.placement_exception,
          placement_waiver: this.featureMapper.placement_waiver,
          site_type: this.featureMapper.site_type,
          site_description: this.featureMapper.site_description,
          zone: this.featureMapper.zone,
          address: this.featureMapper.address,
          compute_features: this.featureMapper.compute_features,
          networks: this.featureMapper.networks,
          storages: this.featureMapper.storages,
          support_models: this.featureMapper.support_models,
          tools: this.featureMapper.tools,
          workloads: this.featureMapper.workloads
        });

        // Redirect after save
        featureMapper.$save(function (response) {
          $location.path('featureMappers/' + response._id);
          // Clear form fields
          /**
          $scope.featureMapper.aic_version = '';
          $scope.featureMapper.clli = '';
          $scope.featureMapper.cluster = '';
          $scope.featureMapper.contrail_version = '';
          $scope.featureMapper.address.common_name = '';
          $scope.featureMapper.address.street1 = '';
          $scope.featureMapper.address.street2 = '';
          $scope.featureMapper.address.floor = '';
          $scope.featureMapper.address.section = '';
          $scope.featureMapper.address.state = '';
          $scope.featureMapper.address.postal_code = '';
          $scope.featureMapper.address.country = '';
          **/
          $scope.featureMapper = null;
        }, function (errorResponse) {
          $scope.error = errorResponse.data.message;
        });
      };

      // Remove existing FeatureMapper
      $scope.remove = function (featureMapper) {
        if (featureMapper) {
          featureMapper.$remove();

          for (var i in $scope.featureMappers) {
            if ($scope.featureMappers[i] === featureMapper) {
              $scope.featureMappers.splice(i, 1);
            }
          }
        } else {
          $scope.featureMapper.$remove(function () {
            $location.path('featureMappers/list');
          });
        }
      };

      // Duplicate existing FeatureMapper
      $scope.replicate = function (featureMapper) {
        if (featureMapper) {
          var fm = new FeatureMappers({
            aic_version: featureMapper.aic_version,
            clli: featureMapper.clli,
            cluster: featureMapper.cluster,
            contrail_version: featureMapper.contrail_version,
            date_end: featureMapper.date_end,
            date_start: featureMapper.date_start,
            myLogins_access: featureMapper.myLogins_access,
            placement_exception: featureMapper.placement_exception,
            placement_waiver: featureMapper.placement_waiver,
            site_type: featureMapper.site_type,
            site_description: featureMapper.site_description,
            zone: featureMapper.zone,
            address: featureMapper.address,
            compute_features: featureMapper.compute_features,
            networks: featureMapper.networks,
            storages: featureMapper.storages,
            support_models: featureMapper.support_models,
            tools: featureMapper.tools,
            workloads: featureMapper.workloads
          });
          fm.$save(function () {
            $location.path('featureMappers/' + featureMapper._id + '/edit');
          }, function (errorResponse) {
            $scope.error = errorResponse.data.message;
          });
        }
      };

      // Update existing FeatureMapper
      $scope.update = function (isValid) {
        $scope.error = null;

        if (!isValid) {
          $scope.$broadcast('show-errors-check-validity', 'featureMapperForm');
          return false;
        }

        /**
        console.log('$scope.featureMapper -- %j', $scope.featureMapper);
        console.log('$scope.featureMapper.networks -- %j', $scope.featureMapper.networks);
        console.log('this.featureMapper -- %j', this.featureMapper);
        console.log('this.featureMapper.networks -- %j', this.featureMapper.networks);
        **/
        // $scope.docCleanup($scope.featureMapper, $scope.featureMapper);

        $scope.featureMapper.$update(function () {
          $location.path('featureMappers/' + $scope.featureMapper._id);
        }, function (errorResponse) {
          $scope.error = errorResponse.data.message;
        });
      };

      // Find a list of FeatureMappers
      $scope.find = function () {
        $scope.featureMappers = FeatureMappers.query();
      };

      // Find existing FeatureMapper
      $scope.findOne = function () {
        // Callback set up  won't get instant response  Just a promise
        $scope.featureMapper = FeatureMappers.get({
          featureMapperId: $stateParams.featureMapperId
        });
      };

      // Add array member on button click
      $scope.addMember = function (source, target) {
        if (angular.isDefined(target)) {
          if (angular.isDefined(source)) {
            var addVar = source.trim();
            if (addVar.length >= 0) {
              if (target.indexOf(addVar) < 0) {
                target.push(addVar);
              }
            }
          }
        }
      };

      // Remove array member on button click
      $scope.removeMember = function (source, target) {
        if (angular.isDefined(target)) {
          if (angular.isDefined(source)) {
            var remVar = source.trim();
            if (remVar.length > 0) {
              var index = target.indexOf(remVar);
              if (index >= 0) {
                target.splice(index,1);
              }
            }
          }
        }
      };

      // Add array member on button click
      $scope.saveMember = function (source, target, index) {
        if (angular.isDefined(target)) {
          if (angular.isDefined(source)) {
            if (index > -1 && target.length > index) {
              target[index] = source;
            }
          }
        }
      };

      // document clean up
      $scope.docCleanup = function (myfm, tgt) {

        var index2;
        var clliStrings = [];

        myfm.address.section = '';

        if (myfm.start_date === '') {
          myfm.start_date = '01/01/2014';
        }
        if (myfm.end_date === '') {
          myfm.end_date = '01/01/2025';
        }

        index2 = myfm.workloads.indexOf('CCP');
        if (index2 >= 0) {
          console.log('splice CCP ' + index2 + ' from %j', myfm.workloads);
          myfm.workloads.splice(index2,1);
          console.log('splice CCP ' + index2 + ' from %j', myfm.workloads);
        }

        if (myfm.workloads.indexOf(myfm.site_description) < 0) {
          myfm.workloads.push(myfm.site_description);
        }
        if (myfm.site_description === 'Shared VMware') {
          if (myfm.networks.indexOf('Conexus Intranet') < 0) {
            console.log('push Conexus Intranet to %j', myfm.networks);
            myfm.networks.push('Conexus Intranet');
          }

          clliStrings = ['ALLNTXDW', 'ALPRGAED','BOTHWAKY', 'BRWDTNAI', 'DLLSTXCF', 'KGMTNC20'];
          for (index2=0; index2 < clliStrings.length; index2++) {
            if (myfm.clli === clliStrings[index2]) {
              if (myfm.networks.indexOf('Mobility Extranet') < 0) {
                myfm.networks.push('Mobility Extranet');
              }
              if (myfm.networks.indexOf('Mobility Intranet') < 0) {
                console.log('push Mobility Extranet to %j', myfm.networks);
                myfm.networks.push('Mobility Intranet');
              }
            }
          }

          clliStrings = ['ALLNTXDW', 'BOTHWAKY', 'KGMTNC20'];
          for (index2=0; index2 < clliStrings.length; index2++) {
            if (myfm.clli === clliStrings[index2]) {
              if (myfm.networks.indexOf('Mobility OAM') < 0) {
                myfm.networks.push('Mobility OAM');
              }
            }
          }

          clliStrings = ['ALPRGAED', 'BRHMALDC','FRFDCA60', 'HYWRCA02', 'KGMTNC20', 'SFLDMIBB'];
          for (index2=0; index2 < clliStrings.length; index2++) {
            if (myfm.clli === clliStrings[index2]) {
              if (myfm.networks.indexOf('Conexus Extranet') < 0) {
                console.log('push Conexus Extranet to %j', myfm.networks);
                myfm.networks.push('Conexus Extranet');
              }
            }
          }
        }

        if (myfm.site_description === 'CCP VMware') {
          index2 = myfm.networks.indexOf('Mobility Extranet');
          if (index2 >= 0) {
            console.log('splice Mobility Extranet ' + index2 + ' from %j', myfm.networks);
            myfm.networks.splice(index2,1);
          }
          index2 = myfm.networks.indexOf('Conexus Extranet');
          if (index2 >= 0) {
            console.log('splice Conexus Extranet ' + index2 + ' from %j', myfm.networks);
            myfm.networks.splice(index2,1);
          }
          index2 = myfm.networks.indexOf('Extranet');
          if (index2 >= 0) {
            console.log('splice Extranet ' + index2 + ' from %j', myfm.networks);
            myfm.networks.splice(index2,1);
          }

          clliStrings = ['ALLNTXDW', 'BOTHWAKY', 'KGMTNC20'];
          for (index2=0; index2 < clliStrings.length; index2++) {
            if (myfm.clli === clliStrings[index2]) {
              if (myfm.networks.indexOf('Mobility OAM') < 0) {
                myfm.networks.push('Mobility OAM');
              }
            }
          }

        }

        if (myfm.site_description === 'CCP VMware Extranet') {
          clliStrings = ['ALLNTXDW', 'ALPRGAED','BOTHWAKY', 'BRWDTNAI', 'DLLSTXCF', 'KGMTNC20'];
          for (index2=0; index2 < clliStrings.length; index2++) {
            if (myfm.clli === clliStrings[index2]) {
              if (myfm.networks.indexOf('Mobility Extranet') < 0) {
                console.log('push Mobility Extranet to %j', myfm.networks);
                myfm.networks.push('Mobility Extranet');
              }
            }
          }

          if (myfm.networks.indexOf('Conexus Extranet') < 0) {
            console.log('push Conexus Extranet to %j', myfm.networks);
            myfm.networks.push('Conexus Extranet');
          }
        }
        tgt = myfm;

      };
    }
]);
