(function () {
  'use strict';
  let module = angular.module('app', ['ngAnimate', 'ngSanitize', 'ui.router', 'dndLists']);

  //Declare all angular components/services/factories/filters here
  module.service('api', ApiService);
  module.service('$activity', ActivityService);
  
  module.component("appActivityList", ActivityListComponent);
  module.component("appDone", DoneComponent);
  module.component('appNavbar', NavbarComponent);
  module.component('appFooter', FooterComponent);
  module.component('appHome', HomeComponent);
  module.component("appSidebar", SideBarComponent);
  module.component("appStory", StoryComponent);
  module.component("appDrag", DragComponent);
  module.component("appStoryCollect", StoryCollectComponent);
  module.component("appActivity", ActivityComponent);
  module.component("appLogin", LoginComponent);
  module.component("appAdmin", AdminComponent);

  module.directive('fileReader', function () {
    return {
      scope: {
        fileReader: "="
      },
      link: function (scope, element) {
        $(element).on('change', function (changeEvent) {
          var files = changeEvent.target.files;
          if (files.length) {
            var r = new FileReader();
            r.onload = function (e) {
              var contents = e.target.result;
              scope.$apply(function () {
                scope.fileReader = contents;
              });
            };

            r.readAsText(files[0]);
          }
        });
      }
    };
  });

  //Configure angular here
  module.config(function ($locationProvider, $urlRouterProvider, $stateProvider, $httpProvider) {
    //$locationProvider.html5Mode(true); //would need to configure web server to work with html5 mode
    $urlRouterProvider.otherwise('/login');

    $stateProvider
      .state("home", {
        url: "/",
        component: 'appLogin'
      })
      .state("done", {
        url: "/done",
        component: "appDone"
      })
      .state("activity", {
        url: "/activity",
        component: 'appActivity',
      })
      .state("login", {
        url: "/login",
        component: "appLogin"
      })
      .state("admin", {
        url: "/admin",
        component: "appAdmin"
      })
      .state("activityList", {
        url: "/list",
        component: "appActivityList"
      });


    //For api auth
    $httpProvider.interceptors.push(ApiInterceptor);


  });

})();
