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";

let website = document.location.href.split("website=")[1];
let post = JSON.parse(localStorage.getItem("post"));

// HTML elements.
let AccountName = document.getElementsByClassName("Account")[0];
let AccountImage = document.getElementsByClassName("AccountImage")[0];
let AccountDescription = document.getElementsByClassName("AccountDescription")[0];

let AlternateAccountName = document.getElementsByClassName("Account")[1];
let AlternateAccountImage = document.getElementsByClassName("AccountImage")[1];
let AlternateAccountDescription = document.getElementsByClassName("AccountDescription")[1];

let FollowButton = document.getElementsByClassName("Follow")[0];
let BlockButton = document.getElementsByClassName("Block")[0];

let AccountPosts = document.getElementsByClassName("Posts")[1];

// Other vars.
let Following = false;
let Blocking = false;

// SPAWN
GetAccount();

// The interaction is given to another function for async.
FollowButton.onclick = (event) => {
	FollowBoop();
}

async function FollowBoop() {
	if (website == "Mastodon") {
		let Relations = await MastodonAPI.GetRelationship(post.account.id);
		await MastodonAPI.CreateFollow(post.account.id, Relations[0].following);
	} else if (website == "Bluesky") {
		await BlueskyAPI.CreateFollow(localStorage.getItem(Variables.BlueskyDID), post.post.author.did);
	}
	SetFollow();
}

// Extra thing for the frontend to say "hey I am the follow meister".
function SetFollow() {
	Following = !(Following);
	if (Following == true) {
		FollowButton.innerHTML = "Unfollow...";
	} else {
		FollowButton.innerHTML = "Follow!";
	}
}

// The interaction is given to another function for async.
BlockButton.onclick = (event) => {
	BlockBoop();
}

async function BlockBoop() {
	if (website == "Mastodon") {
		let Relations = await MastodonAPI.GetRelationship(post.account.id);
		await MastodonAPI.CreateBlock(post.account.id, Relations[0].blocking);
	} else if (website == "Bluesky") {
		await BlueskyAPI.CreateBlock(localStorage.getItem(Variables.BlueskyDID), post.post.author.did);
	}
	SetBlock();
}

// Extra thing for the frontend to say "hey I am the block meister".
function SetBlock() {
	Blocking = !(Blocking);
	if (Blocking == true) {
		BlockButton.innerHTML = "Unblock...";
	} else {
		BlockButton.innerHTML = "Block!";
	}
}

async function GetAccount() {
	if (website == "Mastodon") {
		// Get the relationshop.
		let Relations = await MastodonAPI.GetRelationship(post.account.id);
		// Set the buttons using the relationship.
		Following = !(Relations[0].following);
		Blocking = !(Relations[0].blocking);
		SetFollow();
		SetBlock();
		AccountName.innerHTML = post.account.acct;
		AccountDescription.innerHTML = post.account.note;
		AccountImage.setAttribute("src", post.account.avatar);
		// Build the fields
		let Field = "<aside class=\"Trees\">";
		for (let i of post.account.fields) {
			Field += "<b class=\"Entry\">" + i.name + "</b><em class=\"Exitry\">" + i.value + "</em>";
		}
		Field += "</aside>";
		AccountDescription.innerHTML += Field;
		// Get their recent posts. Determine if they are cewl or not.
		let Posts = await MastodonAPI.GetAccountStatuses(post.account.id);
		for (let i of Posts) {
			if (i.reblog != null) {
				i = i.reblog;
			}
			AccountPosts.innerHTML += i.content + "<br/><br/>";
		}
	} else if (website == "Bluesky") {
		// Set the relationship follows
		let thing = await BlueskyAPI.GetRecord(localStorage.getItem(Variables.BlueskyDID), "app.bsky.graph.follow", post.post.author.did);
		let thing2 = await BlueskyAPI.GetRecord(localStorage.getItem(Variables.BlueskyDID), "app.bsky.graph.block", post.post.author.did);
		if (thing.hasOwnProperty("error") && thing.error == "RecordNotFound") {
			Following = true;
		} else {
			Following = false;
		}
		if (thing2.hasOwnProperty("error") && thing2.error == "RecordNotFound") {
			Blocking = true;
		} else {
			Blocking = false;
		}
		SetFollow();
		SetBlock();
		let account = await BlueskyAPI.GetProfile(post.post.author.did);
		AccountName.innerHTML = account.handle;
		AccountDescription.innerHTML = account.description;
		AccountImage.setAttribute("src", account.avatar);
		// Get their recent posts. Determine if they are cewl or not.
		let Posts = await BlueskyAPI.GetProfileFeed(account.did);
		for (let i of Posts.feed) {
			AccountPosts.innerHTML += i.post.record.text + "<br/><br/>";
		}
	// No other accounts. Just you.
	} else {
		FollowButton.setAttribute("hidden", "");
		BlockButton.setAttribute("hidden", "");
		AlternateAccountImage.removeAttribute("hidden");
		// This is meant for the regular account. A big ol' you :3
		let Token = localStorage.getItem(Variables.MastodonAccessToken);
		if (Token != null) {
			let MastoProfile = await MastodonAPI.GetOwnAccount();
			AccountName.innerHTML = MastoProfile.username;
			AccountDescription.innerHTML = MastoProfile.note;
			AccountImage.setAttribute("src", MastoProfile.avatar);
			// Build the fields
			let Field = "<div style=\"display: flex; justify-content: center;\">";
			for (let i of MastoProfile.fields) {
				Field += "<b class=\"Entry\">" + i.name + "</b><em class=\"Exitry\">" + i.value + "</em>";
			}
			Field += "</div>";
			AccountDescription.innerHTML += Field;
		}
		Token = localStorage.getItem(Variables.BlueskyAccessToken);
		if (Token != null) {
			// Mastodon stuff.
			let MastoProfile = await MastodonAPI.GetOwnAccount();
			// Get my followings on Mastodon.
			let MyMastodonFollows = await MastodonAPI.GetAccountFollows(MastoProfile.id);
			document.getElementsByClassName("Posts")[0].innerHTML += "FOLLOWS: <br/><br/>";
			for (let i of MyMastodonFollows) {
				document.getElementsByClassName("Posts")[0].innerHTML += i.acct + "<br/><br/>";
			}
			// Bluesky stuff.
			let BlueProfile = await BlueskyAPI.GetProfile(localStorage.getItem(Variables.BlueskyDID));
			AlternateAccountImage.setAttribute("width", "20%");
			AlternateAccountImage.setAttribute("height", "20%");
			AlternateAccountName.innerHTML = BlueProfile.handle;
			AlternateAccountDescription.innerHTML = BlueProfile.description;
			AlternateAccountImage.setAttribute("src", BlueProfile.avatar);
			// Get my followings on Bsky
			let MyBlueskyFollows = await BlueskyAPI.GetFollows(localStorage.getItem(Variables.BlueskyDID));
			AccountPosts.innerHTML += "FOLLOWS: <br/><br/>";
			for (let i of MyBlueskyFollows.follows) {
				AccountPosts.innerHTML += i.handle + "<br/><br/>";
			}
		}
	}
	
}
