window.getNamespace("JSkippy/TemporaryAlert").TemporaryAlert = 
class TemporaryAlert
{
	constructor(strMessage, strType, nMilliseconds)
	{
		if(!strType)
		{
			strType = "danger";
		}
		
		if(!nMilliseconds)
		{
			nMilliseconds = 5000;
		}
		
		nMilliseconds = parseInt(nMilliseconds);
		if(nMilliseconds < 1)
		{
			throw new Error("nMilliseconds for TemporaryAlert must be >= 1");
		}
		
		const arrAllowedAlertTypes = ["success", "info", "warning", "danger"];
		
		if(!arrAllowedAlertTypes.includes(strType))
		{
			const strError = "TemporaryAlert type can only be one of " + JSON.stringify(arrAllowedAlertTypes);

			// If temporary alert fails, then, oh well, going back to alerts.
			//new TemporaryAlert(strError);
			alert(strMessage);
			throw new Error(strError);
		}
		
		const elDivMessage = document.createElement("div");
		elDivMessage.textContent = strMessage;
		elDivMessage.className = "alert alert-" + strType;
		elDivMessage.style.boxShadow = "5px 5px 5px #999999";
		elDivMessage.style.marginBottom = "13px";
		elDivMessage.style.whiteSpace = "pre-wrap";
		
		TemporaryAlert.alertsContainer.appendChild(elDivMessage);
		
		setTimeout(
			() => {
				elDivMessage.parentNode.removeChild(elDivMessage);
			},
			nMilliseconds
		);
	}
	
	
	static get alertsContainer()
	{
		if(!TemporaryAlert.elDivAlertsContainer)
		{
			TemporaryAlert.elDivAlertsContainer = document.createElement("div");
			document.body.appendChild(TemporaryAlert.elDivAlertsContainer);
			
			TemporaryAlert.elDivAlertsContainer.style.position = "fixed";
			TemporaryAlert.elDivAlertsContainer.style.right = "20px";
			TemporaryAlert.elDivAlertsContainer.style.bottom = "20px";
			TemporaryAlert.elDivAlertsContainer.style.width = "500px";
			TemporaryAlert.elDivAlertsContainer.style.zIndex = 999999;
		}
		
		return TemporaryAlert.elDivAlertsContainer;
	}
};

