'use strict';

/**
 * @ngdoc directive
 * @name hmdaPilotApp.directive:WizardNav
 * @description
 * # Wizard Nav directive
 * Directive for displaying the wizard navigation.
 */
module.exports = /*@ngInject*/ function ($location, $timeout, StepFactory, Wizard, ngDialog) {

    function getStepClass(step) {
        if (step.isActive) {
            step.stepClass = 'active';
        } else {
            step.stepClass = step.status;
        }

        if (step.isComplete() || (step.isFocused && !step.isActive)) {
            step.stepClass += ' focusable';
        }

        return step;
    }

    function getStepBadge(step, stepIdx) {
        if (step.isComplete()) {
            step.badgeClass = 'step-badge cf-icon cf-icon-approved';
            step.badgeText = '';
        } else {
            step.badgeClass = 'step-badge badge-num';
            step.badgeText = stepIdx + 1;
        }

        return step;
    }

    function controller($scope, Configuration) {
        if (Configuration.confirmSessionReset) {
            $scope.$on('$locationChangeStart', function(event, newUrl) {
                if (newUrl.indexOf('#/selectFile') !== -1 ) {
                    ngDialog.openConfirm({
                        template: 'partials/confirmSessionReset.html'
                    }).then(function (value) {
                        if (value === 'reset') {
                            $location.path('/');
                        }
    			});
                    event.preventDefault();
                }

                return;
            });
        }
    }

    return {
        restrict: 'E',
        templateUrl: 'partials/wizardNav.html',
        scope: {
            steps: '='
        },
        link: function(scope, element) {
            // Initialize scope variables
            scope.steps = [];

            // Determine if the wizard nav should be displayed or not
            scope.showWizardNav = function() {
                console.log('showWizardNav');
                return ['/about', '/common-questions'].indexOf($location.path()) === -1;
            };

            scope.setActive = function(step) {
                console.log('markActive');
                var newSteps = Wizard.getSteps();

                for (var i=0; i < newSteps.length; i++) {
                    newSteps[i].isFocused = false;
                }
                step.isFocused = true;
                $location.path(step.view);
            };

            // Watch the Wizard steps to see if they change
            scope.$watch(function() {
                return Wizard.getCurrentStep();
            }, function() {
                var newSteps = Wizard.getSteps();

                for (var i=0; i < newSteps.length; i++) {
                    newSteps[i] = getStepClass(newSteps[i]);
                    newSteps[i] = getStepBadge(newSteps[i], i);
                }

                scope.steps = newSteps;

                $timeout(function() { // Wrap the events in a timeout to give the partial time to render :(
                    element.find('a').on('focus', function(event) {
                        angular.element(event.target).parent().addClass('is_focused');
                    });

                    element.find('a').on('blur', function(event) {
                        angular.element(event.target).parent().removeClass('is_focused');
                    });
                }, 100);

            });

            scope.$on('$routeChangeSuccess', function (event, current, previous) {
                if (current !== previous) {
                    scope.steps = Wizard.getSteps();
                }
            });
        },
        controller: /*@ngInject*/ controller
    };
};

@@ -293,32 +293,59 @@
 epClass(step) %7B%0A
+        console.log(step);%0A
         if (step
@@ -499,46 +499,8 @@
 te()
- %7C%7C (step.isFocused && !step.isActive)
 ) %7B%0A
@@ -546,32 +546,118 @@
 le';%0A        %7D%0A%0A
+        if (step.isFocused) %7B%0A            step.stepClass += ' is_focused';%0A        %7D%0A%0A
         return s
@@ -1813,17 +1813,8 @@
 cope
-, element
 ) %7B%0A
@@ -2007,54 +2007,8 @@
 ) %7B%0A
-                console.log('showWizardNav');%0A
     
@@ -2188,12 +2188,11 @@
 og('
-mark
+set
 Acti
@@ -2298,32 +2298,131 @@
 .length; i++) %7B%0A
+                    if (newSteps%5Bi%5D === step) %7B%0A                        console.log('equals');%0A    
                 
@@ -2453,12 +2453,11 @@
 d = 
-fals
+tru
 e;%0A 
@@ -2463,33 +2463,44 @@
                 
-%7D
+    %7D else %7B
 %0A               
@@ -2504,30 +2504,147 @@
     
-step.isFocused = true;
+        newSteps%5Bi%5D.isFocused = false;%0A                    %7D%0A                    newSteps%5Bi%5D = getStepClass(newSteps%5Bi%5D);%0A                %7D
 %0A   
@@ -3171,503 +3171,8 @@
 s;%0A%0A
-                $timeout(function() %7B // Wrap the events in a timeout to give the partial time to render :(%0A                    element.find('a').on('focus', function(event) %7B%0A                        angular.element(event.target).parent().addClass('is_focused');%0A                    %7D);%0A%0A                    element.find('a').on('blur', function(event) %7B%0A                        angular.element(event.target).parent().removeClass('is_focused');%0A                    %7D);%0A                %7D, 100);%0A%0A
     

use is_focused and update the stepClass