'use strict';

angular.module('confRegistrationWebApp')
  .service('ConfCache', function ConfCache($cacheFactory, $rootScope, $http, $q, uuid) {
    var cache = $cacheFactory('conf');

    var path = function (id) {
      return 'conferences/' + (id || '');
    };

    var checkCache = function (path, callback) {
      var cachedConferences = cache.get(path);
      if (angular.isDefined(cachedConferences)) {
        callback(cachedConferences, path);
      } else {
        $http.get(path).success(function (conferences) {
          cache.put(path, conferences);
          callback(conferences, path);
        });
      }
    };

    this.query = function (id) {
      checkCache(path(id), function (conferences, path) {
        $rootScope.$broadcast(path, conferences);
      });
    };

    this.get = function (id) {
      var defer = $q.defer();
      checkCache(path(id), function (conferences) {
        defer.resolve(conferences);
      });
      return defer.promise;
    };

    this.getCallback = function (id, callback) {
      checkCache(path(id), function (conferences) {
        callback(conferences);
      });
    };

    this.update = function (id, conference) {
      cache.put(path(id), conference);
    };

    this.empty = function () {
      cache.removeAll();
    };

    this.create = function (name) {
      var registrationEndTime = new Date();
      registrationEndTime.setYear(registrationEndTime.getFullYear() + 1);
      var newConferenceId = uuid();
      var newPageId = uuid();

      var data = {
        id: newConferenceId,
        name: name,
        registrationStartTime: new Date(),
        registrationEndTime: registrationEndTime,
        eventStartTime: new Date(),
        eventEndTime: registrationEndTime,
        conferenceCost: 0,
        earlyRegistrationAmount: 0,
        paymentGatewayType: 'TRUST_COMMERCE',
        registrationPages: [
          {
            id: newPageId,
            conferenceId: newConferenceId,
            title: 'Your Information',
            position: 0,
            blocks: [
              {
                id: uuid(),
                pageId: newPageId,
                type: 'nameQuestion',
                profileType: 'NAME',
                title: 'Name',
                position: 0,
                required: true
              },
              {
                id: uuid(),
                pageId: newPageId,
                type: 'emailQuestion',
                title: 'Email',
                profileType: 'EMAIL',
                position: 1,
                required: true
              }
            ]
          }
        ]
      };
      return $http.post(path(), data).then(function (response) {
        if (response.status === 201) {
          var conference = response.data;
          cache.removeAll();
          return conference;
        } else {
          alert('Error creating conference. ' + response.data.errorMessage + ': ' + response.data.customErrorMessage);
        }
      });
    };
  });

@@ -1328,126 +1328,8 @@
 ) %7B%0A
-      var registrationEndTime = new Date();%0A      registrationEndTime.setYear(registrationEndTime.getFullYear() + 1);%0A
     
@@ -1531,35 +1531,32 @@
 ndTime: 
-registrationEndTime
+moment().day(14)
 ,%0A      
@@ -1569,33 +1569,39 @@
 tStartTime: 
-new Date(
+moment().day(14
 ),%0A        e
@@ -1613,35 +1613,32 @@
 ndTime: 
-registrationEndTime
+moment().day(20)
 ,%0A      
@@ -1690,24 +1690,74 @@
 nAmount: 0,%0A
+        earlyRegistrationCutoff: moment().day(7),%0A
         paym

Update default event dates