<script lang="ts">
import { formatDataVolume } from "util/Formatting";
import { fs_encode_path, fs_node_icon } from "lib/FilesystemAPI.svelte"
import type { FSNavigator } from "filesystem/FSNavigator";
import SortButton from "layout/SortButton.svelte";
import { FileAction, type FileActionHandler } from "./FileManagerLib";

let {
	nav,
	file_event,
	show_hidden = false,
	large_icons = false,
	hide_edit = false,
	hide_branding = false
}: {
	nav: FSNavigator
	file_event: FileActionHandler
	show_hidden?: boolean
	large_icons?: boolean
	hide_edit?: boolean
	hide_branding?: boolean
} = $props();
</script>

<table class="directory">
	<thead>
		<tr>
			<td></td>
			<td>
				<SortButton active_field={$nav.sort_last_field} asc={$nav.sort_asc} sort_func={nav.sort_children} field="name">
					Name
				</SortButton>
			</td>
			<td class="hide_small">
				<SortButton active_field={$nav.sort_last_field} asc={$nav.sort_asc} sort_func={nav.sort_children} field="file_size">
					Size
				</SortButton>
			</td>
			<td></td>
		</tr>
	</thead>
	<tbody>
		{#each $nav.children as child, index (child.path)}
			<tr
				onclick={e => file_event(FileAction.Click, index, e)}
				oncontextmenu={e => file_event(FileAction.Context, index, e)}
				class="node"
				class:node_selected={child.fm_selected}
				class:hidden={child.name.startsWith(".") && !show_hidden}
			>
				<td>
					<img src={fs_node_icon(child, 64, 64)} class="node_icon" class:large_icons alt="icon"/>
				</td>
				<td class="node_name">
					<a class="title_link" href={"/d"+fs_encode_path(child.path)}>
						{child.name}
					</a>
				</td>
				<td class="node_size hide_small">
					{#if child.type === "file"}
						{formatDataVolume(child.file_size, 3)}
					{/if}
				</td>
				<td class="node_icons">
					<div class="icons_wrap">
						{#if child.abuse_type !== undefined}
							<i class="icon" title="This file / directory has received an abuse report. It cannot be shared">block</i>
						{:else if child.is_shared()}
							<a
								href="/d/{child.id}"
								onclick={e => file_event(FileAction.Share, index, e)}
								class="button action_button"
							>
								<i class="icon" title="This file / directory is shared. Click to open public link">share</i>
							</a>
						{/if}
						{#if child.properties !== undefined && child.properties.branding_enabled === "true" && !hide_branding}
							<button class="action_button" onclick={e => file_event(FileAction.Branding, index, e)}>
								<i class="icon">palette</i>
							</button>
						{/if}
						{#if !hide_edit}
							<button class="action_button" onclick={e => file_event(FileAction.Menu, index, e)}>
								<i class="icon">menu</i>
							</button>
						{/if}
					</div>
				</td>
			</tr>
		{/each}
	</tbody>
</table>

<style>
.directory {
	background: var(--body_background);
	border-collapse: collapse;
	/* backdrop-filter: blur(4px); */
	max-width: 1200px;
	margin: auto; /* center */
}
td {
	padding: 0;
}
.node {
	text-decoration: none;
	color: var(--body_text-color);
	cursor: pointer;
}
.node:not(:last-child) {
	border-bottom: 1px solid var(--separator);
}
.node:hover:not(.node_selected) {
	background: var(--input_hover_background);
	color: var(--input_text);
	text-decoration: none;
}
.node.node_selected {
	background-color: var(--highlight_color) !important;
	color: var(--highlight_text_color);
}
td {
	vertical-align: middle;
}
.node_icon {
	height: 32px;
	width: 32px;
	vertical-align: middle;
	/* border-radius: 4px; */
	margin: 2px;
}
.node_name {
	width: 100%;
	line-height: 1.2em;
	word-break: break-all;
}
.title_link {
	text-decoration: none;
	color: var(--body_text_color);
}
.node_size {
	min-width: 5em;
	white-space: nowrap;
}
.icons_wrap {
	display: flex;
	flex-direction: row;
	justify-content: end;
}
.hidden {
	display: none;
}

/* Large icon mode only supported on wide screens */
@media (min-width: 500px) {
	.node_icon.large_icons {
		height: 64px;
		width: 64px;
	}
}
@media (max-width: 500px) {
	.hide_small {
		display: none;
	}
}
</style>
