// User Story: PROF-35 | VIP-19, croja022 (business search feature)
// Initial file creation and implementation.
(function () {
    'use strict';
    angular
        .module('patent')
        .controller('patentBusinessSearchController', patentBusinessSearchController);
        patentBusinessSearchController.$inject = ['DTOptionsBuilder', 'DTColumnBuilder','$sce', '$location', '$state', '$scope', '$stateParams', 'User', '$window'];
    /* @ngInject */
    var helper = PatentHelper();
    function patentBusinessSearchController(DTOptionsBuilder, DTColumnBuilder, $scope) 
    {
      var vm = this; 
      vm.done = false;
      vm.done = true;
      vm.options = ['Yelp'];
      vm.selection = vm.options[0];
      var yelp_dtOptions = DTOptionsBuilder.fromSource('').withFnServerData(ServerData).withPaginationType('full_numbers').withDataProp('businesses').withOption('responsive', true).withPaginationType('full_numbers').withDataProp('businesses').withOption('processing', true).withButtons([
        'colvis',        
        'print',                   
      ]);        
        
      var yelp_dtColumns = [
            DTColumnBuilder.newColumn('name').withTitle('Name').withOption('defaultContent', '-default value-'),
            DTColumnBuilder.newColumn('categories').withTitle('Category').renderWith(function(data, type, full)
            {
                var result = "";
                for(var i = 0; i < data.length; i++){
                 result += data[i].title + ", ";
                }
                if(result)
                    result = result.trim().slice(0, -1);
                return result;
            }).withOption('defaultContent', '-default value-'),
        DTColumnBuilder.newColumn('phone').withTitle('Phone').renderWith(function(data, type, full)
         {
           //TODO: Format phone number
           return data;
         }).withOption('defaultContent', '-default value-'),
        DTColumnBuilder.newColumn('is_closed').withTitle('Status').renderWith(function(data, type, full)
         {
           if (data)
            return "Closed location";
           return "Open for business";
         }).withOption('defaultContent', '-default value-')
       ]; 
      vm.term = "";
      vm.location = "Miami";
      vm.search = search;
      vm.dtInstance = {};            
      vm.dtOptions = yelp_dtOptions;
      vm.dtColumns = yelp_dtColumns;
      function ServerData(sSource, aoData, fnCallback, oSettings) {  
        console.log("ajax");
          if (vm.selection == vm.options[0] && vm.dtOptions == yelp_dtOptions &&  vm.dtColumns == yelp_dtColumns){
          oSettings.jqXHR =
              $.ajax({
                     url: helper.getYelpUrl(vm.term, vm.location),
                     headers: {
                     'Authorization': 'Bearer ' + helper.YelpKey,
                     },
                     method: 'GET',
                     dataType: 'json',
                     success: function(data) {
                     fnCallback(data);
                     }});
        }
      }
    
      function search(){
          if((!vm.term || !vm.term.replace(/ +/g, ' ')) &&
             (!vm.location  || !vm.location.replace(/ +/g, ' '))){
              return;
          }
          if(vm.selection == vm.options[0]){
              vm.dtColumns = yelp_dtColumns;
              vm.dtOptions = yelp_dtOptions;
          }
          vm.dtInstance.reloadData();
      }
    }
    
  })();