var lang = getLang()
var monthFullName = [
  "January",
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December"
]
var monthFullNameTh = [
  "มกราคม",
  "กุมภาพันธ์",
  "มีนาคม",
  "เมษายน",
  "พฤษภาคม",
  "มิถุนายน",
  "กรกฎาคม",
  "สิงหาคม",
  "กันยายน",
  "ตุลาคม",
  "พฤศจิกายน",
  "ธันวาคม"
]
var dayFullName = [
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday"
]
var dayFullNameTh = [
  "อาทิตย์",
  "จันทร์",
  "อังคาร",
  "พุธ",
  "พฤหัสบดี",
  "ศุกร์",
  "เสาร์"
]
var urlApiMasterData = '/hr/api/masterdata'


setTarget = function () {
  var path = window.location.pathname;
  var lang = getLang().toUpperCase() || 'ENG';
  var module;
  file_name = 'XML/' + lang + '_CENTER_SWAPLANG.xml';
  if (path.substring(0, path.lastIndexOf("/")) !== '/hr') {
    module = path.substring(path.indexOf("/", 1) + 1, path.lastIndexOf("/"));
    if (module == 'WORKFLOW' || module == 'WORKFLOW_ADMIN') {
      module = 'CENTER';
    }
    file_name = '../XML/' + lang + '_' + module + '_SWAPLANG.xml';
  }
  return file_name;
};

setXML = function () {
  if (window.ActiveXObject) {
    objDOM = new ActiveXObject("Msxml.DOMDocument");
    objDOM.async = false;
    objDOM.load(file_name);
  } else if (window.XMLHttpRequest) {
    var xmlDoc = new XMLHttpRequest();
    xmlDoc.open('GET', file_name, false);
    try {
      xmlDoc.responseType = 'msxml-document';
    } catch (e) {
    }
    xmlDoc.send(null);
    objDOM = xmlDoc.responseXML;
  }
  return objDOM;
};

var swaplang = function () {
  var file_name = setTarget();
  var objDOM = setXML();
};

swaplang.prototype.swap2String = function (code) {
  var result = "";
  try {
    result = objDOM.selectSingleNode("/changelanguage/" + code).firstChild.nodeValue;
  } catch (e) {
    console.log("can't find code : " + code);
  }
  return result;
};

swaplang.prototype.write = function (code) {
  return this.swap2String(code);
};

var mySwapLang = null;
if (getLang() != "") {
  mySwapLang = new swaplang();
}


var myhr = angular.module('myHR', [])
myhr.controller('Swaplang', swaplang);

function findWithAttr(array, attr, value) {
  // if return -1 = not found
  for (var i = 0; i < array.length; i += 1) {
    if (array[i][attr] === value) {
      return i;
    }
  }
  return -1;
}

function findObjWithAttr(array, attr, value) {
  // if return -1 = not found
  for (var i = 0; i < array.length; i += 1) {
    if (array[i][attr] === value) {
      return array[i];
    }
  }
  return {};
}

function daysBetween(first, second) {

  // Copy date parts of the timestamps, discarding the time parts.
  var one = new Date(first.getFullYear(), first.getMonth(), first.getDate());

  var two = new Date(second.getFullYear(), second.getMonth(), second.getDate());


  // Do the math.
  var millisecondsPerDay = 1000 * 60 * 60 * 24;
  var millisBetween = two.getTime() - one.getTime();
  var days = millisBetween / millisecondsPerDay;

  // Round down.
  return Math.floor(days);
}


//<editor-fold desc="Angular Directive">
myhr.directive('swaplang2', function () {
  var directive = {};
  directive.restrict = 'E';
  directive.template = '';
  directive.compile = function (element, attributes) {
    var linkfunc = function ($scope, element, attributes) {
      if (mySwapLang == null) {
        mySwapLang = new swaplang();
      }
      element.html(mySwapLang.swap2String(attributes.code));
    }
    return linkfunc;
  }
  return directive;
});
myhr.directive('swlang', function () {
  var directive = {};
  directive.restrict = 'A';
  directive.template = "";
  directive.compile = function (element, attributes) {
    var linkFunction = function ($scope, element, attributes) {
      if (mySwapLang == null) {
        mySwapLang = new swaplang();
      }
      element.html(mySwapLang.swap2String(element.attr("code")));
    }
    return linkFunction;
  }
  return directive;
});
myhr.directive('swaplang', function () {
  return {
    restrict: 'E',
    template: '',
    link: function ($scope, element, attrs) {
      if (mySwapLang == null) {
        mySwapLang = new swaplang(getLang())
      }
      var swapResult = mySwapLang.swap2String(attrs.code)
      if (swapResult !== '') {
        element.html(mySwapLang.swap2String(attrs.code))
      }
    }
  }
})

myhr.directive("masterPrefix", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@code"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMprefix/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterBranch", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMbranch/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterDegree", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMdegree/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterDistrict", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMdistrict/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterFaculty", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMfaculty/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterJobcode", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMjobcode/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterNationality", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMnationality/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterProvince", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMprovince/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterRace", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMrace/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterRelation", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMrelation/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterReligion", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMreligion/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterSkilllevel", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMskilllevel/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterSourcejob", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMsourceJob/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterLanguage", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMlanguage/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterOccupation", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMoccupation/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterPaper", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMpaperList/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});

myhr.directive("masterGender", function ($http, $timeout) {
  return {
    restrict: "AE",
    template: "{{name}}",
    scope: {
      code: "@"
    },
    link: function (scope, element, attr) {
      scope.$watch('code', function ($code) {
        $http({
          url: urlApiMasterData + '/RestMgender/' + $code,
          method: "GET",
          cache: true
        }).success((data) => {
          if (data.result.name) {
            scope.name = data.result.name[lang]
            element.val(data.result.name[lang])
          }
        });
      });
    }
  }
});
//</editor-fold>

//<editor-fold desc="Angular filter">
myhr.filter('ddmmyyyyDate', function () {
  return function (date) {
    if (date) {
      if (date.indexOf('T') > -1) {
        var dateValue = date.split('T')
        date = dateValue[0]
      }
      var splitArray = date.split('-')
      var day = splitArray[2]
      var month = splitArray[1]
      var year = splitArray[0]
      var fullStrDate = ''
      if (lang == "tha") {
        year = parseInt(year) + 543;
        fullStrDate = day + ' ' + monthFullNameTh[parseInt(month) - 1] + ' ' + year
      }
      else {
        fullStrDate = day + ' ' + monthFullName[parseInt(month) - 1] + ' ' + year
      }
      return fullStrDate
    }
  }
})
myhr.filter('mmyyyyDate', function () {
  return function (date) {
    if (date) {
      if (date.indexOf('T') > -1) {
        var dateValue = date.split('T')
        date = dateValue[0]
      }
      var splitArray = date.split('-')
      var month = splitArray[1]
      var year = splitArray[0]
      var fullStrDate = ''
      if (lang == "tha") {
        year = parseInt(year) + 543;
        fullStrDate = monthFullNameTh[parseInt(month) - 1] + ' ' + year
      }
      else {
        fullStrDate = monthFullName[parseInt(month) - 1] + ' ' + year
      }
      return fullStrDate
    }
  }
})


//</editor-fold>

//<editor-fold desc="Angular Services">

//</editor-fold>