﻿var sSchedule_dataSources_locationDataSource = function () {

	//#region ------------------- ' Variables '-----------------------

	var dataSource = new sSchedule_dataSources();
	var models = new sSchedule_common_dataModels();
	var common = new Sschedule_commonFunctions();
	var serviceRootUrl = common.getServiceRootUrl();

	//#endregion ------------------- ' Variables '-----------------------

	function getDsByName(name, parameterMapFunc, successCallback, readCallBack) {
		var locationsDataSource = null;
		var dataModel = null;
		var apiName = null;
		var readEntity = null;
		var crudServiceUrl = null;
		var regionDataSource = null;
		var updateEntity = null;
		var createEntity = null;
		var destroyEntity = null;

		switch (name) {
			case "LocationsDs":
				{
					dataModel = models.getModel("Locations");
					apiName = "Locations";
					readEntity = "GetAllLocationsWithContactDetails";
					updateEntity = "ManageLocations";
					createEntity = "ManageLocations";
					destroyEntity = "ManageDeletedLocations";
					crudServiceUrl = serviceRootUrl + apiName + "/";
					locationsDataSource = new kendo.data.DataSource({
						transport:
							{
								read: dataSource.readDs(crudServiceUrl, readEntity, readCallBack),
								create: dataSource.createDs(crudServiceUrl, createEntity, successCallback),
								update: dataSource.updateDs(crudServiceUrl, updateEntity, successCallback),
								destroy: dataSource.destroyDs(crudServiceUrl, destroyEntity, successCallback),
								parameterMap: function (options, operation) {
									if (operation !== "read" && options) {
										if (models) {
											var modelsData = options.models;
											if (modelsData)
												return kendo.stringify(modelsData);
										}
									} else {
										var params = { portalId: sSchedule_portalId };
										return params;
									}
								},
							},
						error: dataSource.errorDs(),
						batch: true,
						schema: {
							type: "json",
							model: dataModel
						},
					});
				}
				break;
			case "AllLocationsListDs":
				{
					dataModel = models.getModel("Locations");
					apiName = "Locations";
					readEntity = "GetAllLocationsBySiteId";
					crudServiceUrl = serviceRootUrl + apiName + "/";
					locationsDataSource = new kendo.data.DataSource({
						transport:
							{
								read: dataSource.readDs(crudServiceUrl, readEntity, readCallBack),
								parameterMap: parameterMapFunc,
							},
						batch: false,
						schema: {
							type: "json",
							model: dataModel
						},
						error: dataSource.errorDs(),
					});
				}
				break;
			case "LocationsListDs":
				{
					dataModel = models.getModel("Locations");
					apiName = "Locations";
					readEntity = "GetAllLocationsBySiteId";
					crudServiceUrl = serviceRootUrl + apiName + "/";
					locationsDataSource = new kendo.data.DataSource({
						transport:
							{
								read: dataSource.readDs(crudServiceUrl, readEntity, readCallBack),
								parameterMap: parameterMapFunc,
							},
						batch: false,
						schema: {
							type: "json",
							model: dataModel
						},
						error: dataSource.errorDs(),
					});
				}
				break;
			case "PortalGroupsLocationsDs":
				{
					dataModel = models.getModel("Locations");
					apiName = "Locations";
					readEntity = "GetAllLocationsByPortalGroups";
					crudServiceUrl = serviceRootUrl + apiName + "/";
					locationsDataSource = new kendo.data.DataSource({
						transport:
							{
								read: dataSource.readDs(crudServiceUrl, readEntity, readCallBack),
								parameterMap: parameterMapFunc,
							},
						batch: false,
						schema: {
							type: "json",
							model: dataModel
						},
						error: dataSource.errorDs(),
					});
				}
				break;
			case "LocationTreeListDs":
				{
					apiName = "Locations";
					readEntity = "GetAllLocationsWithFields";
					crudServiceUrl = serviceRootUrl + apiName + "/";

					locationsDataSource = new kendo.data.HierarchicalDataSource({
						type: "json",
						transport:
							{
								read: dataSource.readDs(crudServiceUrl, readEntity, successCallback, readCallBack),
								parameterMap: parameterMapFunc,
							},
						schema: {
							model: {
								hasChildren: "HasChildren",
								id: "Id",
								Name: "Name",
								children: "ChildItems"
							}
						},
						error: dataSource.errorDs(),
					});
				}
				break;
			case "PortalGroupsLocationTreeListDs":
				{
					apiName = "Locations";
					readEntity = "GetAllLocationsWithFieldsByPortalGroups";
					crudServiceUrl = serviceRootUrl + apiName + "/";

					locationsDataSource = new kendo.data.HierarchicalDataSource({
						type: "json",
						transport:
							{
								read: dataSource.readDs(crudServiceUrl, readEntity, successCallback, readCallBack),
								parameterMap: parameterMapFunc,
							},
						schema: {
							model: {
								hasChildren: "HasChildren",
								id: "Id",
								Name: "Name",
								children: "ChildItems"
							}
						},
						error: dataSource.errorDs(),
					});
				}
				break;
		case "RegionDs":
				{
					dataModel = models.getModel("Regions");
					apiName = "Locations";
					readEntity = "GetAllRegionsByCountryId";
					crudServiceUrl = serviceRootUrl + apiName + "/";
					regionDataSource = new kendo.data.DataSource({
						transport:
							{
								read: dataSource.readDs(crudServiceUrl, readEntity),
								parameterMap: parameterMapFunc
							},
						error: dataSource.errorDs(),
						batch: false,
						schema: {
							type: "json",
							model: dataModel
						}
					});
					return regionDataSource;
				}
		}
		return locationsDataSource;
	}

	return {
		getDsByName: getDsByName
	};
}
