'use strict';

(function (angular, buildfire) {
  angular.module('eventsFeedPluginWidget')
    .provider('Buildfire', [function () {
      var Buildfire = this;
      Buildfire.$get = function () {
        return buildfire
      };
      return Buildfire;
    }])
    .factory("DataStore", ['Buildfire', '$q', 'STATUS_CODE', 'STATUS_MESSAGES', function (Buildfire, $q, STATUS_CODE, STATUS_MESSAGES) {
      var onUpdateListeners = [];
      return {
        get: function (_tagName) {
          var deferred = $q.defer();
          Buildfire.datastore.get(_tagName, function (err, result) {
            if (err) {
              return deferred.reject(err);
            } else if (result) {
              return deferred.resolve(result);
            }
          });
          return deferred.promise;
        },
        onUpdate: function () {
          var deferred = $q.defer();
          var onUpdateFn = Buildfire.datastore.onUpdate(function (event) {
            if (!event) {
              return deferred.notify(new Error({
                code: STATUS_CODE.UNDEFINED_EVENT,
                message: STATUS_MESSAGES.UNDEFINED_EVENT
              }), true);
            } else {
              return deferred.notify(event);
            }
          });
          onUpdateListeners.push(onUpdateFn);
          return deferred.promise;
        },
        clearListener: function () {
          onUpdateListeners.forEach(function (listner) {
            listner.clear();
          });
          onUpdateListeners = [];
        }
      }
    }])
    .factory('CalenderFeedApi', ['$q', '$http', 'STATUS_CODE', 'STATUS_MESSAGES', 'PAGINATION', 'PROXY_SERVER',
      function ($q, $http, STATUS_CODE, STATUS_MESSAGES, PAGINATION, PROXY_SERVER) {
        var requiresHttps = function () {
          var useHttps = false;
          var userAgent = navigator.userAgent || navigator.vendor;
          var isiPhone = (/(iPhone|iPod|iPad)/i.test(userAgent));
          var isAndroid = (/android/i.test(userAgent));

          //iOS 10 and higher should use HTTPS
          if (isiPhone) {
            //This checks the first digit of the OS version. (Doesn't distinguish between 1 and 10)
            if (!(/OS [4-9](.*) like Mac OS X/i.test(userAgent))) {
              useHttps = true;
            }
          }

          //For web based access, use HTTPS
          if (!isiPhone && !isAndroid) {
            useHttps = true;
          }

          console.warn('userAgent: ' + userAgent);
          console.warn('useHttps: ' + useHttps);

          return useHttps;
        };
        var getProxyServerUrl = function () {
          return requiresHttps() ? PROXY_SERVER.secureServerUrl : PROXY_SERVER.serverUrl;
        };
        var getSingleEventDetails = function (url, eventIndex, date) {
          var deferred = $q.defer();
          if (!url) {
            deferred.reject(new Error('Undefined feed url'));
          }
          $http.post(getProxyServerUrl()  + '/event', {
            url: url,
            index: eventIndex,
            date: date
          })
            .success(function (response) {
              if (response.statusCode == 200)
                deferred.resolve(response.event);
              else
                deferred.resolve(null);
            })
            .error(function (error) {
              deferred.reject(error);
            });
          return deferred.promise;
        };
        var getFeedEvents = function (url, date, offset, refreshData, requestType) {
          var deferred = $q.defer();
          if (!url) {
            deferred.reject(new Error('Undefined feed url'));
          }
          $http.post(getProxyServerUrl() + '/events', {
            url: url,
            limit: requestType == 'SELECTED' ? PAGINATION.eventsCount : PAGINATION.eventsCountAll,
            offset: offset,
            date: date,
            refreshData: refreshData
          })
            .success(function (response) {
              if (response.statusCode == 200)
                deferred.resolve(response);
              else
                deferred.reject(null);
            })
            .error(function (error) {
              deferred.reject(error);
            });
          return deferred.promise;
        };
        return {
          getSingleEventDetails: getSingleEventDetails,
          getFeedEvents: getFeedEvents
        };
      }])
    .factory('Location', [function () {
      var _location = window.location;
      return {
        goTo: function (path) {
          _location.href = path;
        }
      };
    }])
    .factory('EventCache', [function () {
      var event = null;
      return {
        setCache: function (data) {
          event = data;
        },
        getCache: function () {
          return event;
        }
      };
    }])
})(window.angular, window.buildfire);
@@ -1740,837 +1740,8 @@
 ) %7B%0A
-        var requiresHttps = function () %7B%0A          var useHttps = false;%0A          var userAgent = navigator.userAgent %7C%7C navigator.vendor;%0A          var isiPhone = (/(iPhone%7CiPod%7CiPad)/i.test(userAgent));%0A          var isAndroid = (/android/i.test(userAgent));%0A%0A          //iOS 10 and higher should use HTTPS%0A          if (isiPhone) %7B%0A            //This checks the first digit of the OS version. (Doesn't distinguish between 1 and 10)%0A            if (!(/OS %5B4-9%5D(.*) like Mac OS X/i.test(userAgent))) %7B%0A              useHttps = true;%0A            %7D%0A          %7D%0A%0A          //For web based access, use HTTPS%0A          if (!isiPhone && !isAndroid) %7B%0A            useHttps = true;%0A          %7D%0A%0A          console.warn('userAgent: ' + userAgent);%0A          console.warn('useHttps: ' + useHttps);%0A%0A          return useHttps;%0A        %7D;%0A
     
@@ -1802,26 +1802,8 @@
 turn
- requiresHttps() ?
  PRO
@@ -1831,33 +1831,8 @@
 rUrl
- : PROXY_SERVER.serverUrl
 ;%0A  

Require HTTPS