// Removes at given index and then put last element at said index.
// Returns modified Array.
function RemoveAtIndex(arr, index) {
	let Temp1 = arr[index];
	let Temp2 = arr[arr.length - 1];
	arr[index] = Temp2;
	arr.pop();
	return arr;
}

function AddChannel() {
	browser.storage.local.get("ChannelList").then(
		(List) => {
			if (document.getElementsByName("AddChannel")[0].value == "") {
				console.log("nothing in input, cancelling...");
				return;
			}
			// If there is no array, make new array.
			if (Object.values(List).length == 0) {
				browser.storage.local.set({ChannelList: [document.getElementsByName("AddChannel")[0].value]});
			}
			// push a value to the existing array.
			else {
				let TempArray = Array.from(Object.values(List)[0]);
				TempArray.push(document.getElementsByName("AddChannel")[0].value);
				browser.storage.local.set({ChannelList: TempArray});
			}
		}
	);
}

function RemoveChannel() {
	browser.storage.local.get("ChannelList").then(
		(List) => {
			if (Object.values(List).length != 0) {
				let TempArray = Array.from(Object.values(List)[0]);
				let index = TempArray.indexOf(document.getElementsByName("RemoveChannel")[0].value);
				if (index != -1) {
					TempArray = RemoveAtIndex(TempArray, index);
					browser.storage.local.set({ChannelList: TempArray});
				}
			}
		}
	);
}

function AddTopic() {
	browser.storage.local.get("TopicList").then(
		(List) => {
			if (document.getElementsByName("AddTopic")[0].value == "") {
				console.log("nothing in input, cancelling...");
				return;
			}
			// If there is no array, make new array.
			if (Object.values(List).length == 0) {
				browser.storage.local.set({TopicList: [document.getElementsByName("AddTopic")[0].value]});
			}
			// push a value to the existing array.
			else {
				let TempArray = Array.from(Object.values(List)[0]);
				TempArray.push(document.getElementsByName("AddTopic")[0].value);
				browser.storage.local.set({TopicList: TempArray});
			}
		}
	);
}

function RemoveTopic() {
	browser.storage.local.get("TopicList").then(
		(List) => {
			if (Object.values(List).length != 0) {
				let TempArray = Array.from(Object.values(List)[0]);
				let index = TempArray.indexOf(document.getElementsByName("RemoveTopic")[0].value);
				if (index != -1) {
					TempArray = RemoveAtIndex(TempArray, index);
					browser.storage.local.set({TopicList: TempArray});
				}
			}
		}
	);
}

// Now... load stuff!
window.onload = (event) => {
	document.getElementsByName("AddChannel")[1].addEventListener("click", AddChannel);
	document.getElementsByName("RemoveChannel")[1].addEventListener("click", RemoveChannel);
	browser.storage.local.get("ChannelList").then(
		(List) => {
			for (let i of Object.values(List)[0]) {
				document.getElementsByClassName("Results")[0].innerHTML += "<p> "+ i + " </p>";
			}
		}
	);
	document.getElementsByName("AddTopic")[1].addEventListener("click", AddTopic);
	document.getElementsByName("RemoveTopic")[1].addEventListener("click", RemoveTopic);
	browser.storage.local.get("TopicList").then(
		(List) => {
			for (let i of Object.values(List)[0]) {
				document.getElementsByClassName("Results")[1].innerHTML += "<p> "+ i + " </p>";
			}
		}
	);
};