import * as MastodonAPI from "/JS/MastodonAPI.js";
import * as BlueskyAPI from "/JS/BlueskyAPI.js";
import * as TumblrAPI from "/JS/TumblrAPI.js";
import * as YoutubeAPI from "/JS/YoutubeAPI.js";
import * as Variables from "/JS/Variables.js";

// Settings buttons
let RemoteButton = document.getElementsByClassName("Remote")[0];

// Website Stuff
let MastodonLoginButton = document.getElementsByClassName("Login Mastodon")[0];
let MastodonWebInput = document.getElementsByClassName("WebInput Mastodon")[0];
let MastodonLogoutButton = document.getElementsByClassName("Logout Mastodon")[0];

let BlueskyLoginButton = document.getElementsByClassName("Login Bluesky")[0];
let BlueskyWebInput = document.getElementsByClassName("WebInput Bluesky")[0];
let BlueskyLogoutButton = document.getElementsByClassName("Logout Bluesky")[0];

let YoutubeLoginButton = document.getElementsByClassName("Login Youtube")[0];
let YoutubeAPIInput = document.getElementsByClassName("WebInput Youtube")[0];
let YoutubeHandleInput = document.getElementsByClassName("WebInput Youtube")[1];
let YoutubeLogoutButton = document.getElementsByClassName("Logout Youtube")[0];

// original link
let Origin = location.origin + "/setting"

// Allow the user to change if Remote is a thing or not.
RemoteButton.onclick = (event) => {
	// Toggle the Variable
	if (localStorage.getItem("Remote") != null) {
		localStorage.removeItem("Remote");
	} else {
		localStorage.setItem("Remote", "true");
	}
}

// Mastodon buttons
// Login
MastodonLoginButton.onclick = (event) => {
	if (MastodonWebInput.value != "") {
		let text = MastodonWebInput.value
		MastodonAPI.HandleAuthentication(text);
	}
}

// Logout
MastodonLogoutButton.onclick = (event) => {
	localStorage.removeItem(Variables.MastodonClientID);
	localStorage.removeItem(Variables.MastodonClientSecret);
	localStorage.removeItem(Variables.MastodonAccessToken);
	localStorage.removeItem(Variables.MastodonTokenType);
	localStorage.removeItem(Variables.MastodonWebsite);
	document.location.href = Origin;
}

// Bluesky Buttons
// Login
BlueskyLoginButton.onclick = (event) => {
	if (BlueskyWebInput.value != "") {
		let text = BlueskyWebInput.value;
		BlueskyAPI.HandleAuthorization(text);
	}
}

// Logout
BlueskyLogoutButton.onclick = (event) => {
	localStorage.removeItem(Variables.BlueskyPDS);
	localStorage.removeItem(Variables.BlueskyDID);
	localStorage.removeItem(Variables.BlueskyPKCEVerifier);
	localStorage.removeItem(Variables.BlueskyPKCEChallenge);
	localStorage.removeItem(Variables.BlueskyNonce);
	localStorage.removeItem(Variables.BlueskyAccessToken);
	localStorage.removeItem(Variables.BlueskyRefreshToken);
	localStorage.removeItem(Variables.BlueskyPublicKey);
	localStorage.removeItem(Variables.BlueskyPrivateKey);
	document.location.href = Origin;
}

// Youtube Buttons
// Login
YoutubeLoginButton.onclick = (event) => {
	if (YoutubeHandleInput.value != "" && YoutubeAPIInput.value != "") {
		YoutubeAPI.GetChannelID(YoutubeAPIInput.value, YoutubeHandleInput.value);
		// Clear the stuff
		YoutubeHandleInput.value = "";
		YoutubeAPIInput.value = "";
	}
}

// Logout
YoutubeLogoutButton.onclick = (event) => {
	localStorage.removeItem(Variables.YoutubeID);
	document.location.href = Origin;
}

// if an access token is found, login.
async function CheckLogin() {
	// Check for a mastodon token.
	if (localStorage.getItem(Variables.MastodonAccessToken) != null) {
		// Swap the buttons
		MastodonLoginButton.remove();
		MastodonWebInput.remove();
		MastodonLogoutButton.classList.remove("Hidden");
	} else {
		// Auto log in
		await MastodonAPI.GainToken(Variables.MastodonWebsite);
	}
	// Check for a bluesky token.
	if (localStorage.getItem(Variables.BlueskyAccessToken) != null) {
		// Swap the buttons
		BlueskyLoginButton.remove();
		BlueskyWebInput.remove();
		BlueskyLogoutButton.classList.remove("Hidden");
	} else {
		// Auto log in
		await BlueskyAPI.GainTokens();
	}
	// Check for a Youtube ID.
	if (localStorage.getItem(Variables.YoutubeID) != null) {
		YoutubeLoginButton.remove();
		YoutubeAPIInput.remove();
		YoutubeHandleInput.remove();
		YoutubeLogoutButton.classList.remove("Hidden");
	}
}

// Runs on website start.
// Remove traces of "login".
CheckLogin();
