/* File Created: September 22, 2014 */

function htmlEncode(value) {
	return $('<div/>').text(value).html();
}
var patientmodel = function () {
	var self = this;

	// Order of Patient settings from SettingHelper.GetPatientSetting() 
	var patientSettingEnum = {  
	    "ShowPatientPicture": 0,
	    "ShowOfficeCodes": 1,
	    "ShowDayOfBirth": 2,
	    "ShowCustomText": 3,
	    "ShowCustomInfoCodes": 4,
	    "ShowContactInfo": 5,
	    "ShowChartNum": 6,
	    "ShowAlertCodes": 7,
	    "PatientNameFormat": 8
	};

    // Get the model data
	self.modelpatdata = $.parseJSON($("#hdnPatModel").val());
	// AX-542
	self.lang = ko.observable(self.modelpatdata.Lang);
	if (self.lang().length >= 2 && self.lang().substr(0, 2) == "es")
	    moment.locale("es");

	self.cansendmessage = ko.observable(self.modelpatdata.CanSendMessage);

	// gender enum
	var genum = {};
	genum[0] = "U";
	genum[1] = "M";
	genum[2] = "F";
	genum[3] = "T";

	// alert array
	self.alertarr = ko.observableArray();
	self.alertarr.push(self.modelpatdata.PatientDet.AlertCodeList.$values[0]);
	self.alertarr.push(self.modelpatdata.PatientDet.AlertCodeList.$values[1]);
	self.alertarr.push(self.modelpatdata.PatientDet.AlertCodeList.$values[2]);
	self.alertarr.push(self.modelpatdata.PatientDet.AlertCodeList.$values[3]);
	self.alertarr.push(self.modelpatdata.PatientDet.AlertCodeList.$values[4]);

	// office code array
	self.offcodearr = ko.observableArray();
	self.offcodearr.push(self.modelpatdata.PatientDet.OfficeCodeList.$values[0]);
	self.offcodearr.push(self.modelpatdata.PatientDet.OfficeCodeList.$values[1]);
	self.offcodearr.push(self.modelpatdata.PatientDet.OfficeCodeList.$values[2]);
	self.offcodearr.push(self.modelpatdata.PatientDet.OfficeCodeList.$values[3]);
	self.offcodearr.push(self.modelpatdata.PatientDet.OfficeCodeList.$values[4]);

	// Detect whether it is iOS or not 
	self.IsIOS = ko.observable(/(iPad|iPhone|iPod)/g.test(navigator.userAgent));

	// custom code 
	// Label array
	self.customfields = ko.computed(function () {
		var customfield = "";
		for (var i = 0; i < 10; i++) {
			if (self.modelpatdata.CustomFieldDesc[i] !== undefined) {
				customfield += '<span class="patient-desc-label-s">' + htmlEncode(self.modelpatdata.CustomFieldDesc[i].Description) + '</span><span class="patient-desc-field divider-10px">' + htmlEncode(self.modelpatdata.PatientDet.CustomCodeList.$values[i].Description) + '</span><br />';
			}
		}

		return customfield;
	});

	// Set visibility for Custom Code
	self.customfieldslength = ko.computed(function () {
		return self.customfields().length;
	});

	// Custom Text Fields and Labels
	self.customtextfields = ko.computed(function () {
		var customtextfield = "";

		for (var i = 26; i < 30; i++) {
			if (self.modelpatdata.CustomFieldDesc[i] !== undefined) {
				customtextfield += '<span class="patient-desc-label-s">' + htmlEncode(self.modelpatdata.CustomFieldDesc[i].Description) + '</span><span class="patient-desc-field divider-10px">' + htmlEncode(self.modelpatdata.PatientDet.CustomTextList.$values[i-26]) + '</span><br />';
			}	
		}
		return customtextfield;
	});

	// Set visibility for Custom Text
	self.customtextfieldslength = ko.computed(function () {
		return self.customtextfields().length;
	});

	self.showalerts = ko.computed(function () {
		var showinfo = true;
		if (self.modelpatdata.PatientConfig.Settings.$values[patientSettingEnum.ShowAlertCodes].Key === "DA.General.Patient.ShowAlertCodes")
			showinfo = self.modelpatdata.PatientConfig.Settings.$values[patientSettingEnum.ShowAlertCodes].Value === "1" ? true : false;
		return showinfo;
	});

	self.showofficecodes = ko.computed(function () {
		var showinfo = true;
		if (self.modelpatdata.PatientConfig.Settings.$values[patientSettingEnum.ShowOfficeCodes].Key === "DA.General.Patient.ShowOfficeCodes")
			showinfo = self.modelpatdata.PatientConfig.Settings.$values[patientSettingEnum.ShowOfficeCodes].Value === "1" ? true : false;
		return showinfo;
	});

	self.showcustominfoCode = ko.computed(function () {
		var showinfo = true;
		if (self.modelpatdata.PatientConfig.Settings.$values[patientSettingEnum.ShowCustomInfoCodes].Key === "DA.General.Patient.ShowCustomInfoCodes") {
			showinfo = self.modelpatdata.PatientConfig.Settings.$values[patientSettingEnum.ShowCustomInfoCodes].Value === "1" && (self.customfieldslength() > 0) ? true : false;
		}
		return showinfo;
	});

	self.showcustomext = ko.computed(function () {
		var showinfo = true;
		if (self.modelpatdata.PatientConfig.Settings.$values[patientSettingEnum.ShowCustomText].Key === "DA.General.Patient.ShowCustomText")
			showinfo = self.modelpatdata.PatientConfig.Settings.$values[patientSettingEnum.ShowCustomText].Value === "1" && (self.customtextfieldslength() > 0) ? true : false;
		return showinfo;
});
    
	self.preferredlanguage = ko.computed(function () {
		return self.modelpatdata.PatientDet.PrefLanguage.Description === null ? "" : self.modelpatdata.PatientDet.PrefLanguage.Description;
	});

	self.contacttime = ko.computed(function () {
		return self.modelpatdata.PatientDet.ContactTime === null ? "" : self.modelpatdata.PatientDet.ContactTime;
	});

	self.getcolor = function (code, type) {
		var color;
		if (code !== undefined && code !== null && code.length > 0) {
			if (type === 'A') {
				color = self.modelpatdata.AlertCodes[code].Color;
			} else {
				color = self.modelpatdata.OfficeCodes[code].Color;
			}
		} else {
			color = "";
		}
		return self.gethexcolor(color);
	};

	self.gethexcolor = function (color) {
		var hexval = "";
		if ($.trim(color) !== "0" && $.trim(color).length > 0) {
			var hexpadding = "000000";
			var msaccess2Hex = parseInt(color).toString(16);
			var padhexarr = (hexpadding.substring(0, 6 - msaccess2Hex.length) + msaccess2Hex).match(/[\s\S]{1,2}/g) || [];
			// Reverse the sequence.
			hexval = "#" + padhexarr[2] + padhexarr[1] + padhexarr[0];
		}
		return hexval;
	};


};

$(function () {
	var patabs = $("#tabs2").tabs();
	var patmodel = new patientmodel;
	ko.applyBindings(patmodel, document.getElementById("patientView"));

	var hidealert = !patmodel.showalerts();
	var hidemoreinfo = !(patmodel.showofficecodes() || patmodel.showcustominfoCode() || patmodel.showcustomext());

	if (hidealert) {
		setTabs(patabs, 1, 0);
	}

	if (hidealert && hidemoreinfo) {
		setTabs(patabs, null, 0);
	}

	if (!hidealert && hidemoreinfo) {
		setTabs(patabs, 0, 1);
	}

	$("#a-patient-navback").click(function () {
		$(".patient-top-nav").hide();
		$(".patient-view").hide();

		if ($(".appt-det-top").length)
			$(".appt-det-top").show();

		if ($("#patientdiv").length)
			$('#patientdiv').empty();
		keepAlive();
	});

	// The two function below needs to be moved to a common js lib.
	function setTabs(ptabs, activetab, hidetab) {
		if (tabCount(ptabs) > 0) {
			if (hidetab !== null) {
				var panelId = ptabs.find('li:eq(' + hidetab + ')').remove().attr("aria-controls");
				$("#" + panelId).remove();

				if (tabCount(ptabs) === 0) {
					ptabs.hide();
					return;
				}
			}

			if (activetab !== null) {
				ptabs.tabs("option", "active", activetab);
			}
		}
		else {
			ptabs.hide();
			return;
		}
	}

	function tabCount(ptabs) {
		return ptabs.tabs.length;
	}
});
