McUtil.service('McUtilContainers', function McUtilContainers(){
	'use strict';

	//console.log(McUtil);

	var form_containers = [];
	var totalScreenCount = 0;
	var currentScreen = 0;
	var checkingOut = false;

	return {
		init: init,
		next: next,
		previous: previous,
		getCurrentScreen: getCurrentScreen,
		isFirstScreen: isFirstScreen,
		isLastScreen: isLastScreen,
		getContainers: getContainers,
		getCurrentScreenTitle: getCurrentScreenTitle,
		getScreenCountAsArray: getScreenCountAsArray,
		getScreenOfField: getScreenOfField,
		goToScreen: goToScreen,
		setCheckingOut: setCheckingOut,
		isCheckingOut: isCheckingOut
	};

	function getCurrentScreen() {
		return currentScreen;
	}

	function goToScreen(newScreenNum) {
		if (isCheckingOut())
			setCheckingOut(false);
		currentScreen = newScreenNum;
	}

	function setCheckingOut(newBooleanValue) {
		checkingOut = newBooleanValue;
	}

	function isCheckingOut() {
		return checkingOut;
	}

	/**
   * Get the title of the screen we are currently on
   * @return {String}  The screen title
   */
	function getCurrentScreenTitle(sys_class_name, name) {
		if (sys_class_name == 'sc_cat_item_producer') {
			if (form_containers.length != 0 && !isFirstScreen()) {
				if(!form_containers[currentScreen][0].caption) {
					return name + ' (cont...)'
				} else {
					return form_containers[currentScreen][0].caption;
				}
			}
		} else if (form_containers.length != 0 && !isFirstScreen() && !isLastScreen()) {
			return form_containers[currentScreen][0].caption;
		} else {
			return '';
		}
	}

	function getScreenCountAsArray() {
		return new Array(totalScreenCount);
	}

	function setTotalScreenCount(newScreenCount) {
		totalScreenCount = newScreenCount;
	}

	function isFirstScreen() {
		return currentScreen === 0;
	}

	function isLastScreen() {
		return currentScreen === totalScreenCount - 1;
	}


	function next() {
		currentScreen++;
	}

	function previous() {
		currentScreen--;
	}

	function getScreenOfField(fieldName) {
		function findFieldNameMatch(screen) {
			for(var i = 0; i < screen.length; i++) {
				if(screen[i].columns){
					for (var j = 0; j < screen[i].columns.length; j++) {
						for (var y = 0; y < screen[i].columns[j].fields.length; y++) {
							if(screen[i].columns[j].fields[y].name == fieldName) {
								return true;
							}
						}
					}
				}
			}
			// Searched through all containers of this screen without a match
			return false;
		}
		return _.findIndex(form_containers, findFieldNameMatch);
	}

	/**
   * Group containers that do not have caption with the
   * previous container.
   * @param {array} c
   */
	function regroupContainers(c, sys_class_name) {
		var newCGroupings = c.reduce(function(acc, cv, ci, arr) {
			/**
       * Allow the first section not to have a caption.
       * The name of the item visually acts as the caption.
       */
			if(!cv.caption && !cv.captionDisplay && ci !== 0) {
				acc[acc.length - 1].push(cv);
			} else {
				acc.push([cv]);
			}
			return acc;
		}, []);
		// Add in two empty arrays as place markers for the intro and final screens
		newCGroupings.splice(0,0,[]);
		if (sys_class_name != 'sc_cat_item_producer')
			newCGroupings.splice(newCGroupings.length,0,[]);
		setTotalScreenCount(newCGroupings.length);
		return newCGroupings;
	}

	function init(containers, sys_class_name) {
		form_containers = regroupContainers(containers, sys_class_name);
		// Reset the currentScreen variable to zero to prevent state from persisting

		currentScreen = 0;

	}

	function getContainers() {
		return form_containers[currentScreen];
	}
});