class ImportController {

	import() {
		let params = {csrf: document.querySelector('[name=csrf]')?.value};
		let body = new URLSearchParams(params);
		if (typeof newsletter !== 'undefined') {
			for (const key in newsletter) {
				body.append('newsletter[' + key + ']', newsletter[key]);
			}
		}

		
		let request = function () {
			return fetch(nextUrl, {method: "POST",   headers: {
				"X-Requested-With": "XMLHttpRequest",
				"Content-Type": "application/x-www-form-urlencoded",
			}, body})
			.then((response) => {
				nextUrl = false;
				if (!response.ok) {
					return Promise.resolve(response.text()).then((responseInText) => {
						return Promise.reject([response, responseInText]);
					});
				}
				return response.json()
			})
			
			.then((json) => {
					document.getElementById('progress-status').innerHTML = '';
					
					if (json['error']) {
						document.getElementById('progress-bar').classList.add('bg-danger');
						document.getElementById('progress-status').innerHTML = '<div class="text-danger">' + json['error'] + '</div>';
					}

					let percent = (json['position']) * 100 / json['count'];				
					
					if (json['success']) {
						document.getElementById('progress-bar').style.width = "100%";
						document.getElementById('progress-status').innerHTML = '<div class="text-success">' + json['success'] + '</div>';
					}

					if (json['table']) {
						document.getElementById('progress-status').innerHTML = json['table'];
					}					
					
					if (json['file']) {
						document.getElementById('progress-file').innerHTML = json['file'];
					}					
					
					if (json['position']) {
						document.getElementById('progress-position').innerHTML = json['position'];
					}			
					
					if (json['count']) {
						document.getElementById('progress-count').innerHTML = json['count'];
					}			
					
					if (json['page']) {
						document.getElementById('progress-page').innerHTML = json['page'];
					}

					
					if (json['url'] && (!json['count'] || !json['last'] )) {
						nextUrl = json['url'];

						window.ajaxStack.add(request);
					} else {
						percent = 100;
						document.getElementById('progress-bar').classList.add('bg-success');
						document.getElementById('progress-bar').style.width = '100%';

						if (json['success']) {
						} else {
						}
					}

					document.getElementById('progress-bar').style.width = percent + '%';					
				})
				.catch(error => {
						let [response, responseInText] = error;
						let message = response.statusText ?? "Error!";

						if (typeof error.json === "function") {
							error.json().then(jsonError => {
								message = jsonError;
							}).catch(genericError => {
								message = error.statusText + " " + message;
							});
						}				
					
						if (typeof displayToast === 'function') displayToast("danger", "Error", message);
						if (document.getElementById('progress-success')) document.getElementById('progress-success').innerHTML = '';
						if (document.getElementById('progress-status')) document.getElementById('progress-status').innerHTML = '<div class="text-danger">' + message + '</div>';
						document.getElementById('progress-bar').classList.add('bg-danger');
				});
		};

		window.ajaxStack.add(request);		
	}	
}
