'use strict';

app.directive('userPanel', function() {
  return {
    restrict: 'EA',
    //replace: true,
    scope: {},
    template: '<a class="btn btn-nav pull-right" ng-click="showHelpModal()">' +
                '<i class="fa fa-question-circle fa-lg"></i>&nbsp;&nbsp;<span>Help</span>'+
              '</a>'+
              '<button class="btn btn-nav pull-right" ng-if="currentUser" data-placement="bottom-right" data-html="1" data-trigger="click" bs-dropdown="dropdown">'+
                '<i class="fa fa-user fa-lg"></i>&nbsp;&nbsp; {{currentUser.username}} &nbsp;<i class="fa fa-caret-down"></i>'+
              '</button>',
    controller: function($rootScope, $scope, $state, $window, $modal, Auth, User, Systems) {

      Systems.getSystemEnv().then(function(response) {
        $scope.eulaLink = response.eulaLink;
        $scope.downloadsTitle = response.downloadsTitle;
        $scope.remoteOwnersManualLink = response.remoteOwnersManualLink;
        $scope.remoteOwnersManualFile = response.remoteOwnersManualFile;
        $scope.bsOwnersManualLink = response.bsOwnersManualLink;
        $scope.bsOwnersManualFile = response.bsOwnersManualFile;
        $scope.installationGuideLink = response.installationGuideLink;
        $scope.installationGuideFile = response.installationGuideFile;
        $scope.programmingGuideLink = response.programmingGuideLink;
        $scope.programmingGuideFile = response.programmingGuideFile;
        $scope.quickStartGuideLink = response.quickStartGuideLink;
        $scope.quickStartGuideFile = response.quickStartGuideFile;
        $scope.advancedDevicesQuickGuideLink = response.advancedDevicesQuickGuideLink;
        $scope.advancedDevicesQuickGuideFile = response.advancedDevicesQuickGuideFile;
        $scope.zWaveIntegrationLink = response.zWaveIntegrationLink;
        $scope.zWaveIntegrationFile = response.zWaveIntegrationFile;
        $scope.cameraIntegrationLink = response.cameraIntegrationLink;
        $scope.cameraIntegrationFile = response.cameraIntegrationFile;
        $scope.dealerAccessLink = response.dealerAccessLink;
        $scope.dealerAccessFile = response.dealerAccessFile;
        $scope.mobileAppQuickGuideLink = response.mobileAppQuickGuideLink;
        $scope.mobileAppQuickGuideFile = response.mobileAppQuickGuideFile;
        $scope.usingFavoriteChannelsLink = response.usingFavoriteChannelsLink;
        $scope.usingFavoriteChannelsFile = response.usingFavoriteChannelsFile;
      }).catch(function(error) {
        toastr.error(error);
      });

      $scope.currentUser = null;

      $scope.logoutUser = function () {
        User.logout();
        $window.location.href = '/';
      };

      $scope.$on('user:set', function(event, currentUser) {
        console.log('caught user:set event');
        $scope.currentUser = currentUser;
      });

      $scope.$on('user:unset', function(event) {
        console.log('caught user:unset event');
        $scope.currentUser = null;
      });


      $scope.switch = {};

      var helpModal = $modal({
        scope: $scope,
        placement: 'top',
        template: 'views/modal-tpls/help.html',
        show: false
      });

      $scope.switchUpdate = false;
      $scope.$on('modal.hide',function(){
        if ($scope.switchUpdate) {
          location.reload();
        }
      });

      $scope.showHelpModal = function() {
        if($scope.currentUser) {
          $scope.switch.menu = !!$scope.currentUser.optMenuShow;
          $scope.switch.help = !!$scope.currentUser.optHelpShow;
        }

        helpModal.$promise.then(helpModal.show);
      };

      $scope.$watch('switch.help', function(newValue, oldValue) {
        var optHelpShow,
          user;

        if (newValue != oldValue && typeof oldValue != 'undefined') {
          $scope.switchUpdate = true;

          if (newValue) {
            optHelpShow = 1;
          } else {
            optHelpShow = 0;
          }

          user = JSON.parse($window.localStorage.getItem('hcm_editor'));
          user.optHelpShow = optHelpShow;
          Auth.setCurrentUser(user);

          var jsonData = JSON.stringify({help_show: user.optHelpShow, menu_show: user.optMenuShow});
          User.setOption(jsonData);
        }
      });

      $scope.$watch('switch.menu', function(newValue, oldValue) {
        var optMenuShow,
          user;

        if (newValue != oldValue && typeof oldValue != 'undefined') {
          $scope.switchUpdate = true;

          if (newValue) {
            optMenuShow = 1;
          } else {
            optMenuShow = 0;
          }

          user = JSON.parse($window.localStorage.getItem('hcm_editor'));
          user.optMenuShow = optMenuShow;
          Auth.setCurrentUser(user);

          var jsonData = JSON.stringify({help_show: user.optHelpShow, menu_show: user.optMenuShow});
          User.setOption(jsonData);
        }
      });


      $scope.dropdown = [
        {
          "text": "<i class=\"fa fa-list\"></i>&nbsp;&nbsp;Overview",
          "href": "/#/menu/system-overview",
          "target": "_self"
        },
        {
          "text": "<i class=\"fa fa-users\"></i>&nbsp;&nbsp;Account Settings",
          "href": "/#/account-settings",
          "target": "_self"
        },
        {
          "text": "<i class=\"fa fa-gears\"></i>&nbsp;&nbsp;System Settings",
          "href": "/#/system-settings",
          "target": "_self"

        },
        {
          "text": "<i class=\"fa fa-sign-out\"></i>&nbsp;&nbsp;Logout",
          "click": "logoutUser()"
        }
      ];


    }
  };
});
