<!-- html format to parse django template data -->

<!-- IMPORTS -->
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-auth.js"></script> 
<script src="https://www.gstatic.com/firebasejs/7.22.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.1.1/firebase-performance.js"></script>

<!-- JAVASCRIPT -->
<script type="text/javascript">

// redirects.
var signin_url = "/accounts/login/"

// Configure firebase 
// Visit https://console.firebase.google.com/project/<your project id>/overview and click on "Add Firebase to your web app"
__firebase__ = firebase

// the firebase class.
class Firebase {

	// init.
	constructor(height, width) {

		// init firebase.
		__firebase__.initializeApp({
		 	apiKey: "{{FIREBASE.API_KEY}}",
		    authDomain: "{{FIREBASE.AUTH_DOMAIN}}",
		    databaseURL: "{{FIREBASE.DATABASE_URL}}",
		    projectId: "{{FIREBASE.PROJECT_ID}}",
		    storageBucket: "{{FIREBASE.STORAGE_BUCKET}}",
		    messagingSenderId: "{{FIREBASE.MESSAGING_SENDER_ID}}",
		    appId: "{{FIREBASE.APP_ID}}",
		    measurementId: "{{FIREBASE.MEASUREMENT_ID}}",
		});

		// objects.
		this.auth = __firebase__.auth()
		this.db = __firebase__.firestore();
		this.perf = __firebase__.performance();

		// vars.
		this.signin_url = signin_url
		this.id_token = null
	}

	// sign out the signed in user.
	signout(redirect=true) {
		var response = Response.default()
		this.auth.signOut()
		.then(function() {
			// sign out successful.
			response.success = true
			response.message = "Successfully signed out."
			if (redirect == true) {
				__reset_local_storage__()
				location.href = this.signin_url
			}
			return response
		}).catch(function(error) {
			// sign out failed.
			response.error = error.message
			return response
		})
	}

	// sign in with email & password.
	signin(email, password, authenticated_handler=null, unauthenticated_handler=null, timeout=1000) {
		var response = Response.default()
		__reset_local_storage__()

		// sign in.
		var set = false
		this.auth.signInWithEmailAndPassword(email, password)
		.then(function(user) {
		   // user signed in
		   localStorage.setItem("EMAIL", user.email)
			this.auth.currentUser.getIdToken(/* forceRefresh */ true).then(function(id_token) {
				this.id_token = id_token
				localStorage.setItem("ID_TOKEN", id_token)
			}).catch(function(error) {
				this.id_token = null
				localStorage.setItem("ID_TOKEN", null)
			})
			response.success = true
			response.message = "Successfully signed in."
			if (authenticated_handler != null) {
				authenticated_handler(response)
			}
		})
		.catch(function(error) {
			// sign in failed.
			response.error = error.message
			if (response.error.includes("A network error (such as timeout")) {
				response.error = "Network error. Try resetting your browser history & cache."
			}
			if (unauthenticated_handler != null) {
				unauthenticated_handler(response)
			}
		})

	}

	// check if user is authenticated.
	authenticated(authenticated_handler=null, unauthenticated_handler=null) {
		var response = Response.default()
		__firebase__.auth().onAuthStateChanged(
			function(user) {
				if (user) {

					// get id token.
					localStorage.setItem("EMAIL", user.email)
					this.auth.currentUser.getIdToken(/* forceRefresh */ true).then(function(id_token) {
						this.id_token = id_token
						localStorage.setItem("ID_TOKEN", id_token)
					}).catch(function(error) {
						this.id_token = null
						localStorage.setItem("ID_TOKEN", null)
					})

					// version 1.
					// email verified not required.
					if (authenticated_handler != null) {
						response.success = true
						response.message = "User is authenticated."
						authenticated_handler(response)
					}
					/*
					// version 2.
					// check email verified.
					if (user.emailVerified == true || user.emailVerified == "true" || user.emailVerified == "True") {
						if (authenticated_handler != null) {
							response.success = true
							response.message = "User is authenticated."
							authenticated_handler(response)
						}
					} else {
						if (unauthenticated_handler != null) {
							response.error = "Verify your email address."
							response.uid = user.uid
							unauthenticated_handler(response)
						}
					}
					*/
				} else {
					if (unauthenticated_handler != null) {
						response.error = "User is not authenticated."
						unauthenticated_handler(response)
					}
				}
			}, function(error) {
				if (unauthenticated_handler != null) {
					response.error = "User is not authenticated."
					unauthenticated_handler(response)
				}
			}
		)
	}



	// the id token.
	get_id_token(handler=null) {
		try {
			__firebase__.auth().onAuthStateChanged(function(user) {
				if (user) {
				   	this.auth.currentUser.getIdToken(/* forceRefresh */ true).then(function(id_token) {
						this.id_token = id_token
						localStorage.setItem("ID_TOKEN", this.id_token)
						if (handler != null) {
							handler(Response.success("Successfully retrieved the id token.", {id_token:id_token}))
						}
					}).catch(function(error) {
						this.id_token = null
						localStorage.setItem("ID_TOKEN", this.id_token)
						if (handler != null) { handler(Response.error("Failed to retrieve the id token."))}
					})
				} else {
				  	this.id_token = null
					localStorage.setItem("ID_TOKEN", this.id_token)
				    if (handler != null) { handler(Response.error("Failed to retrieve the id token."))}
				}
			})
		} catch(error) {
			this.id_token = null
			localStorage.setItem("ID_TOKEN", this.id_token)
		    if (handler != null) { handler(Response.error("Failed to retrieve the id token."))}
		}
	}


	// get the current user's email.
	var EMAIL = localStorage.getItem("EMAIL")
	get_email(success_handler=null, error_handler=null) {
		try {
			__firebase__.auth().onAuthStateChanged(function(user) {
				if (user) {
					EMAIL = user.email
					localStorage.setItem("EMAIL", EMAIL)
				    if (success_handler != null) { success_handler(ID_TOKEN) }
				} else {
				  	EMAIL = null
					localStorage.setItem("EMAIL", EMAIL)
				    if (error_handler != null) { error_handler(EMAIL) }
				}
			})
		} catch(error) {
			EMAIL = null
			localStorage.setItem("EMAIL", EMAIL)
		    if (error_handler != null) { error_handler(EMAIL) }
		}
	}

	// get the current user's email verified boolean.
	get_email_verified(success_handler=null, error_handler=null) {
		try {
			__firebase__.auth().onAuthStateChanged(function(user) {
				if (user) {
				    if (success_handler != null) { success_handler(user.emailVerified) }
				} else {
				    if (error_handler != null) { error_handler(null) }
				}
			})
		} catch(error) {
		    if (error_handler != null) { error_handler(null) }
		}
	}

	// reset local storage.
	__reset_local_storage__() {
		localStorage.setItem("ID_TOKEN", null)
		localStorage.setItem("EMAIL", null)
		localStorage.setItem("PRIVATE_KEY", null)
	}

}

// initialize class.
const firebase = new Firebase();


</script>	