"use strict";

let newsletter_send_all_form;
let newsletter_send_all_safe_code;
let newsletter_send_all_input_safe_code_repeat;
let newsletter_send_all_input_content_name;
let newsletter_send_all_input_csrf_token;
let newsletter_send_all_input_submit;
let newsletter_send_all_feedback;
let newsletter_send_all_feedback_wait;
let newsletter_send_all_feedback_success;
let newsletter_send_all_feedback_failure;

let newsletter_send_all_valid_safe_code = false;

window.addEventListener("load", function(){
	// STORE ELEMENTS //
	newsletter_send_all_form = document.getElementById("newsletter-send-all-form");
	newsletter_send_all_safe_code = document.getElementById("newsletter-send-all-form-safe-code");
	newsletter_send_all_input_safe_code_repeat = document.getElementById("newsletter-send-all-form-safe-code-repeat");
	newsletter_send_all_input_content_name = document.getElementById("newsletter-send-all-form-content-name");
	newsletter_send_all_input_csrf_token = document.getElementById("newsletter-send-all-form-csrf-token");
	newsletter_send_all_input_submit = document.getElementById("newsletter-send-all-form-submit");
	newsletter_send_all_feedback = document.getElementById("newsletter-send-all-form-feedback");
	newsletter_send_all_feedback_wait = document.getElementById("newsletter-send-all-form-feedback-wait");
	newsletter_send_all_feedback_success = document.getElementById("newsletter-send-all-form-feedback-success");
	newsletter_send_all_feedback_failure = document.getElementById("newsletter-send-all-form-feedback-failure");
	
	
	// INITIALIZE INPUTS //
	newsletter_send_all_init_safe_code();
	newsletter_send_all_init_safe_code_repeat();
	newsletter_send_all_init_submit();
});



/**
* HELPER: Initialize safe code text.
*/
async function newsletter_send_all_init_safe_code(){
	// POPULATE WITH RANDOM NUMBER //
	newsletter_send_all_safe_code.textContent = Math.random().toString().slice(2, 6);
}



/**
* HELPER: Initialize safe code repeat input.
*/
async function newsletter_send_all_init_safe_code_repeat(){
	// REGISTER INPUT HANDLER //
	newsletter_send_all_input_safe_code_repeat.addEventListener("input", newsletter_send_all_update_safe_code_repeat);
}



/**
* HELPER: Initialize submit button input.
*/
async function newsletter_send_all_init_submit(){
	// REGISTER CLICK HANDLER //
	newsletter_send_all_input_submit.addEventListener("click", newsletter_send_all_submit);
	
	
	// UPDATE STATE //
	newsletter_send_all_update_submit();
}



/**
* CALLBACK: Update safe code repeat state.
*/
async function newsletter_send_all_update_safe_code_repeat(){
	// VALIDATE INPUT //
	// load values
	let should = newsletter_send_all_safe_code.textContent;
	let is = newsletter_send_all_input_safe_code_repeat.value;
	
	// compare
	newsletter_send_all_valid_safe_code = (is === should);
	
	
	// UPDATE VALIDITY INDICATOR //
	// get element
	let validity_indicator = newsletter_send_all_input_safe_code_repeat.parentElement;
	
	// reset state
	validity_indicator.classList.remove("valid", "invalid");
	
	// set state
	if(newsletter_send_all_valid_safe_code){
		validity_indicator.classList.add("valid");
	} else {
		validity_indicator.classList.add("invalid");
	}
	
	
	// UPDATE SUBMIT BUTTON //
	newsletter_send_all_update_submit();
}



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



/**
* CALLBACK: Maybe submit the form.
*/
async function newsletter_send_all_submit(){
	// MAKE SURE ALL INPUTS ARE VALID //
	if(!newsletter_send_all_valid_safe_code) return;
	
	
	// SHOW WAIT FEEDBACK //
	newsletter_send_all_feedback.classList.remove("hidden", "gone");
	newsletter_send_all_form.classList.add("hidden");
	newsletter_send_all_feedback_wait.classList.remove("hidden", "gone");
	
	
	// COLLECT VALUES //
	// content name
	let content_name = newsletter_send_all_input_content_name.value;
	
	// csrf token
	let csrf_token = newsletter_send_all_input_csrf_token.value;
	
	
	// SEND API REQUEST //
	var xhr = new XMLHttpRequest();
	xhr.open("POST", "/admin/newsletter/api/send-all", true);
	xhr.setRequestHeader("Content-Type", "application/json");
	xhr.send(JSON.stringify({
		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_all_feedback_wait.classList.add("gone");
			newsletter_send_all_feedback_success.classList.remove("hidden", "gone");
			return;
		}
		
		// negative feedback: no member
		if(response !== null && (response.reason ?? "") === "no_member"){
			newsletter_send_all_feedback_wait.classList.add("gone");
			newsletter_send_all_feedback_failure_no_member.classList.remove("hidden", "gone");
			return;
		}
		
		// negative feedback: default
		newsletter_send_all_feedback_wait.classList.add("gone");
		newsletter_send_all_feedback_failure.classList.remove("hidden", "gone");
	}
}
