package main

import (
	"fmt"
	"html/template"
	"github.com/nbd-wtf/go-nostr/sdk"
	"github.com/nbd-wtf/go-nostr/nip05"
)

type ProfilePageParams struct {
	HeadParams

	Details                    DetailsParams
	AuthorRelays               []string
	Content                    string
	CreatedAt                  string
	Domain                     string
	LastNotes                  []EnhancedEvent
	Metadata                   sdk.ProfileMetadata
	NormalizedAuthorWebsiteURL string
	RenderedAuthorAboutText    template.HTML
	Nevent                     string
	Nprofile                   string
	Proxy                      string
	Title                      string
	Clients                    []ClientReference
}

templ profileTemplate(params ProfilePageParams) {
	<!DOCTYPE html>
	<html class="theme--default font-light print:text-base">
		<meta charset="UTF-8"/>
		<head>
			if params.Metadata.Name != "" && params.Metadata.DisplayName != "" {
				<title>
					{ params.Metadata.Name }
					if params.Metadata.Name != "" && params.Metadata.DisplayName != "" {
						/
					}
					{ params.Metadata.DisplayName } is on Nostr
				</title>
			}
			<meta
				name="description"
				content={ fmt.Sprintf("%s is %s's public key on Nostr", params.Metadata.Npub(), params.Metadata.ShortName()) }
			/>
			<meta property="og:title" content={ params.Title }/>
			<meta property="og:site_name" content={ params.Metadata.Npub() }/>
			if params.Metadata.Picture != "" {
				<meta property="og:image" content={ params.Metadata.Picture }/>
				<meta property="twitter:image" content={ params.Proxy + params.Metadata.Picture }/>
			}
			if params.Metadata.About != "" {
				<meta property="og:description" content={ params.Metadata.About }/>
			}
			<meta name="twitter:card" content="summary"/>
			<link rel="canonical" href={ "https://njump.me/" + params.Metadata.Npub() }/>
			<link
				rel="sitemap"
				type="application/xml"
				title={ "Sitemap for " + params.Metadata.Npub() }
				href={ "/" + params.Metadata.Npub() + ".xml" }
			/>
			<link
				rel="alternate"
				type="application/atom+xml"
				title="RSS"
				href={ "/" + params.Metadata.Npub() + ".rss" }
			/>
			@headCommonTemplate(params.HeadParams)
		</head>
		<body class="mb-16 bg-white text-gray-600 print:text-black dark:bg-neutral-900 dark:text-neutral-50">
			@topTemplate(params.HeadParams)
			<div
				itemscope
				itemtype="https://schema.org/Person"
				class="mx-auto px-4 sm:flex sm:items-center sm:justify-center sm:px-0"
			>
				<div class="w-full max-w-screen-2xl justify-between gap-10 overflow-visible print:w-full sm:flex sm:w-11/12 sm:px-4 md:w-10/12 lg:w-9/12 lg:gap-48vw">
					<header
						class="relative top-auto flex basis-1/5 sm:max-w-[20%] items-center self-start sm:sticky sm:top-8 sm:mt-8 sm:block sm:items-start"
					>
						<div
							class="hidden basis-[60%] pr-4 sm:pr-0 items-center overflow-hidden text-left break-words"
							_="on load or scroll from window or resize from window get #profile_name then measure its top, height then if top is less than height / -2 or height is 0 remove .hidden otherwise add .hidden"
						>
							<div class="mb-3 sm:text-center">
								<div class="text-2xl break-words" itemprop="name">{ params.Metadata.Name }</div>
								if params.Metadata.Name != params.Metadata.DisplayName {
									<div class="text-base text-stone-400 break-words" itemprop="alternateName">
										{ params.Metadata.DisplayName }
									</div>
								}
							</div>
						</div>
						<div
							class="imgclip max-w-[40%] basis-2/5 overflow-hidden sm:max-w-full sm:basis-auto"
						>
							<img class="block h-auto w-full" src={ params.Metadata.Picture } itemprop="image"/>
						</div>
					</header>
					<div class="w-full flex-1 break-words print:w-full sm:w-1/2">
						<header class="mb-6 hidden leading-5 sm:flex sm:items-center">
							<h1 class="sm:max-w-full">
								<div id="profile_name" class="text-2xl break-words">
									if params.Metadata.Event == nil {
										<span class="text-stone-200 font-bold">&lt;unnamed&gt;</span>
									} else {
										{ params.Metadata.Name }
									}
								</div>
								if params.Metadata.Name != params.Metadata.DisplayName {
									<div class="text-xl text-stone-400 break-words">
										{ params.Metadata.DisplayName }
									</div>
								}
							</h1>
						</header>
						<div class="-ml-4 mb-6 h-1.5 w-1/2 bg-zinc-100 sm:-ml-2.5 dark:bg-zinc-700"></div>
						if params.Metadata.Website != "" {
							<div class="mb-6 leading-5">
								<a
									itemprop="sameAs"
									class="border-b-2 border-b-gray-300 pb-0.5 hover:text-strongpink"
									href={ templ.URL(params.NormalizedAuthorWebsiteURL) }
								>{ params.Metadata.Website }</a>
							</div>
						}
						if params.RenderedAuthorAboutText != "" {
							<div
								class="prose mb-6 leading-5 dark:prose-invert prose-headings:font-light sm:prose-a:text-justify"
								dir="auto"
								itemprop="description"
							>
								@templ.Raw(params.RenderedAuthorAboutText)
							</div>
						}
						if params.Metadata.Website != "" || params.RenderedAuthorAboutText != "" {
							<div class="-ml-4 mb-6 h-1.5 w-1/3 bg-zinc-100 sm:-ml-2.5 dark:bg-zinc-700"></div>
						}
						<div class="mb-6 leading-5">
							<div class="text-sm text-strongpink">Public Key</div>
							<span itemprop="identifier">{ params.Metadata.Npub() }</span>
						</div>
						if params.Metadata.NIP05 != "" {
							<div class={ "mb-6", "leading-5", templ.KV("line-through", !params.Metadata.NIP05Valid(ctx)) }>
								<div class="text-sm text-strongpink">NIP-05 Address</div>
								<a href={ templ.URL(nip05.IdentifierToURL(params.Metadata.NIP05)) } class="underline-offset-[6px] hover:underline">
									<span itemprop="alternateName">{ nip05.NormalizeIdentifier(params.Metadata.NIP05) }</span>
								</a>
							</div>
						}
						<div class="mb-6 leading-5">
							<div class="text-sm text-strongpink">Profile Code</div>
							{ params.Nprofile }
						</div>
						if len(params.AuthorRelays) != 0 {
							<div class="mb-6 leading-5">
								<div class="text-sm text-strongpink">Publishing to</div>
								for _, relay := range params.AuthorRelays {
									<div
										itemprop="affiliation"
										itemscope
										itemtype="https://schema.org/Organization"
									>
										<span class="hidden" itemprop="name identifier">{ relay }</span>
										<a
											itemprop="url"
											href={ templ.URL("/r/" + relay) }
											class="mr-1 mt-2 inline-block max-w-full rounded-lg border border-slate-300 px-2 py-0.5 hover:border hover:border-solid hover:border-strongpink hover:bg-strongpink hover:text-white"
										>
											{ relay }
										</a>
									</div>
								}
							</div>
						}
						if params.Metadata.Event != nil {
							@detailsTemplate(params.Details)
						}
						if len(params.LastNotes) != 0 {
							<aside>
								<div class="-ml-4 mb-6 h-1.5 w-1/3 bg-zinc-100 sm:-ml-2.5 dark:bg-zinc-700"></div>
								<nav class="mb-6 leading-5">
									<h2 class="text-2xl text-strongpink">Last Notes</h2>
									for _, ee := range params.LastNotes {
										<div
											itemscope
											itemtype="https://schema.org/Article"
											class="my-8 block no-underline hover:-ml-6 hover:border-l-05rem hover:border-solid hover:border-l-gray-100 hover:pl-4 dark:hover:border-l-zinc-700"
										>
											<div class="hidden" itemprop="author" itemscope itemtype="https://schema.org/Person">
												<a href={ templ.SafeURL("/" + params.Metadata.Npub()) } itemprop="url"></a>
												<span itemprop="identifier">{ params.Metadata.Npub() }</span>
												<span itemprop="name">{ params.Metadata.ShortName() }</span>
											</div>
											<div class="-ml-2.5 mb-1.5 flex flex-row flex-wrap border-b-4 border-solid border-b-gray-100 pb-1 pl-2.5 dark:border-b-neutral-800">
												<a
													itemprop="url"
													href={ templ.URL("/" + ee.Nevent()) }
												>
													<span
														itemprop="dateCreated"
														datetime={ ee.CreatedAtStr() }
														class="text-sm text-strongpink"
													>
														{ ee.CreatedAtStr() }
													</span>
												</a>
												if ee.isReply() {
													<div class="ml-2 text-sm text-gray-300 dark:text-gray-400">- reply</div>
												}
											</div>
											<span
												class="mt-0.5 max-h-40 basis-full overflow-hidden hover:text-strongpink cursor-pointer"
												_="on load if my scrollHeight > my offsetHeight add .gradient end
												   on click halt the event then set the window's location to @loc"
												loc={ "/" + ee.Nevent() }
												dir="auto"
												itemprop="articleBody"
											>
												@templ.Raw(ee.Preview())
											</span>
										</div>
									}
								</nav>
							</aside>
						}
						<div class="-ml-4 mb-6 h-1.5 w-1/3 bg-zinc-100 sm:-ml-2.5 dark:bg-zinc-700"></div>
					</div>
					@clientsTemplate(params.Clients)
				</div>
			</div>
			@footerTemplate()
		</body>
	</html>
}
