import { computed, ref, watch } from 'vue'
import { useRouter } from 'vue-router'

import { useAuthStore } from '../../stores/auth'
import { useAwardsStore } from '../../stores/awards'
import { useHomeArchivePresentation } from './useHomeArchivePresentation'
import type { HomePreviewPhase } from './homeLandingTypes'
import { useHomeLandingModalState } from './useHomeLandingModalState'
import {
  useHomeLandingOverviewPresentation,
  useHomeModalCandidatePresentation,
} from './useHomeLandingPresentation'
import { useHomePhasePresentation } from './useHomePhasePresentation'
import { useHomeParticipationState } from './useHomeParticipationState'
import { useHomeSocialPresentation } from './useHomeSocialPresentation'

export function useHomeLandingState() {
  const store = useAwardsStore()
  const authStore = useAuthStore()
  const router = useRouter()

  const activeCat = ref(0)
  const previewPhase = ref<HomePreviewPhase>('voting')
  const clipSubmissionsEnabled = computed(() => store.overview.featureFlags.clipSubmissionsEnabled)
  const clipSubmissionDisabledMessage = computed(() => store.overview.featureFlags.clipSubmissionDisabledMessage)

  const {
    role,
    isGuest,
    isUser,
    isAdmin,
    twitchUser,
    siteContent,
    faqItems,
    publicStreamUrl,
    showDate,
    showStartsAt,
    currentYear,
    displayCategories,
    landingCategories,
    candidateCount,
    bootstrapArchiveYears,
    formatRange,
    formatTimelineRange,
    formatShowDate,
    initialsFor,
  } = useHomeLandingOverviewPresentation(store, authStore)
  const {
    hostSocialLinks,
    communitySocialLinks,
    footerLinks,
    privacyContentHtml,
    platformKey,
    isUploadedSocialIcon,
    socialSimpleIconPath,
    socialSimpleIconColor,
  } = useHomeSocialPresentation(siteContent)
  const {
    modal,
    deleteConfirm,
    accountActionError,
    archiveYear,
    privacyModalOpen,
    accountModalOpen,
    archiveModalOpen,
    modalOpen,
    isShow,
    isVote,
    isPicker,
    isClip,
    deleteNotConfirm,
    openModal,
    closeModal,
    openVote,
    openNominate,
    openClip,
    stop,
    onOpenAccount,
    onCloseAccount,
    onOpenPrivacy,
    onClosePrivacy,
    onOpenArchive,
    onCloseArchive,
    setArchiveYear,
    onRequestDelete,
    onCancelDelete,
  } = useHomeLandingModalState(store, bootstrapArchiveYears, () => {
    submitted.value = false
    formError.value = ''
    successKind.value = null
    if (modal.value === 'clip') {
      clipDsgvo.value = false
    }
  })
  const {
    archiveYears,
    selectedArchive,
    winnerShowcaseYear,
    winnerShowcase,
    archiveYearButtonStyle,
    winnerPlatformLabel,
    winnerPlatformStyle,
  } = useHomeArchivePresentation(store, archiveYear)
  const {
    nominationPhase,
    votingPhase,
    preparationPhase,
    showPhase,
    completedPhase,
    showCountdown,
    streamLive,
    streamLocked,
    phaseCardTitle,
    phaseCardDescription,
    phaseCardRange,
    phaseStatusLabel,
    phaseStatusStyle,
    phasePrimaryLabel,
    phasePrimaryDisabled,
    phasePrimaryActionStyle,
    streamEyebrow,
    streamTitle,
    streamText,
    streamMeta,
    streamButtonLabel,
    streamButtonUrl,
    streamLockedTitle,
    statOneValue,
    statOneLabel,
    statTwoValue,
    statThreeValue,
    statThreeLabel,
    categoryIntroText,
    timelineLineStyle,
    sectionTitle,
    sectionText,
    sectionActionLabel,
    sectionActionHref,
    sectionActionDisabled,
    sectionActionStyle,
  } = useHomePhasePresentation({
    previewPhase,
    landingCategories,
    candidateCount,
    siteContent,
    publicStreamUrl,
    showDate,
    showStartsAt,
    currentYear,
    formatRange,
    formatShowDate,
  })
  const {
    votes,
    submitted,
    submitting,
    formError,
    successKind,
    clipDsgvo,
    notSubmitted,
    voteCount,
    totalCats,
    canSubmitVote,
    savedVoteActive,
    activeCategory,
    activeCatName,
    pickerTitle,
    pickerSubtitle,
    successTitle,
    successText,
    clipNomOptions,
    catOptions,
    canSubmitClip,
    clipSubmitStyle,
    initializeHomeInteractions,
    onLogin,
    onLogout,
    onConfirmDelete,
    openAdminPanel,
    setCat,
    pickNominee,
    submitVote,
    submitReminder,
    submitNomination,
    clipDsgvoChange,
    onClipCatChange,
    submitClip,
    onPrimaryPhaseAction,
    onSectionAction,
    onTimelineFinalAction,
    syncPreviewPhaseFromOverview,
  } = useHomeParticipationState({
    store,
    authStore,
    routerPush: async (path) => {
      await router.push(path)
    },
    twitchUser,
    displayCategories,
    archiveYears,
    archiveYear,
    modal,
    previewPhase,
    activeCat,
    deleteConfirm,
    accountActionError,
    openModal,
    closeAccountModal: onCloseAccount,
  })
  const { catList, noms } = useHomeModalCandidatePresentation({
    displayCategories,
    activeCat,
    modal,
    votes,
    activeCategory,
    setCat,
    pickNominee,
  })

  function setPreviewPhase(phase: HomePreviewPhase) {
    previewPhase.value = phase
    closeModal()
    activeCat.value = 0
    submitted.value = false
    formError.value = ''
    successKind.value = null
    clipDsgvo.value = false
  }

  function openVoteForPhase(event?: Event) {
    if (!votingPhase.value) {
      event?.preventDefault()
      return
    }

    openVote(event)
  }

  function openClipForPhase(event?: Event) {
    if (!nominationPhase.value || !clipSubmissionsEnabled.value) {
      event?.preventDefault()
      return
    }

    openClip(event)
  }

  watch([previewPhase, clipSubmissionsEnabled], ([phase, clipsEnabled]) => {
    if ((phase !== 'voting' && modal.value === 'vote') || ((phase !== 'nomination' || !clipsEnabled) && modal.value === 'clip')) {
      closeModal()
    }
  })

  return {
    store,
    authStore,
    role,
    isGuest,
    isUser,
    isAdmin,
    twitchUser,
    siteContent,
    hostSocialLinks,
    communitySocialLinks,
    footerLinks,
    faqItems,
    privacyContentHtml,
    publicStreamUrl,
    displayCategories,
    landingCategories,
    candidateCount,
    nominationPhase,
    votingPhase,
    preparationPhase,
    showPhase,
    completedPhase,
    showCountdown,
    streamLive,
    streamLocked,
    privacyModalOpen,
    accountModalOpen,
    archiveModalOpen,
    modalOpen,
    isShow,
    isVote,
    isPicker,
    isClip,
    notSubmitted,
    deleteNotConfirm,
    voteCount,
    totalCats,
    canSubmitVote,
    savedVoteActive,
    activeCatName,
    phaseCardTitle,
    phaseCardDescription,
    phaseCardRange,
    phaseStatusLabel,
    phaseStatusStyle,
    phasePrimaryLabel,
    phasePrimaryDisabled,
    phasePrimaryActionStyle,
    streamEyebrow,
    streamTitle,
    streamText,
    streamMeta,
    streamButtonLabel,
    streamButtonUrl,
    streamLockedTitle,
    statOneValue,
    statOneLabel,
    statTwoValue,
    statThreeValue,
    statThreeLabel,
    categoryIntroText,
    timelineLineStyle,
    sectionTitle,
    sectionText,
    sectionActionLabel,
    sectionActionHref,
    sectionActionDisabled,
    sectionActionStyle,
    previewPhase,
    pickerTitle,
    pickerSubtitle,
    successTitle,
    successText,
    clipNomOptions,
    catOptions,
    canSubmitClip,
    clipSubmitStyle,
    clipSubmissionsEnabled,
    clipSubmissionDisabledMessage,
    archiveYears,
    selectedArchive,
    winnerShowcaseYear,
    winnerShowcase,
    activeCat,
    submitted,
    submitting,
    formError,
    clipDsgvo,
    archiveYear,
    modal,
    catList,
    noms,
    formatTimelineRange,
    formatShowDate,
    initialsFor,
    openClip: openClipForPhase,
    openVote: openVoteForPhase,
    openNominate,
    onLogin,
    onOpenAccount,
    openAdminPanel,
    onPrimaryPhaseAction,
    isUploadedSocialIcon,
    socialSimpleIconPath,
    socialSimpleIconColor,
    platformKey,
    onSectionAction,
    closeModal,
    stop,
    submitReminder,
    submitVote,
    submitNomination,
    onClipCatChange,
    clipDsgvoChange,
    onOpenPrivacy,
    submitClip,
    onCloseArchive,
    archiveYearButtonStyle,
    winnerPlatformStyle,
    winnerPlatformLabel,
    privacyModalStop: stop,
    accountModalStop: stop,
    archiveModalStop: stop,
    onClosePrivacy,
    onCloseAccount,
    deleteConfirm,
    accountActionError,
    onLogout,
    onRequestDelete,
    onCancelDelete,
    onConfirmDelete,
    onOpenArchive,
    setArchiveYear,
    onTimelineFinalAction,
    setPreviewPhase,
    syncPreviewPhaseFromOverview,
    initializeHomeInteractions,
  }
}
