/*global _*/
(function() {
  'use strict';

  angular
    .module('fusionSeedApp.components.facetField')
    .directive('facetField', facetField);

  function facetField() {
    'ngInject';
    return {
      restrict: 'EA',
      templateUrl: 'assets/components/facetField/facetField.html',
      scope: true,
      controller: Controller,
      controllerAs: 'vm',
      bindToController: {
        facetName: '@facetName',
        facetLabel: '@facetLabel',
        facetAutoOpen: '@facetAutoOpen'
      }
    };
  }

  function Controller(ConfigService, QueryService, QueryDataService, Orwell, FoundationApi, $log) {
    'ngInject';
    var vm = this;
    vm.facetCounts = [];
    vm.toggleFacet = toggleFacet;
    var resultsObservable = Orwell.getObservable('queryResults');

    activate();

    //////////////

    /**
     * Activate the controller.
     */
    function activate() {

      // Add observer to update data when we get results back.
      resultsObservable.addObserver(function (data) {
        // Exit early if there are no facets in the response.
        if (!data.hasOwnProperty('facet_counts')) return;

        // Determine if facet exists.
        var facetFields = data.facet_counts.facet_fields;
        if (facetFields.hasOwnProperty(vm.facetName)) {
          // Transform an array of values in format [‘aaaa’, 1234,’bbbb’,2345] into an array of objects.
          vm.facetCounts = arrayToObjectArray(facetFields[vm.facetName]);
        }

        // Set inital active state
        var active = true;
        // If we have autoOpen set active to this state.
        if (angular.isDefined(vm.facetAutoOpen) && vm.facetAutoOpen === 'false') {
          active = false;
        }
        vm.active = active;
      });
    }

    /**
     * Turn a flat array into an object array.
     *
     * Transforms an array of values in format ['aaaa', 1234,'bbbb',2345] into
     * an array of objects.
     * [{title:'aaaa', amount:1234, hash: zf-hs-njkfhdsle},
     *  {title:'bbbb', amount:2345, hash: zf-hs-jkewrkljn}]
     *
     * @param  {array} arr The array to transform
     * @return {array}     An array of objects
     */
    function arrayToObjectArray(arr) {
      return _.transform(arr, function (result, value, index) {
        if (index % 2 === 1) {
          result[result.length - 1] = {
            title: result[result.length - 1],
            amount: value,
            hash: FoundationApi.generateUuid(),
            active: isFacetActive(vm.facetName, value)
          };
        } else {
          result.push(value);
        }
      });
    }

    function toggleFacet(facet) {
      var key = vm.facetName;
      var query = QueryService.getQueryObject();

      // CASE: fq exists.
      if(!query.hasOwnProperty('fq')){
        query = addQueryFacet(query, key, facet.title);
      } else {
        // Remove the key object from the query.
        // We will re-add later if we need to.
        var keyArr = _.remove(query.fq, {key: key, transformer:'fq:field'});

        // CASE: facet key exists in query.
        if(keyArr.length > 0) {
          var keyObj = keyArr[0];
          var removed = _.remove(keyObj.values, function(value){return value === facet.title;});
          // CASE: value didn't previously exist add to values.
          if(removed.length === 0){
            keyObj.values.push(facet.title);
          }
          // attach keyobject back to query if there are still values.
          if(keyObj.values.length > 0){
            query.fq.push(keyObj);
          }
          // Delete 'fq' if it is now empty.
          if(query.fq.length === 0){
            delete query.fq;
          }
        } else { // CASE: Facet key doesnt exist ADD key AND VALUE.
          query = addQueryFacet(query, key, facet.title);
        }

      }
      // Set the query and trigger the refresh.
      QueryService.setQuery(query);
    }

    function isFacetActive(key, value){
      var query = QueryService.getQueryObject();
      if(!query.hasOwnProperty('fq')){
        return false;
      }
      var keyObj = _.find(query.fq, {key: key, transformer: 'fq:field'});
      if(_.isEmpty(keyObj)){
        return false;
      }
      if(_.isEmpty(_.find(keyObj.values, value))){
        return false;
      }
      return true;
    }

    function addQueryFacet(query, key, title){
      if(!query.hasOwnProperty('fq')){
        query.fq = [];
      }
      var keyObj = {
        key: key,
        values: [title],
        transformer: 'fq:field'
      };
      query.fq.push(keyObj);
      return query;
    }

  }
})();

@@ -990,28 +990,144 @@
 ver(
-function 
+parseFacets);%0A      // initialize the facets.%0A      parseFacets(resultsObservable.getContent());%0A    %7D%0A%0A    function parseFacets
 (data)
- 
 %7B%0A
-  
     
@@ -1184,26 +1184,24 @@
 onse.%0A      
- 
- 
 if (!data.ha
@@ -1241,26 +1241,24 @@
 urn;%0A%0A      
-  
 // Determine
@@ -1277,26 +1277,24 @@
 ists.%0A      
- 
- 
 var facetFie
@@ -1329,24 +1329,111 @@
 t_fields;%0A  
+    $log.debug('facetFields',facetFields);%0A      $log.debug('facetName',vm.facetName);%0A
       if (fa
@@ -1482,18 +1482,16 @@
         
-  
 // Trans
@@ -1585,18 +1585,16 @@
         
-  
 vm.facet
@@ -1651,31 +1651,27 @@
 me%5D);%0A      
-  
 %7D%0A%0A
-  
       // Set
@@ -1693,26 +1693,24 @@
 state%0A      
-  
 var active =
@@ -1716,18 +1716,16 @@
 = true;%0A
-  
       //
@@ -1771,18 +1771,16 @@
  state.%0A
-  
       if
@@ -1856,26 +1856,24 @@
 ) %7B%0A        
- 
- 
 active = fal
@@ -1882,26 +1882,24 @@
 ;%0A      
-  
 %7D%0A      
   vm.act
@@ -1890,18 +1890,16 @@
 %7D%0A      
- 
- 
 vm.activ
@@ -1910,26 +1910,16 @@
 active;%0A
-      %7D);%0A
     %7D%0A%0A 
@@ -4017,16 +4017,39 @@
 efresh.%0A
+      query.start = 0;%0A
       Qu

update facetFields to better handle initial data parse.