<script lang="ts" module>
export type ZipEntry = {
	size: number,
	children?: {[index: string]: ZipEntry},
	properties?: string[],
	download_url?: string, // Added by us
	details_open?: boolean, // Added by us
};
</script>
<script lang="ts">
import { formatDataVolume, formatDate } from "util/Formatting"
import ZipItem from "filesystem/viewers/ZipItem.svelte";
import IconBlock from "layout/IconBlock.svelte";
import TextBlock from "layout/TextBlock.svelte"
import { fs_node_icon, fs_path_url, FSNode } from "lib/FilesystemAPI.svelte";
import { loading_finish, loading_start } from "lib/Loading";

let {
	node,
	children,
}: {
	node: FSNode;
	children?: import('svelte').Snippet;
} = $props();

let status = $state("loading")

let zip: ZipEntry = $state({size: 0} as ZipEntry)
let uncomp_size = $state(0)
let comp_ratio = $state(0)
let archive_type = $state("")
let truncated = $state(false)

export const update = async () => {
	if (node.file_type === "application/x-7z-compressed") {
		archive_type = "7z"
	} else {
		archive_type = ""
	}

	try {
		loading_start()
		status = "loading"
		let resp = await fetch(fs_path_url(node.path)+"?zip_info")

		if (resp.status >= 400) {
			status = "parse_failed"
			return
		}

		zip = await resp.json() as ZipEntry

		// Check if the zip has the property which allows separate files to be
		// downloaded. If so then we set the download URL for each file
		if (zip.properties !== undefined) {
			if (zip.properties.includes("read_individual_files")) {
				// Set the download URL for each file in the zip
				recursive_set_url(fs_path_url(node.path)+"?zip_file=", zip)
			}
			truncated = zip.properties.includes("truncated")
		}

		uncomp_size = recursive_size(zip)
		comp_ratio = (uncomp_size / node.file_size)
		status = "finished"
	} catch (err) {
		console.error(err)
		status = "parse_failed"
	} finally {
		loading_finish()
	}
}

const recursive_set_url = (parent_path: string, file: ZipEntry) => {
	file.download_url = parent_path

	if (file.children !== undefined) {
		for (const [name, child] of Object.entries(file.children)) {
			recursive_set_url(file.download_url + "/" + name, child)
		}
	}
}

const recursive_size = (file: ZipEntry) => {
	let size = file.size

	if (file.children !== undefined) {
		for (const v of Object.values(file.children)) {
			size += recursive_size(v)
		}
	}

	// Return the total size of this file and all its children
	return size
}
</script>

{@render children?.()}

<IconBlock icon_href={fs_node_icon(node, 256, 256)}>
	{#if archive_type === "7z"}
		This is a 7-zip archive. You will need
		<a href="https://www.7-zip.org/">7-zip</a> or compatible software to
		extract it<br/>
	{/if}

	{node.name}<br/>
	<table>
		<tbody>
			<tr>
				<td>Compressed size</td>
				<td>{formatDataVolume(node.file_size, 3)}</td>
			</tr>
			{#if !truncated}
				<tr>
					<td>Uncompressed size</td>
					<td>{formatDataVolume(uncomp_size, 3)} (Ratio: {comp_ratio.toFixed(2)}x)</td>
				</tr>
			{/if}
			<tr>
				<td>Upload date</td>
				<td>{formatDate(node.created, true, true, true)}</td>
			</tr>
		</tbody>
	</table>
	<hr/>
	<button class="button_highlight" onclick={() => node.download()}>
		<i class="icon">download</i>
		<span>Download</span>
	</button>
</IconBlock>

{#if status === "finished"}
	<TextBlock>
		<h2>Files in this zip archive</h2>
		{#if truncated}
			<div class="highlight_yellow">
				Due to the large size of this archive, the results have been
				truncated. The list below is incomplete!
			</div>
		{/if}

		<ZipItem item={zip} />
	</TextBlock>
{:else if status === "parse_failed"}
	<TextBlock>
		<p>
			Zip archive could not be parsed. This usually means that the archive
			is encrypted or that it uses an unsupported compression format.
		</p>
	</TextBlock>
{/if}

<style>
table {
	width: auto;
}
td {
	padding: 0;
	padding-right: 1em;
}
</style>
