/**
 * Created by shadeeka on 12/4/15.
 */
angular.module('app.services')
  // =========================================================================
  // All events filter and gateway
  // =========================================================================

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

    var eventMap = {
      "ftt": {
        name: "ftt",
        endPoint: "ncs-$tenant-ops-$dt",
        date: "$dt",
        data: {factory_id: "$factory", line_id: "$line", type: 'count', style: "$style", datetime: "$time", value: 1,so:"$so",
          li:"$li"}
      },

      "defects": {
        name: "defects", endPoint: "ncs-$tenant-ops-$dt", date: "$dt", data: {
          factory_id: "$factory",
          line_id: "$line",
          type: 'count',
          style: "$style",
          datetime: "$time",
          so:"$so",
          li:"$li",
          value: 1,
          issues: [],
          siluhoutteid: "$siluhoutteid"
        }
      },

      "rwtogood": {
        name: "rwtogood", endPoint: "ncs-$tenant-ops-$dt", date: "$dt", data: {
          factory_id: "$factory",
          line_id: "$line",
          type: 'count',
          style: "$style",
          datetime: "$time",
          so:"$so",
          li:"$li",
          value: 1,
          categories: []
        }
      },

      "reject": {
        name: "reject",
        endPoint: "ncs-$tenant-ops-$dt",
        date: "$dt",
        data: {
          factory_id: "$factory",
          line_id: "$line",
          type: 'count',
          style: "$style",
          so:"$so",
          li:"$li",
          datetime: "$time",
          value: 1,
          "rejectreason": {}
        }
      },

      "members": {
        name: "members", endPoint: "ncs-$tenant-ops-$dt", date: "$dt", data: {
          factory_id: "$factory",
          line_id: "$line",
          type: 'count',
          datetime: "$time"
        }
      },

      "styles": {
        name: "styles", endPoint: "ncs-$tenant-ops-$dt", date: "$dt", data: {
          factory_id: "$factory",
          line_id: "$line",
          type: 'stylestart',
          so: "",
          li: "",
          style: "",
          siluhoutteid: ""
          //startdatetime: "$time"
        }
      },

      "operations": {
        name: "operations", endPoint: "ncs-$tenant-ops-$dt", date: "$dt", data: {
          factory_id: "$factory",
          line_id: "$line",
          plandate: "",
          sessionplanname: '',
          datetime: "$time"
        }
      },

      "styleInfo": {
        name: "styleinfo", endPoint: "ncs-$tenant-plans", date: "$dt", data: {
          factory_id: "$factory",
          line_id: "$line",
          style: "",
          so: "",
          li: "",
          smv: "",
          kickoff: "",
          quantity: "",
          style_type: "",
          plandate: "$datedashformatted",
          daynumber: 0,
          planchangereason: "New plan upload",
          planchangedate: "$time"
        }
      }

    };
    var currentDate = "";
    return {

      getEvent: function (eventType) {

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

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

      preProcess: function (abstractEvent) {

        //set timestamp first
        var eventObject = angular.copy(abstractEvent);
        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("$dt", this.getFormattedDate())
          .replace("$tenant", propertyCache.config.tenant)
          .replace("$line", propertyCache.config.line)
          .replace("$factory", propertyCache.config.factory)
          .replace("$style", propertyCache.day_start.style)
          .replace("$dt", this.getFormattedDate())
          .replace("$time", new Number(new Date().getTime()))
          .replace("$siluhoutteid", propertyCache.day_start.siluhoutteid)
          .replace("$so" , propertyCache.day_start.key2)
          .replace("$li" , propertyCache.day_start.key1)
          .replace("$datedashformatted" , this.getFormattedCurrentDate());

        //localStorage.setItem("current_date" , this.getFormattedDate());
        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('.');
      },

      getFormattedCurrentDate: 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('-');
      }
    };
  }]);
