<script lang="ts">
import type { FSNavigator } from "./FSNavigator";
import { fs_check_is_error, fs_node_icon, fs_share_hotlink_url, fs_share_url, fs_update, type FSNode, type FSPermissions } from "lib/FilesystemAPI.svelte";
import { copy_text } from "util/Util";
import CopyButton from "layout/CopyButton.svelte";
import Dialog from "layout/Dialog.svelte";
import { fade } from "svelte/transition";
import { get_user } from "lib/NovaAPI";

let { nav }: {
	nav: FSNavigator;
} = $props();

let path: FSNode[]
let base: FSNode = $state()
let toast = $state("")
let share_url = $state("")
let direct_share_url = $state("")
let is_parent = $state(false)
let parent_node: FSNode = $state()

let dialog: Dialog = $state()
let dialog_not_allowed: Dialog = $state()
export const open = async (e: MouseEvent, p: FSNode[]) => {
	path = p
	base = path[path.length-1]

	const user = await get_user()
	if (user.subscription.file_sharing === false && base.created_by === user.id) {

	}

	share_url = fs_share_url(path)
	if (share_url === "") {
		// File is not public, make public
		try {
			await make_public()
		} catch (err) {
			if (fs_check_is_error(err, "sharing_not_allowed")) {
				if (e.target instanceof HTMLButtonElement) {
					dialog_not_allowed.open(e.target.getBoundingClientRect())
				} else {
					dialog_not_allowed.open((e.target as HTMLElement).parentElement.getBoundingClientRect())
				}
				return
			} else {
				alert("Error file making file public: "+err)
			}
		}
	}

	await share()

	if (e.target instanceof HTMLButtonElement) {
		dialog.open(e.target.getBoundingClientRect())
	} else {
		dialog.open((e.target as HTMLElement).parentElement.getBoundingClientRect())
	}
}

const make_public = async () => {
	if (!base.is_shared()) {
		base = await fs_update(
			base.path,
			{link_permissions: {read: true} as FSPermissions},
		)
		await nav.reload()

		// Insert the new FSNode into the path
		path[path.length-1] = base
	}
}

const share = async () => {
	// If the base node does not have a public ID then the file is shared
	// through a parent node. In that case we ask the user if it was their
	// intention to share the parent directory
	is_parent = base.id === undefined
	if (is_parent) {
		// Walk path backwards looking for the last public ID
		for (let i = path.length - 1; i >= 0; i--) {
			if (path[i].id !== undefined && path[i].id !== "me") {
				parent_node = path[i]
				break
			}
		}
	}

	share_url = fs_share_url(path)
	direct_share_url = fs_share_hotlink_url(path)

	try {
		await navigator.share({
			title: base.name,
			text: "I would like to share '" + base.name + "' with you",
			url: share_url,
		})
	} catch(_) {
		if (copy_text(share_url)) {
			toast = "Link copied to clipboard"
			setTimeout(() => {toast = ""}, 10000)
		} else {
			alert("Could not copy text")
		}
	}
}
</script>


<Dialog bind:this={dialog_not_allowed}>
	<div class="dialog_inner">
		<div class="highlight_yellow" transition:fade>
			Sharing is not allowed on free accounts. Upgrade to premium to
			enable this feature
		</div>
	</div>
</Dialog>

<Dialog bind:this={dialog}>
	<div class="dialog_inner">
		{#if toast !== ""}
			<div class="highlight_green" transition:fade>{toast}</div>
			<div class="separator" transition:fade></div>
		{/if}

		<div>Sharing link</div>
		<div class="link_copy">
			<div class="button_container">
				<CopyButton text={share_url}>Copy</CopyButton>
			</div>
			<a href="{share_url}">{share_url}</a>
		</div>
		<div class="separator"></div>
		<div>Direct sharing link (hotlink)</div>
		<div class="link_copy">
			<div class="button_container">
				<CopyButton text={direct_share_url}>Copy</CopyButton>
			</div>
			<a href="{direct_share_url}">{direct_share_url}</a>
		</div>

		{#if is_parent}
			<div class="separator"></div>
			<div>
				This link also gives access to
				<img src={fs_node_icon(parent_node, 64, 64)} class="node_icon" alt="icon"/>
				{parent_node.name}
				<br/>
				<button onclick={async e => {await make_public(); await share()}} style="display: inline;">
					Only share
					<img src={fs_node_icon(base, 64, 64)} class="node_icon" alt="icon"/>
					{base.name}
				</button>
			</div>
		{/if}
	</div>
</Dialog>

<style>
.dialog_inner {
	display: flex;
	flex-direction: column;
	text-align: center;
	max-width: 40em;
}
.link_copy {
	display: flex;
	flex-direction: row;
	align-items: center;
	gap: 0.5em;
}
.link_copy > .button_container {
	flex: 0 0 auto;
}
.link_copy > a {
	flex: 1 1 auto;
	text-overflow: ellipsis;
	white-space: nowrap;
	overflow-x: hidden;
}
.node_icon {
	width: 1.5em;
	height: 1.5em;
	vertical-align: middle;
}
.separator {
	height: 1px;
	border: none;
	background-color: var(--separator);
	margin-top: 0.5em;
	margin-bottom: 0.5em;
}
</style>
