/**
 * Created by nalee on 8/26/2016.
 */
angular.module('app.services')
  // =========================================================================
  // All events filter and gateway
  // =========================================================================

  .factory('auditProtocol', ['$log', 'propertyCache', function ($log, propertyCache, sparkDataService) {

    var auditMap = {
      "basicInfo": {
        app: '$app',
        userName: "$userName",
        ipAddress: "$ipAddress",
        macAddress: "$macAddress",
        networkStatus: "$networkStatus",
        epochTime: "$epochTime",
        dateTime: "$dateTime",
        factory: "$factory",
        tenant: "$tenant",
        line: "$line"
      },

      "ftt": {
        name: "audit_ftt",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {
            style: "$style",
            value: 1
          }
        }
      },

      "defects": {
        name: "audit_defects",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {
            style: "$style",
            value: 1,
            issues: [],
            siluhoutteid: "$siluhoutteid"
          }
        }
      },

      "rwtogood": {
        name: "audit_rwtogood",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {
            style: "$style",
            value: 1,
            categories: [],
            siluhoutteid: "$siluhoutteid"
          }
        }
      },

      "reject": {
        name: "audit_reject",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {
            style: "$style",
            value: 1,
            rejectreason: {},
            siluhoutteid: "$siluhoutteid"
          }
        }
      },

      "login": {
        name: "audit_login",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      },

      "logout": {
        name: "audit_logout",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      },

      "operations": {
        name: "audit_operations",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {
            sessionPlan: ''
          }
        }
      },

      "styles": {
        name: "audit_styles",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {
            so: '',
            li: '',
            style: '',
            siluhoutteid: ''
          }
        }
      },

      "members": {
        name: "audit_members",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {members: ''}
        }
      },

      "syncApp": {
        name: "audit_sync_app",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      },

      "syncSessionPlan": {
        name: "audit_sync_sessionplan",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      },
      "syncOperation": {
        name: "audit_sync_operation",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      },
      "syncImages": {
        name: "audit_sync_images",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {
            success: '',
            fail: ''
          }
        }
      },
      "disableApp": {
        name: "audit_disableapp",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      },
      "lockApp": {
        name: "audit_lockapp",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {
            code: ''
          }
        }
      },
      "unlockApp": {
        name: "audit_unlockapp",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      },
      "addOperations": {
        name: "audit_add_operations",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {
            operations: ''
          }
        }
      },
      "networkOnline": {
        name: "audit_network_online",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      },
      "networkOffline": {
        name: "audit_network_offline",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      },
      "configuration": {
        name: "audit_configuration",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {
            dataIp: '',
            syncIp: '',
            tenant: '',
            factory: ''
          }
        }
      },
      "syncAuthentication": {
        name: "audit_sync_authentication",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      },
      "appConfiguration": {
        name: "audit_app_configuration",
        endPoint: "$auditEndPoint",
        data: {
          basicInfo: {},
          reference: {}
        }
      }


    };
    var currentDate = "";
    return {

      getEvent: function (eventType) {

        $log.debug("eventProtocolService :", eventType);

        return this.preProcess(auditMap[eventType]);
      },

      preProcess: function (abstractEvent) {

        //set timestamp first
        var eventObject = angular.copy(abstractEvent);
        eventObject.data.basicInfo = auditMap['basicInfo'];

        eventObject.datetime = new Date().getTime();

        if (eventObject.data.datetime)
          eventObject.data.datetime = new Date().getTime();

        //take this to local scope if necessary
        eventObject.count = 1;

        var eventString = JSON.stringify(eventObject);

        eventString = eventString
          .replace("$auditEndPoint", "audit")
          .replace("$app", "QC")
          .replace("$userName", propertyCache.currentUser)
          .replace("$ipAddress", propertyCache.ipAddress)
          .replace("$macAddress", propertyCache.macAddress)
          .replace("$networkStatus", propertyCache.networkStatus)
          .replace("$epochTime", new Date().getTime())
          .replace("$dateTime", this.getFormattedDate())
          .replace("$factory", propertyCache.config.factory)
          .replace("$tenant", propertyCache.config.tenant)
          .replace("$line", propertyCache.config.line)
          .replace("$style", propertyCache.day_start.style)
          .replace("$siluhoutteid", propertyCache.day_start.siluhoutteid)

        auditMap.basicInfo.epochTime =  new Date().getTime()
        return JSON.parse(eventString);

      },

      replaceAll: function (str, find, replace) {
        return str.replace(new RegExp(find, 'g'), replace);
      },

      getFormattedDate: function () {

        var d = new Date();
        var month = '' + (d.getMonth() + 1);
        var day = '' + d.getDate();
        var year = d.getFullYear();

        if (month.length < 2)
          month = '0' + month;
        if (day.length < 2)
          day = '0' + day;
        currentDate = [year, month, day].join('.');
        return [year, month, day].join('-');
      }
    };
  }]);
