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

// Elements.
let PostButton = document.getElementsByClassName("Button")[0];
let VisibilityDropdown = document.getElementsByClassName("PostVisibility")[0];
let InputArea = document.getElementsByClassName("Text")[0];
let YoutubePoser = document.getElementsByClassName("PostYoutube")[0];
let WarningInputArea = document.getElementsByClassName("Warning")[0];
let TagsInputArea = document.getElementsByClassName("Tags")[0];

// Clicking the beeg POST button.
PostButton.onclick = (event) => {
	Post();
}

async function Post() {
	let website;
	if (document.location.href.split("website=").length > 1) {
		website = document.location.href.split("website=")[1];
	} else {
		website = "All";
	}
	// Check to see if you are replying.
	let Text = InputArea.value;
	let Visible = VisibilityDropdown.value;
	// don't do anything if there is no value
	if (Text == "") {
		return;
	}
	// Mastodon posting.
	if (localStorage.getItem(Variables.MastodonAccessToken) != null) {
		let TempVisible;
		// Adding tags
		let TempText = Text;
		let Tags = TagsInputArea.value.split(";");
		if (Tags[0] != "") {
			TempText += "<br/><br/>";
			for (let i of Tags) {
				while (i[0] == " ") {
					i = i.substring(1, i.length);
				}
				if (i[0] != "#" && i.length != 0) {
					i = "#" + i;
				}
				TempText += i + " ";
			}
		}
		switch(Visible) {
			case "Public":
				TempVisible = "public";
				break;
			case "Quiet":
				TempVisible = "unlisted";
				break;
			case "Friend":
				TempVisible = "private";
				break;
			case "Private":
				TempVisible = "direct";
				break;
		}
		if (website == "Mastodon") {
			if (WarningInputArea.value == "") {
				await MastodonAPI.CreateStatus(TempText, TempVisible, undefined, JSON.parse(localStorage.getItem("post")).id);
			} else {
				await MastodonAPI.CreateStatus(TempText, TempVisible, WarningInputArea.value, JSON.parse(localStorage.getItem("post")).id);
			}
			InputArea.value = "";
			WarningInputArea.value = "";
			TagsInputArea.value = "";
			return;
		} else if (website == "All") {
			if (WarningInputArea.value == "") {
				await MastodonAPI.CreateStatus(TempText, TempVisible, undefined);
			} else {
				await MastodonAPI.CreateStatus(TempText, TempVisible, WarningInputArea.value);
			}
		}
	}
	// Bluesky posting.
	if (localStorage.getItem(Variables.BlueskyAccessToken) != null) {
		let Post;
		let TempVisible;
		// Adding tags
		let Tags = TagsInputArea.value.split(";");
		let TagsTemp = [];
		for (let i of Tags) {
			while (i[0] == " ") {
				i = i.substring(1, i.length);
			}
			if (i[0] != "#" && i.length != 0) {
				i = "#" + i;
			}
			TagsTemp.push(i);
		}
		Tags = TagsTemp;
		switch(Visible) {
			case "Public":
				TempVisible = undefined;
				break;
			case "Quiet":
				TempVisible = undefined;
				break;
			case "Friend":
				TempVisible = [{"$type": "app.bsky.feed.threadgate#followingRule"}, {"$type": "app.bsky.feed.threadgate#followerRule"}];
				break;
			case "Private":
				TempVisible = [];
				break;
		}
		if (website == "Bluesky") {
			if (JSON.parse(localStorage.getItem("post")).hasOwnProperty("reply")) {
				if (WarningInputArea.value == "") {
					Post = await BlueskyAPI.CreatePost(localStorage.getItem(Variables.BlueskyDID), Text, undefined, Tags, JSON.parse(localStorage.getItem("post")).post, JSON.parse(localStorage.getItem("post")).reply.root);
				} else {
					Post = await BlueskyAPI.CreatePost(localStorage.getItem(Variables.BlueskyDID), Text, WarningInputArea.value, Tags, JSON.parse(localStorage.getItem("post")).post, JSON.parse(localStorage.getItem("post")).reply.root);
				}
				await BlueskyAPI.CreateThreadGate(localStorage.getItem(Variables.BlueskyDID), Post.uri, TempVisible);
			} else {
				if (WarningInputArea.value == "") {
					Post = await BlueskyAPI.CreatePost(localStorage.getItem(Variables.BlueskyDID), Text, undefined, Tags, JSON.parse(localStorage.getItem("post")).post, JSON.parse(localStorage.getItem("post")).post);
				} else {
					Post = await BlueskyAPI.CreatePost(localStorage.getItem(Variables.BlueskyDID), Text, WarningInputArea.value, Tags, JSON.parse(localStorage.getItem("post")).post, JSON.parse(localStorage.getItem("post")).post);
				}
				await BlueskyAPI.CreateThreadGate(localStorage.getItem(Variables.BlueskyDID), Post.uri, TempVisible);
			}
			InputArea.value = "";
			WarningInputArea.value = "";
			TagsInputArea.value = "";
			return;
		} else if (website == "All") {
			if (WarningInputArea.value == "") {
				Post = await BlueskyAPI.CreatePost(localStorage.getItem(Variables.BlueskyDID), Text, undefined, Tags);
			} else {
				Post = await BlueskyAPI.CreatePost(localStorage.getItem(Variables.BlueskyDID), Text, WarningInputArea.value, Tags);
			}
			await BlueskyAPI.CreateThreadGate(localStorage.getItem(Variables.BlueskyDID), Post.uri, TempVisible);
		}
		// Try to apply tags as well in another post because Bsky sucks balls.
		if (Tags != "") {
			let Textures = "";
			for (let i of Tags) {
				Textures = Textures + i + " ";
			}
			Post = await BlueskyAPI.GetPosts([Post.uri]);
			Post = await Post.posts[0];
			if (Post.hasOwnProperty("reply")) {
				await BlueskyAPI.CreatePost(localStorage.getItem(Variables.BlueskyDID), Textures, undefined, Tags, Post, Post.reply.root, true);
			} else {
				await BlueskyAPI.CreatePost(localStorage.getItem(Variables.BlueskyDID), Textures, undefined, Tags, Post, Post, true);
			}
		}
	}
	// Youtube posting.
	if (YoutubePoser.checked == true) {
		navigator.clipboard.writeText(Text);
		window.open("https://www.youtube.com/channel/" + localStorage.getItem(Variables.YoutubeID) + "/posts");
	}
	// The input being cleared means that the posting happened.
	InputArea.value = "";
	WarningInputArea.value = "";
	TagsInputArea.value = "";
}

// Check if you can interact with the textbox
if (localStorage.getItem(Variables.MastodonAccessToken) == null && localStorage.getItem(Variables.BlueskyAccessToken) == null && localStorage.getItem(Variables.YoutubeID) == null) {
	InputArea.disabled = true;
}
// Check if you can post on Youtube
if (localStorage.getItem(Variables.YoutubeID) == null) {
	YoutubePoser.disabled = true;
}
