package main

import (
	"strings"
	"html/template"
)

type ErrorPageParams struct {
	HeadParams
	Errors  string
	Message string

	// for when we can't find an event, but we still want to give the user a chance to open it elsewhere
	Clients []ClientReference
}

func (e *ErrorPageParams) MessageHTML() template.HTML {
	if e.Message != "" {
		return template.HTML(e.Message)
	}

	switch {
	case strings.Contains(e.Errors, "invalid checksum"):
		return "It looks like you entered an invalid event code.<br> Check if you copied it fully, a good idea is compare the first and the last characters."
	case strings.Contains(e.Errors, "couldn't find this"):
		return "Can't find the event in the relays. Try getting an `nevent1` code with relay hints."
	case strings.Contains(e.Errors, "invalid bech32 string length"),
		strings.Contains(e.Errors, "invalid separator"),
		strings.Contains(e.Errors, "not part of charset"):
		return "You have typed a wrong event code, we need a URL path that starts with /npub1, /nprofile1, /nevent1, /naddr1, or something like /name@domain.com (or maybe just /domain.com) or an event id as hex (like /aef8b32af...)"
	case strings.Contains(e.Errors, "profile metadata not found"):
		return "We couldn't find the metadata (name, picture etc) for the specified user. Please check back here in 6 hours."
	default:
		return "I can't give any suggestions to solve the problem.<br> Please tag <a href='/dtonon.com'>daniele</a> and <a href='/fiatjaf.com'>fiatjaf</a> and complain!"
	}
}

templ errorTemplate(params ErrorPageParams) {
	<!DOCTYPE html>
	<html class="theme--default font-light print:text-base">
		<meta charset="UTF-8"/>
		<head>
			<title>Error</title>
			@headCommonTemplate(params.HeadParams)
		</head>
		<body
			class="mb-16 bg-white text-gray-600 dark:bg-neutral-900 dark:text-neutral-50 print:text-black"
		>
			@topTemplate(params.HeadParams)
			<div class="mx-auto mt-12 w-10/12 text-center lg:w-9/12">
				<div class="mx-auto w-4/5 sm:w-3/5">
					<div class="mt-4 text-2xl leading-6">
						@templ.Raw(params.MessageHTML())
					</div>
					<div class="my-8 italic text-neutral-400 dark:text-neutral-500">
						{ params.Errors }
					</div>
					<div>
						Are you lost?
						<a
							href="/"
							class="block leading-3 underline decoration-neutral-400 underline-offset-4"
						>Go to the homepage</a>
					</div>
					if params.Clients != nil {
						<div class="mt-12">
							@clientsTemplate(params.Clients)
						</div>
					}
				</div>
			</div>
			@footerTemplate()
		</body>
	</html>
}
