<script lang="ts">
  import {onMount} from "svelte"
  import {derived} from "svelte/store"
  import {displayRelayUrl, getTagValue, EVENT_TIME, ZAP_GOAL, THREAD} from "@welshman/util"
  import {deriveRelay} from "@welshman/app"
  import {fly} from "@lib/transition"
  import AltArrowDown from "@assets/icons/alt-arrow-down.svg?dataurl"
  import RemoteControllerMinimalistic from "@assets/icons/remote-controller-minimalistic.svg?dataurl"
  import UserRounded from "@assets/icons/user-rounded.svg?dataurl"
  import LinkRound from "@assets/icons/link-round.svg?dataurl"
  import SquareTopDown from "@assets/icons/square-top-down.svg?dataurl"
  import Exit from "@assets/icons/logout-3.svg?dataurl"
  import Letter from "@assets/icons/letter.svg?dataurl"
  import Login from "@assets/icons/login-3.svg?dataurl"
  import History from "@assets/icons/history.svg?dataurl"
  import Tuning2 from "@assets/icons/tuning-2.svg?dataurl"
  import StarFallMinimalistic from "@assets/icons/star-fall-minimalistic-2.svg?dataurl"
  import NotesMinimalistic from "@assets/icons/notes-minimalistic.svg?dataurl"
  import CalendarMinimalistic from "@assets/icons/calendar-minimalistic.svg?dataurl"
  import AddCircle from "@assets/icons/add-circle.svg?dataurl"
  import ChatRound from "@assets/icons/chat-round.svg?dataurl"
  import Bell from "@assets/icons/bell.svg?dataurl"
  import Icon from "@lib/components/Icon.svelte"
  import Link from "@lib/components/Link.svelte"
  import Button from "@lib/components/Button.svelte"
  import Popover from "@lib/components/Popover.svelte"
  import SecondaryNavItem from "@lib/components/SecondaryNavItem.svelte"
  import SecondaryNavHeader from "@lib/components/SecondaryNavHeader.svelte"
  import SecondaryNavSection from "@lib/components/SecondaryNavSection.svelte"
  import SpaceDetail from "@app/components/SpaceDetail.svelte"
  import SpaceInvite from "@app/components/SpaceInvite.svelte"
  import SpaceExit from "@app/components/SpaceExit.svelte"
  import SpaceJoin from "@app/components/SpaceJoin.svelte"
  import RelayName from "@app/components/RelayName.svelte"
  import ProfileList from "@app/components/ProfileList.svelte"
  import AlertAdd from "@app/components/AlertAdd.svelte"
  import Alerts from "@app/components/Alerts.svelte"
  import RoomCreate from "@app/components/RoomCreate.svelte"
  import MenuSpaceRoomItem from "@app/components/MenuSpaceRoomItem.svelte"
  import SocketStatusIndicator from "@app/components/SocketStatusIndicator.svelte"
  import {
    ENABLE_ZAPS,
    CONTENT_KINDS,
    deriveSpaceMembers,
    deriveEventsForUrl,
    deriveUserRooms,
    deriveOtherRooms,
    userSpaceUrls,
    hasNip29,
    alerts,
    deriveUserCanCreateRoom,
    deriveUserIsSpaceAdmin,
  } from "@app/core/state"
  import {notifications} from "@app/util/notifications"
  import {pushModal} from "@app/util/modal"
  import {makeSpacePath, makeChatPath} from "@app/util/routes"

  const {url} = $props()

  const relay = deriveRelay(url)
  const chatPath = makeSpacePath(url, "chat")
  const goalsPath = makeSpacePath(url, "goals")
  const threadsPath = makeSpacePath(url, "threads")
  const calendarPath = makeSpacePath(url, "calendar")
  const userRooms = deriveUserRooms(url)
  const otherRooms = deriveOtherRooms(url)
  const members = deriveSpaceMembers(url)
  const hasAlerts = $derived($alerts.some(a => getTagValue("feed", a.tags)?.includes(url)))
  const userIsAdmin = deriveUserIsSpaceAdmin(url)

  const spaceKinds = derived(
    deriveEventsForUrl(url, [{kinds: CONTENT_KINDS}]),
    $events => new Set($events.map(e => e.kind)),
  )

  const openMenu = () => {
    showMenu = true
  }

  const toggleMenu = () => {
    showMenu = !showMenu
  }

  const showDetail = () => pushModal(SpaceDetail, {url}, {replaceState})

  const showMembers = () =>
    pushModal(
      ProfileList,
      {url, pubkeys: $members, title: `Members of`, subtitle: displayRelayUrl(url)},
      {replaceState},
    )

  const canCreateRoom = deriveUserCanCreateRoom(url)

  const createInvite = () => pushModal(SpaceInvite, {url}, {replaceState})

  const leaveSpace = () => pushModal(SpaceExit, {url}, {replaceState})

  const joinSpace = () => pushModal(SpaceJoin, {url}, {replaceState})

  const addRoom = () => pushModal(RoomCreate, {url}, {replaceState})

  const manageAlerts = () => {
    const component = hasAlerts ? Alerts : AlertAdd
    const params = {url, channel: "push", hideSpaceField: true}

    pushModal(component, params, {replaceState})
  }

  let showMenu = $state(false)
  let replaceState = $state(false)
  let element: Element | undefined = $state()

  onMount(() => {
    replaceState = Boolean(element?.closest(".drawer"))
  })
</script>

<div bind:this={element} class="flex h-full flex-col justify-between">
  <SecondaryNavSection>
    <div>
      <Button
        class="flex w-full flex-col rounded-xl p-3 transition-all hover:bg-base-100"
        onclick={openMenu}>
        <div class="flex items-center justify-between">
          <strong class="ellipsize flex items-center gap-1">
            <RelayName {url} />
          </strong>
          <Icon icon={AltArrowDown} />
        </div>
        <span class="text-xs text-primary">{displayRelayUrl(url)}</span>
      </Button>
      {#if showMenu}
        <Popover hideOnClick onClose={toggleMenu}>
          <ul
            transition:fly
            class="menu absolute z-popover mt-2 w-full gap-1 rounded-box bg-base-100 p-2 shadow-md">
            <li>
              <Button onclick={createInvite}>
                <Icon icon={LinkRound} />
                Create Invite
              </Button>
            </li>
            <li>
              <Button onclick={showDetail}>
                <Icon icon={RemoteControllerMinimalistic} />
                Space Information
              </Button>
            </li>
            <li>
              <Button onclick={showMembers}>
                <Icon icon={UserRounded} />
                View Members ({$members.length})
              </Button>
            </li>
            {#if $userIsAdmin}
              <li>
                <Link external href="https://landlubber.coracle.social">
                  <Icon icon={Tuning2} />
                  Manage Space
                  <Icon icon={SquareTopDown} size={4} class="opacity-50" />
                </Link>
              </li>
            {:else if $relay?.pubkey}
              <li>
                <Link href={makeChatPath([$relay.pubkey])}>
                  <Icon icon={Letter} />
                  Contact Owner
                </Link>
              </li>
            {/if}
            <li>
              {#if $userSpaceUrls.includes(url)}
                <Button onclick={leaveSpace} class="text-error">
                  <Icon icon={Exit} />
                  Leave Space
                </Button>
              {:else}
                <Button onclick={joinSpace} class="bg-primary text-primary-content">
                  <Icon icon={Login} />
                  Join Space
                </Button>
              {/if}
            </li>
          </ul>
        </Popover>
      {/if}
    </div>
    <div class="flex max-h-[calc(100vh-250px)] min-h-0 flex-col gap-1 overflow-auto">
      {#if hasNip29($relay)}
        <SecondaryNavItem {replaceState} href={makeSpacePath(url, "recent")}>
          <Icon icon={History} /> Recent Activity
        </SecondaryNavItem>
      {:else}
        <SecondaryNavItem
          {replaceState}
          href={chatPath}
          notification={$notifications.has(chatPath)}>
          <Icon icon={ChatRound} /> Chat
        </SecondaryNavItem>
      {/if}
      {#if ENABLE_ZAPS && $spaceKinds.has(ZAP_GOAL)}
        <SecondaryNavItem
          {replaceState}
          href={goalsPath}
          notification={$notifications.has(goalsPath)}>
          <Icon icon={StarFallMinimalistic} /> Goals
        </SecondaryNavItem>
      {/if}
      {#if $spaceKinds.has(THREAD)}
        <SecondaryNavItem
          {replaceState}
          href={threadsPath}
          notification={$notifications.has(threadsPath)}>
          <Icon icon={NotesMinimalistic} /> Threads
        </SecondaryNavItem>
      {/if}
      {#if $spaceKinds.has(EVENT_TIME)}
        <SecondaryNavItem
          {replaceState}
          href={calendarPath}
          notification={$notifications.has(calendarPath)}>
          <Icon icon={CalendarMinimalistic} /> Calendar
        </SecondaryNavItem>
      {/if}
      {#if hasNip29($relay)}
        {#if $userRooms.length > 0}
          <div class="h-2"></div>
          <SecondaryNavHeader>Your Rooms</SecondaryNavHeader>
        {/if}
        {#each $userRooms as h, i (h)}
          <MenuSpaceRoomItem {replaceState} notify {url} {h} />
        {/each}
        {#if $otherRooms.length > 0}
          <div class="h-2"></div>
          <SecondaryNavHeader>
            {#if $userRooms.length > 0}
              Other Rooms
            {:else}
              Rooms
            {/if}
          </SecondaryNavHeader>
        {/if}
        {#each $otherRooms as h, i (h)}
          <MenuSpaceRoomItem {replaceState} {url} {h} />
        {/each}
        {#if $canCreateRoom}
          <SecondaryNavItem {replaceState} onclick={addRoom}>
            <Icon icon={AddCircle} />
            Create room
          </SecondaryNavItem>
        {/if}
      {/if}
    </div>
  </SecondaryNavSection>
  <div class="flex flex-col gap-2 p-4">
    <Button class="btn btn-neutral btn-sm" onclick={showDetail}>
      <SocketStatusIndicator {url} />
    </Button>
    <Button class="btn btn-neutral btn-sm" onclick={manageAlerts}>
      <Icon icon={Bell} />
      Manage Alerts
    </Button>
  </div>
</div>
