"use strict";

let newsletter_send_one_form;
let newsletter_send_one_input_mail_address;
let newsletter_send_one_input_content_name;
let newsletter_send_one_input_csrf_token;
let newsletter_send_one_input_submit;
let newsletter_send_one_feedback;
let newsletter_send_one_feedback_wait;
let newsletter_send_one_feedback_success;
let newsletter_send_one_feedback_failure;
let newsletter_send_one_feedback_failure_no_member;

let newsletter_send_one_valid_mail_address = false;

window.addEventListener("load", function(){
	// STORE ELEMENTS //
	newsletter_send_one_form = document.getElementById("newsletter-send-one-form");
	newsletter_send_one_input_mail_address = document.getElementById("newsletter-send-one-form-mail-address");
	newsletter_send_one_input_content_name = document.getElementById("newsletter-send-one-form-content-name");
	newsletter_send_one_input_csrf_token = document.getElementById("newsletter-send-one-form-csrf-token");
	newsletter_send_one_input_submit = document.getElementById("newsletter-send-one-form-submit");
	newsletter_send_one_feedback = document.getElementById("newsletter-send-one-form-feedback");
	newsletter_send_one_feedback_wait = document.getElementById("newsletter-send-one-form-feedback-wait");
	newsletter_send_one_feedback_success = document.getElementById("newsletter-send-one-form-feedback-success");
	newsletter_send_one_feedback_failure = document.getElementById("newsletter-send-one-form-feedback-failure");
	newsletter_send_one_feedback_failure_no_member = document.getElementById("newsletter-send-one-form-feedback-failure-no-member");
	
	
	// INITIALIZE INPUTS //
	newsletter_send_one_init_mail_address();
	newsletter_send_one_init_submit();
});



/**
* HELPER: Initialize mail address input.
*/
async function newsletter_send_one_init_mail_address(){
	// REGISTER INPUT HANDLER //
	newsletter_send_one_input_mail_address.addEventListener("input", newsletter_send_one_update_mail_address);
}



/**
* HELPER: Initialize submit button input.
*/
async function newsletter_send_one_init_submit(){
	// REGISTER CLICK HANDLER //
	newsletter_send_one_input_submit.addEventListener("click", newsletter_send_one_submit);
	
	
	// UPDATE STATE //
	newsletter_send_one_update_submit();
}



/**
* CALLBACK: Update mail address state.
*/
async function newsletter_send_one_update_mail_address(){
	// VALIDATE INPUT //
	// load value
	let value = newsletter_send_one_input_mail_address.value;
	
	// check against regex
	newsletter_send_one_valid_mail_address = (value.match(/^[a-zA-Z0-9\.\-\_\+]+@([a-z0-9\-]+\.)+[a-z0-9\-]{2,}$/) !== null);
	
	
	// UPDATE VALIDITY INDICATOR //
	// get element
	let validity_indicator = newsletter_send_one_input_mail_address.parentElement;
	
	// reset state
	validity_indicator.classList.remove("valid", "invalid");
	
	// set state
	if(newsletter_send_one_valid_mail_address){
		validity_indicator.classList.add("valid");
	} else {
		validity_indicator.classList.add("invalid");
	}
	
	
	// UPDATE SUBMIT BUTTON //
	newsletter_send_one_update_submit();
}



/**
* HELPER: Update submit button state.
*/
async function newsletter_send_one_update_submit(){
	// DISABLE //
	newsletter_send_one_input_submit.classList.add("disabled");
	
	
	// MAYBE ENABLE //
	if(newsletter_send_one_valid_mail_address){
		newsletter_send_one_input_submit.classList.remove("disabled");
	}
}



/**
* CALLBACK: Maybe submit the form.
*/
async function newsletter_send_one_submit(){
	// MAKE SURE ALL INPUTS ARE VALID //
	if(!newsletter_send_one_valid_mail_address) return;
	
	
	// SHOW WAIT FEEDBACK //
	newsletter_send_one_feedback.classList.remove("hidden", "gone");
	newsletter_send_one_form.classList.add("hidden");
	newsletter_send_one_feedback_wait.classList.remove("hidden", "gone");
	
	
	// COLLECT VALUES //
	// mail address
	let mail_address = newsletter_send_one_input_mail_address.value;
	
	// content name
	let content_name = newsletter_send_one_input_content_name.value;
	
	// csrf token
	let csrf_token = newsletter_send_one_input_csrf_token.value;
	
	
	// SEND API REQUEST //
	var xhr = new XMLHttpRequest();
	xhr.open("POST", "/admin/newsletter/api/send-one", true);
	xhr.setRequestHeader("Content-Type", "application/json");
	xhr.send(JSON.stringify({
		mail_address: mail_address,
		content_name: content_name,
		csrf_token: csrf_token
	}));
	
	xhr.onload = function(){
		let success = true;
		
		// validate http status code
		if(xhr.status !== 200) success = false;
		
		// check response
		let response = null;
		if(success){
			try {
				response = JSON.parse(xhr.response);
			} catch(e){}
			
			if(typeof response !== "object") success = false;
			if(success && response === null) success = false;
			if(success && response.success !== true) success = false;
		}
		
		// positive feedback
		if(success){
			newsletter_send_one_feedback_wait.classList.add("gone");
			newsletter_send_one_feedback_success.classList.remove("hidden", "gone");
			return;
		}
		
		// negative feedback: no member
		if(response !== null && (response.reason ?? "") === "no_member"){
			newsletter_send_one_feedback_wait.classList.add("gone");
			newsletter_send_one_feedback_failure_no_member.classList.remove("hidden", "gone");
			return;
		}
		
		// negative feedback: default
		newsletter_send_one_feedback_wait.classList.add("gone");
		newsletter_send_one_feedback_failure.classList.remove("hidden", "gone");
	}
}
