import { html, render } from "/js/lib/lit-html.js";
import { auth } from "/js/auth.js";
import { View } from "/js/views/view.js";
import { pageEffect } from "/js/router.js";
import { headerTemplate } from "/js/templates/header.template.js";
import { profileFeedTemplate } from "/js/templates/profileFeed.template.js";
import { getDisplayName } from "/js/dataHelpers.js";
import "/js/components/infinite-scroll-container.js";

class ProfileFollowingView extends View {
  async render({
    root,
    params,
    context: {
      dataLayer,
      identityResolver,
      interactionHandlers,
      isAuthenticated,
    },
  }) {
    await auth.requireAuth();

    const { handleOrDid } = params;

    let profileDid = null;
    if (handleOrDid.startsWith("did:")) {
      profileDid = handleOrDid;
    } else {
      profileDid = await identityResolver.resolveHandle(handleOrDid);
    }

    function followingErrorTemplate({ error }) {
      console.error(error);
      return html`<div class="error-state">
        <div>Error loading following</div>
        <button @click=${() => window.location.reload()}>Try again</button>
      </div>`;
    }

    pageEffect(root, () => {
      const currentUser = dataLayer.derived.$currentUser.get();
      const profileFollowing =
        dataLayer.derived.$profileFollows.get(profileDid);
      const profile =
        dataLayer.derived.$hydratedDetailedProfiles.get(profileDid);
      const profileFollowingRequestStatus =
        dataLayer.requests.statusStore.$statuses.get(
          "loadProfileFollows-" + profileDid,
        );
      const hasMore = profileFollowing?.cursor ? true : false;

      const subtitle = profile?.followsCount
        ? `${profile.followsCount.toLocaleString()} following`
        : null;

      render(
        html`<div id="profile-following-view">
          ${headerTemplate({
            title: profile ? getDisplayName(profile) : "",
            subtitle,
          })}
          <main style="position: relative;">
            ${(() => {
              if (profileFollowingRequestStatus.error) {
                return followingErrorTemplate({
                  error: profileFollowingRequestStatus.error,
                });
              }
              return profileFeedTemplate({
                profiles: profileFollowing?.follows ?? null,
                hasMore,
                onLoadMore: loadFollowing,
                emptyMessage: "Not following anyone yet.",
                isAuthenticated,
                currentUserDid: currentUser?.did ?? null,
                profileInteractionHandler:
                  interactionHandlers.profileInteractionHandler,
              });
            })()}
          </main>
        </div>`,
        root,
      );
    });

    async function loadFollowing() {
      const profileFollowing =
        dataLayer.derived.$profileFollows.get(profileDid);
      const cursor = profileFollowing?.cursor;
      await dataLayer.requests.loadProfileFollows(profileDid, { cursor });
    }

    root.addEventListener("page-enter", async () => {
      // Load the profile to get the follows count
      dataLayer.declarative.ensureDetailedProfile(profileDid);
      await loadFollowing();
    });

    root.addEventListener("page-restore", async (e) => {
      const scrollY = e.detail?.scrollY ?? 0;
      if (scrollY > 0) {
        window.scrollTo(0, scrollY);
      }
    });
  }
}

export default new ProfileFollowingView();
