(function() {
	window.app.factory('inventorySvc', inventorySvc);

	inventorySvc.$inject = ['$http'];
	function inventorySvc($http) {
		var inventories = [];

		loadInventories();

		var svc = {
			add: add,
			update: update,
			inventories: inventories,
			getInventory: getInventory,
		};

		return svc;

		function loadInventories() {
			$http.post('/Inventory/All')
				.success(function(data) {
					inventories.addRange(data);
				});
		}

		function add(inventory) {
			return $http.post('/Inventory/Add', inventory)
				.success(function(inventory) {
					inventories.unshift(inventory);
				});
		}

		function update(existingInventory, updatedInventory) {
			return $http.post('/Inventory/Update', updatedInventory)
				.success(function(inventory) {
					angular.extend(existingInventory, inventory);
				});
		}

		function getInventory(id) {
			for (var i = 0; i < inventories.length; i++) {
				if (inventories[i].Id == id) return inventories[i];
			}

			return null;
		}
	}
})();


(function () {
	window.app.factory('inventoryTypeSvc', inventoryTypeSvc);

	inventoryTypeSvc.$inject = ['$http'];
	function inventoryTypeSvc($http) {
		var inventoryTypes = [];

		loadInventoryTypes();

		var svc = {
			add: add,
			update: update,
			inventoryTypes: inventoryTypes,
			getInventoryType: getInventoryType
		};

		return svc;

		function loadInventoryTypes() {
			$http.post('/InventoryType/All')
				.success(function (data) {
					inventoryTypes.addRange(data);
				});
		}

		function add(inventoryType) {
			return $http.post('/InventoryType/Add', inventoryType)
				.success(function (inventoryType) {
					inventoryTypes.unshift(inventoryType);
				});
		}

		function update(existingInventory, updatedInventory) {
			return $http.post('/InventoryType/Update', updatedInventory)
				.success(function (inventory) {
					angular.extend(existingInventory, inventory);
				});
		}

		function getInventoryType(id) {
			for (var i = 0; i < inventoryTypes.length; i++) {
				if (inventoryTypes[i].Id == id) return inventoryTypes[i];
			}

			return null;
		}
	}
})();