﻿var DiaryAccess = function(config) {
  this.getDiaryEntries = function(fromDate, toDate, onSuccess) {
    $.ajax({
      type: "GET",
      url: config.getEntriesUrl,
      cache: false,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: { "fromDate": fromDate, "toDate": toDate },
      dataFilter: function(data, type) {
        return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, "new $1");
      },
      success: function(data) {
        onSuccess(data);
      }
    });
  }
  this.getDiaryWeightsWithGoals = function(fromDate, toDate, onSuccess) {
    $.ajax({
      type: "GET",
      url: config.getDiaryWeightsWithGoalsUrl,
      cache: false,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: { "fromDate": fromDate, "toDate": toDate },
      dataFilter: function(data, type) {
        return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, "new $1");
      },
      success: function(data) {
        onSuccess(data);
      }
    });
  }
  this.getDiaryWaists = function(fromDate, toDate, onSuccess) {
    $.ajax({
      type: "GET",
      url: config.getDiaryWaistsUrl,
      cache: false,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: { "fromDate": fromDate, "toDate": toDate },
      dataFilter: function(data, type) {
        return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, "new $1");
      },
      success: function(data) {
        onSuccess(data);
      }
    });
  }
  this.getDiaryCalories = function(fromDate, toDate, onSuccess) {
    var fromDateStr = fromDate.getFullYear() + "-" + (fromDate.getMonth() + 1) + "-" + fromDate.getDate();
    var toDateStr = toDate.getFullYear() + "-" + (toDate.getMonth() + 1) + "-" + toDate.getDate();
    $.ajax({
      type: "GET",
      url: config.getDiaryCaloriesUrl,
      cache: false,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: { "fromDate": fromDateStr, "toDate": toDateStr },
      dataFilter: function(data, type) {
        return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, "new $1");
      },
      success: function(data) {
        onSuccess(data);
      }
    });
  }
  this.getDiaryDates = function(fromDate, toDate, onSuccess) {
    $.ajax({
      type: "GET",
      url: config.getDiaryDatesUrl,
      cache: false,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: { "fromDate": fromDate, "toDate": toDate },
      dataFilter: function(data, type) {
        return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, "new $1");
      },
      success: function(data) {
        onSuccess(data);
      }
    });
  }
  this.getDiaryMeasures = function(fromDate, toDate, onSuccess) {
    $.ajax({
      type: "GET",
      url: config.getDiaryMeasuresUrl,
      cache: false,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: { "fromDate": fromDate, "toDate": toDate },
      dataFilter: function(data, type) {
        return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, "new $1");
      },
      success: function(data) {
        onSuccess(data);
      }
    });
  }
  this.getLatestEntries = function(onSuccess) {
    $.ajax({
      type: "GET",
      cache:false,
      url: config.getLatestEntriesUrl,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      data: { "count": 5 },
      dataFilter: function(data, type) {
        return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, "new $1");
      },
      success: function(data) {
        onSuccess(data);
      }
    });
  }
 
}

