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

// Buttons
let Favorite = document.getElementsByClassName("Favorite")[0];
let Boost = document.getElementsByClassName("Boost")[0];
let Reply = document.getElementsByClassName("Reply")[0];

let FavoriteFlipper = false;
let BoostFlipper = false;

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

document.getElementsByClassName("Origin")[0].innerHTML = website;
GetPost();

// Button stuff
Favorite.onclick = (event) => {
	if (website == "Mastodon") {
		MastodonAPI.SendFavorite(post.id, post.favourited);
	} else if (website == "Bluesky") {
		BlueskyAPI.SendLike(localStorage.getItem(Variables.BlueskyDID), post.post.uri, post.post.cid);
	}
	SetFavorite();
}

Boost.onclick = (event) => {
	if (website == "Mastodon") {
		MastodonAPI.SendReblog(post.id, post.reblogged);
	} else if (website == "Bluesky") {
		BlueskyAPI.SendRepost(localStorage.getItem(Variables.BlueskyDID), post.post.uri, post.post.cid);
	}
	SetBoost();
}

Reply.onclick = (event) => {
	window.location.href = "../../HTML/post.html?website=" + website;
}

// Functions and things.
async function GetPost() {
	if (website == "Mastodon") {
		// Check for a reblog.
		if (post.reblog != null) {
			document.getElementsByClassName("PostText")[0].innerHTML = post.reblog.content;
			document.getElementsByClassName("Handle")[0].innerHTML = post.reblog.account.username + " ( R: " + post.account.username + " )";
			if (post.reblog.media_attachments.length != 0) {
				for (let i of post.reblog.media_attachments) {
					CreateMedia(i);
				}
			}
		} else {
			document.getElementsByClassName("PostText")[0].innerHTML = post.content;
			document.getElementsByClassName("Handle")[0].innerHTML = post.account.username;
			// Show the image if it exists.
			if (post.media_attachments.length != 0) {
				for (let i of post.media_attachments) {
					CreateMedia(i);
				}
			}
		}
		// Update the post to see if there is new information.
		post = await MastodonAPI.GetStatus(post.id);
		// Set the texts. It's opposite because "setting" causes it to switch.
		FavoriteFlipper = !(post.favourite);
		BoostFlipper = !(post.reblogged);
		SetFavorite();
		SetBoost();
	} else if (website == "Bluesky") {
		// Check for a reblog.
		if (post.hasOwnProperty("reason") && post.reason.$type == "app.bsky.feed.defs#reasonRepost") {
			document.getElementsByClassName("PostText")[0].innerHTML = post.post.record.text;
			document.getElementsByClassName("Handle")[0].innerHTML = post.post.author.handle + " ( R: " + post.reason.by.handle + " )";
		} else {
			document.getElementsByClassName("PostText")[0].innerHTML = post.post.record.text;
			document.getElementsByClassName("Handle")[0].innerHTML = post.post.author.handle;
		}
		// Show the image if it exists.
		if (post.post.record.hasOwnProperty("embed") && post.post.record.embed.$type == "app.bsky.embed.images") {
			for (let i of post.post.embed.images) {
				document.getElementsByClassName("Images")[0].innerHTML = document.getElementsByClassName("Images")[0].innerHTML + "<img src=" + i.fullsize + " alt='" + i.alt + "'/>";
			}
		}
		// We don't need to update the post with new information. The repos are seperate.
		// Set the texts. It's opposite because "setting" causes it to switch.
		let GetRepo = await BlueskyAPI.GetRecord(localStorage.getItem(Variables.BlueskyDID), "app.bsky.feed.like", post.post.uri.split("/")[post.post.uri.split("/").length - 1]);
		if (GetRepo.hasOwnProperty("error") && GetRepo.error == "RecordNotFound") {
			FavoriteFlipper = true;
		}
		GetRepo = await BlueskyAPI.GetRecord(localStorage.getItem(Variables.BlueskyDID), "app.bsky.feed.repost", post.post.uri.split("/")[post.post.uri.split("/").length - 1]);
		if (GetRepo.hasOwnProperty("error") && GetRepo.error == "RecordNotFound") {
			BoostFlipper = true;
		}
		SetFavorite();
		SetBoost();
	} else {
		document.getElementsByClassName("PostText")[0].innerHTML = "Nothing to load.";
	}
}

function CreateMedia(Media) {
	// Check to see if the image is on the same server.
	if (Media.type == "image") {
		if (Media.remote_url == null) {
			document.getElementsByClassName("Images")[0].innerHTML += "<img src=" + Media.url + " alt='" + Media.description + "'/>";
		} else {
			document.getElementsByClassName("Images")[0].innerHTML += "<img src=" + Media.remote_url + " alt='" + Media.description + "'/>";
		}
	} else if (Media.type == "video") {
		if (Media.remote_url == null) {
			document.getElementsByClassName("Images")[0].innerHTML += "<video controls src=" + Media.url + " alt='" + Media.description + "'></video>";
		} else {
			document.getElementsByClassName("Images")[0].innerHTML += "<video controls src=" + Media.remote_url + " alt='" + Media.description + "'></video>";
		}
	}
}

function SetFavorite() {
	FavoriteFlipper = !(FavoriteFlipper);
	if (FavoriteFlipper == false) {
		Favorite.innerHTML = "Favorite!";
	} else {
		Favorite.innerHTML = "Unfavorite...";
	}
}

function SetBoost() {
	BoostFlipper = !(BoostFlipper);
	if (BoostFlipper == false) {
		Boost.innerHTML = "Boost!";
	} else {
		Boost.innerHTML = "Unboost...";
	}
}