package main

import (
	_ "embed"
	"strings"
	"html/template"
	"github.com/nbd-wtf/go-nostr/nip19"
	"github.com/nbd-wtf/go-nostr/nip52"
)

type CalendarPageParams struct {
	BaseEventPageParams
	OpenGraphParams
	HeadParams
	Details       DetailsParams
	TimeZone      string
	StartAtDate   string
	StartAtTime   string
	EndAtDate     string
	EndAtTime     string
	Content       template.HTML
	CalendarEvent Kind31922Or31923Metadata
	Clients       []ClientReference
}

func formatParticipants(participants []nip52.Participant) string {
	var list = make([]string, 0)
	for _, p := range participants {
		nreplace, _ := nip19.EncodePublicKey(p.PubKey)
		nreplace = replaceNostrURLsWithHTMLTags(nostrNpubNprofileMatcher, "nostr:"+nreplace)
		if p.Role != "" {
			nreplace = nreplace + " as " + strings.ToTitle(p.Role)
		}
		list = append(list, nreplace)
	}
	return strings.Join(list, ", ")
}

templ calendarEventInnerBlock(params CalendarPageParams) {
	<h1 class="text-2xl">
		{ params.CalendarEvent.Title }
	</h1>
	<div class="flex flex-col gap-4 sm:flex-row sm:flex-wrap xl:flex-nowrap">
		if params.StartAtDate == params.EndAtDate {
			<div class="sm:w-auto sm:grow xl:grow-0 xl:w-1/3">
				<div class="font-semibold text-sm ml-2">Date</div>
				<div class="py-2 px-4 bg-strongpink text-white rounded-md">
					<div>{ params.StartAtDate }</div>
					if params.StartAtTime != "" && params.EndAtTime != "" {
						<div class="text-sm whitespace-nowrap">From { params.StartAtTime } to { params.EndAtTime } ({ params.TimeZone })</div>
					}
				</div>
			</div>
		} else {
			<div class="sm:w-auto sm:grow xl:grow-0 xl:w-1/3">
				<div class="font-semibold text-sm ml-2">Start date</div>
				<div class="py-2 px-4 bg-strongpink text-white rounded-md">
					<div class="whitespace-nowrap">{ params.StartAtDate }</div>
					<div class="text-sm">{ params.StartAtTime } ({ params.TimeZone })</div>
				</div>
			</div>
			if params.EndAtTime != "" {
				<div class="sm:w-auto sm:grow xl:grow-0 xl:w-1/3">
					<div class="font-semibold text-sm ml-2">End date</div>
					<div class="py-2 px-4 bg-strongpink text-white rounded-md">
						<div class="whitespace-nowrap">{ params.EndAtDate }</div>
						<div class="text-sm">{ params.EndAtTime } ({ params.TimeZone })</div>
					</div>
				</div>
			}
		}
		if params.CalendarEvent.Locations[0] != "" {
			<div class="w-full">
				<div class="font-semibold text-sm ml-2">Location</div>
				<div class="py-2 px-4 bg-neutral-200 dark:bg-neutral-800 rounded-md">
					{ params.CalendarEvent.Locations[0] }
				</div>
			</div>
		}
	</div>
	<div class="mb-4 pt-6">
		if len(params.CalendarEvent.Participants) != 0 {
			<div class="pb-4">
				<span class="font-medium">People</span>:
				@templ.Raw(formatParticipants(params.CalendarEvent.Participants))
			</div>
		}
		if params.CalendarEvent.Image != "" {
			<img class="w-full mt-2" src={ params.CalendarEvent.Image }/>
		}
		@templ.Raw(params.Content)
	</div>
	<div class="mb-4">
		for _, v := range params.CalendarEvent.Hashtags {
			<span class="mr-2 whitespace-nowrap rounded bg-neutral-200 px-2 dark:bg-neutral-700 dark:text-white">
				{ v }
			</span>
		}
	</div>
}

templ calendarEventTemplate(params CalendarPageParams, isEmbed bool) {
	<!DOCTYPE html>
	if isEmbed {
		@embeddedPageTemplate(
			params.Event,
			params.NeventNaked,
		) {
			@calendarEventInnerBlock(params)
		}
	} else {
		@eventPageTemplate(
			"Calendar Event: "+params.CalendarEvent.Title,
			params.OpenGraphParams,
			params.HeadParams,
			params.Clients,
			params.Details,
			params.Event,
		) {
			@calendarEventInnerBlock(params)
		}
	}
}
