<template>
	<MkStickyContainer>
		<template #header>
			<MkPageHeader :actions="[]" :tabs="[]" :display-back-button="true" />
		</template>
		<MkSpacer :content-max="1100">
			<div class="luaHome">
				<header class="serviceHeader">
					<div class="serviceTitle">
						<i class="ph-code ph-bold ph-lg"></i>
						<div>
							<h1>Lua4Frozen</h1>
							<p>#Lua4Frozen posts with attached .lua files</p>
						</div>
					</div>
					<div class="headerLinks">
						<button class="specLink _button" @click="composeLua4FrozenPost">
							<i class="ph-upload-simple ph-bold"></i>
							<span>Post Lua</span>
						</button>
						<MkA to="/lua4frozen/test" class="specLink">
							<i class="ph-play ph-bold"></i>
							<span>Test Runner</span>
						</MkA>
						<MkA to="/lua4frozen/spec" class="specLink">
							<i class="ph-book-open ph-bold"></i>
							<span>Specification</span>
						</MkA>
					</div>
				</header>
				<section class="panel">
					<form class="searchForm" @submit.prevent="load">
						<MkInput v-model="query">
							<template #prefix><i class="ph-magnifying-glass ph-bold"></i></template>
							<template #label>Search</template>
						</MkInput>
						<MkButton primary type="submit">Search</MkButton>
					</form>
				</section>
				<section class="panel">
					<div v-if="loading" class="loading"><MkLoading /></div>
					<div v-else-if="items.length === 0" class="_fullinfo">
						<div>No Lua4Frozen posts</div>
					</div>
					<div v-else class="grid">
						<MkA v-for="item in items" :key="item.id" class="card" :to="`/lua4frozen/${item.id}`">
							<div class="thumb">
								<i class="ph-code ph-bold ph-lg"></i>
							</div>
							<div class="body">
								<div class="title">{{ getTitle(item) }}</div>
								<div v-if="getDescription(item)" class="description">
									{{ getDescription(item) }}
								</div>
								<div class="meta">
									<span>@{{ item.user.username }}</span>
									<span>{{ getLuaFiles(item).length }} Lua</span>
									<span v-if="item.viewCount != null">
										<i class="ph-eye ph-bold"></i>
										{{ item.viewCount }}
									</span>
								</div>
							</div>
						</MkA>
					</div>
				</section>
			</div>
		</MkSpacer>
	</MkStickyContainer>
</template>

<script lang="ts" setup>
import { computed, ref, watch } from "vue";
import * as misskey from "iceshrimp-sdk";
import MkButton from "@/components/MkButton.vue";
import MkInput from "@/components/form/input.vue";
import * as os from "@/os";
import { definePageMetadata } from "@/scripts/page-metadata";
import { selectFiles } from "@/scripts/select-file";

const query = ref("");
const loading = ref(false);
const items = ref<misskey.entities.Note[]>([]);

async function composeLua4FrozenPost(ev: MouseEvent) {
	const files = await selectFiles(ev.currentTarget ?? ev.target, "Lua4Frozen .lua files");
	const hasLua = files.some((file) => file.name.toLowerCase().endsWith(".lua"));
	if (!hasLua) {
		await os.alert({
			type: "warning",
			title: "Missing Lua file",
			text: "Attach at least one .lua file for Lua4Frozen.",
		});
	}
	os.post({
		initialText: "#Lua4Frozen",
		initialFiles: files,
		instant: true,
	});
}

function splitText(note: misskey.entities.Note): string[] {
	return (note.text ?? "").replace(/\r\n/g, "\n").split("\n");
}

function getTitle(note: misskey.entities.Note): string {
	return splitText(note)[0]?.trim() || "(no title)";
}

function getDescription(note: misskey.entities.Note): string {
	return splitText(note).slice(1).join("\n").trim();
}

function getLuaFiles(note: misskey.entities.Note): misskey.entities.DriveFile[] {
	return note.files.filter((file) => file.name.toLowerCase().endsWith(".lua"));
}

async function load() {
	loading.value = true;
	try {
		items.value = (await (os.api as any)("notes/lua4frozen-search", {
			query: query.value,
			limit: 48,
		})) as misskey.entities.Note[];
	} finally {
		loading.value = false;
	}
}

watch(query, () => {}, { immediate: true });
load();

definePageMetadata(
	computed(() => ({
		title: "Lua4Frozen",
		icon: "ph-code ph-bold ph-lg",
	})),
);
</script>

<style lang="scss" scoped>
.luaHome {
	display: grid;
	gap: 20px;
}

.serviceHeader,
.panel {
	padding: 16px;
	border-radius: 8px;
	background: var(--panel);
	border: solid 1px var(--divider);
}

.serviceHeader {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 12px;
	flex-wrap: wrap;
}

.serviceTitle {
	display: flex;
	align-items: center;
	gap: 14px;
}

.specLink {
	display: inline-flex;
	align-items: center;
	gap: 8px;
	padding: 8px 12px;
	border-radius: 8px;
	color: var(--accent);
	background: var(--accentedBg);
}

.headerLinks {
	display: flex;
	align-items: center;
	gap: 8px;
	flex-wrap: wrap;
}

.serviceTitle > i,
.thumb {
	display: grid;
	place-items: center;
	color: var(--accent);
	background: var(--accentedBg);
}

.serviceTitle > i {
	width: 48px;
	height: 48px;
	border-radius: 8px;
}

h1 {
	margin: 0;
	font-size: 1.35rem;
}

p {
	margin: 4px 0 0;
	color: var(--fgTransparentWeak);
}

.searchForm {
	display: grid;
	grid-template-columns: 1fr auto;
	gap: 12px;
	align-items: end;
}

.loading {
	padding: 24px;
}

.grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
	gap: 12px;
}

.card {
	overflow: hidden;
	border-radius: 8px;
	background: var(--bg);
	border: solid 1px var(--divider);
	color: var(--fg);
}

.thumb {
	aspect-ratio: 16 / 9;
}

.body {
	padding: 12px;
}

.title {
	font-weight: 700;
	overflow: hidden;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
}

.description {
	margin-top: 4px;
	color: var(--fgTransparentWeak);
	overflow: hidden;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
}

.meta {
	display: flex;
	flex-wrap: wrap;
	gap: 10px;
	margin-top: 8px;
	color: var(--fgTransparentWeak);
	font-size: 0.88em;
}

@media (max-width: 600px) {
	.searchForm {
		grid-template-columns: 1fr;
	}
}
</style>
