angular.module('codeBaseAdminApp')
.controller('VisitorBlocker', function ($scope, $templateCache, $compile, $http, $location, $mdDialog, Dialog, $rootScope, Toast) {

    $scope.engines = [
      {
        value: 'local',
        title: 'Local'
      },
      {
        value: 'remote',
        title: 'Remote (Legacy, Third-Party Blocker)'
      }
    ];
    
    $scope.block_types = [
      {
        value: 'email',
        title: 'Email'
      },
      {
        value: 'ipaddress',
        title: 'IP Address'
      },
      {
        value: 'cc',
        title: 'Credit Card'
      },
      {
        value: 'ccip',
        title: 'Credit Card + IP Address'
      }
    ];
    
    let orderBlockType = $scope.extension.order_block_type;
    let blockTypeArray = [];
    
    if (typeof(orderBlockType) !== "undefined" && typeof(orderBlockType) === "string") {
        blockTypeArray = [orderBlockType];
        $scope.extension.order_block_type = blockTypeArray;
    }

    $scope.inputData = {
			country : {
				options : [],
				default_option : 'Select Country',
				key : 'iso',
				label : 'country'
			}
    };
    $scope.temp = {};
    $scope.country_id_separator = ",";
    $scope.showExtension = false;
    
    $scope.defaultStepWiseMethods = function () {
        return {
            'step_id': '',
            'gateway_id': ''
        }
    };
    
    $scope.extension.enable_order_decline_limit = !$scope.extension.order_decline_limit ? true : $scope.extension.enable_order_decline_limit;
    $scope.extension.order_decline_type = !$scope.extension.order_decline_type ? 'SQLite' : $scope.extension.order_decline_type;
    $scope.extension.order_decline_limit = !$scope.extension.order_decline_limit ? '5' : $scope.extension.order_decline_limit;
    $scope.extension.order_decline_limit_upsell = !$scope.extension.order_decline_limit_upsell ? '5' : $scope.extension.order_decline_limit_upsell;
    $scope.extension.default_block_time = !$scope.extension.default_block_time ? '1440' : $scope.extension.default_block_time;
    $scope.extension.order_block_type = !$scope.extension.order_block_type ? 'ipaddress' : $scope.extension.order_block_type;
    
    $scope.$parent.$watch('countryList', function(newValue, oldValue) {
        if(!angular.isUndefined(newValue) && newValue.length > 0 && $scope.$parent.countryListReady) {
           $scope.showExtension = true;
           $scope.inputData.country.options = $scope.$parent['countryList'];
        }
    });


    $scope.addToTextarea = function(itemToAdd, itenintoAdd) {
        if(angular.isUndefined($scope.temp[itemToAdd]) || $scope.temp[itemToAdd] === '') {
            Toast.showToast('Input can not be blank.');
            return;
        }

        if(!angular.isUndefined($scope.extension[itenintoAdd]) && $scope.extension[itenintoAdd].length) {
            $scope.extension[itenintoAdd] = $scope.temp[itemToAdd] + '\n' + $scope.extension[itenintoAdd];
        }
        else {
            $scope.extension[itenintoAdd] = $scope.temp[itemToAdd];
        }
        
        $scope.temp[itemToAdd] = '';
    }
    

    $scope.changeEngine = function() {
      if($scope.extension.block_engine == "remote" && $scope.extension.enable_order_decline_limit) {
        $scope.extension.enable_order_decline_limit = false;
      }
      else if($scope.extension.block_engine == "local" && $scope.extension.enable_blacklist_service) {
        $scope.extension.enable_blacklist_service = false;
      }
    }

    if (typeof $scope.extension.gateway_settings == "undefined" ||
            !$scope.extension.gateway_settings.length > 0) {
        $scope.extension.gateway_settings = [];
        $scope.extension.gateway_settings.push($scope.defaultStepWiseMethods());
        console.log($scope.extension.gateway_settings);
    }
    $scope.add_custom_decline_msg = '';


    $scope.addMethod = function () {
        $scope.extension.gateway_settings.push($scope.defaultStepWiseMethods());
    };
    $scope.removeMsg = function (index) {
        $scope.extension.gateway_settings.splice(index, 1);
    }

    $scope.edit = function (index) {
        console.log($scope.extension.gateway_settings);
        if (!$scope.extension.gateway_settings[index]['step_id']) {
            $scope.extension.gateway_settings[index]['step_id'] = "";
            Toast.showToast('Add step Id');
            return false;
        }
        if (!$scope.extension.gateway_settings[index]['gateway_id']) {
            $scope.extension.gateway_settings[index]['gateway_id'] = "";
            Toast.showToast('Add gateway Id');
            return false;
        }

    }
    
    $scope.getDefaultBlockAddressParams = function(){
        return {
            'addressToBlock' : ''
        }
    }
    if (typeof $scope.extension.addressblock == "undefined" || 
        !$scope.extension.addressblock.length > 0) {
    
       $scope.extension.addressblock = [];
        $scope.extension.addressblock.push($scope.getDefaultBlockAddressParams());
    }
    $scope.addAddressMethod = function(){
            $scope.extension.addressblock.push($scope.getDefaultBlockAddressParams());
    }
    $scope.remove = function (index) {
        $scope.extension.addressblock.splice(index, 1);
    }
    
    $scope.saveExtension = function(){
        $scope.$parent.saveExtension();
        $scope.VisitorBlocker.$submitted = false;
    }


}).filter('ucfirst', function() {
    return function(input) {
      return (angular.isString(input) && input.length > 0) ? input.charAt(0).toUpperCase() + input.substr(1).toLowerCase() : input;
    }
});