'use client'

import type {
  ReactNode,
} from 'react'
import type { Plugin } from '../types'
import type {
  MarketplaceCollection,
  PluginsSort,
  SearchParams,
  SearchParamsFromCollection,
} from './types'
import { debounce, noop } from 'es-toolkit/compat'
import {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from 'react'
import {
  createContext,
  useContextSelector,
} from 'use-context-selector'
import { useInstalledPluginList } from '@/service/use-plugins'
import {
  getValidCategoryKeys,
  getValidTagKeys,
} from '../utils'
import { DEFAULT_SORT } from './constants'
import {
  useMarketplaceCollectionsAndPlugins,
  useMarketplaceContainerScroll,
  useMarketplacePlugins,
} from './hooks'
import { PLUGIN_TYPE_SEARCH_MAP } from './plugin-type-switch'
import {
  getMarketplaceListCondition,
  getMarketplaceListFilterType,
  updateSearchParams,
} from './utils'

export type MarketplaceContextValue = {
  searchPluginText: string
  handleSearchPluginTextChange: (text: string) => void
  filterPluginTags: string[]
  handleFilterPluginTagsChange: (tags: string[]) => void
  activePluginType: string
  handleActivePluginTypeChange: (type: string) => void
  page: number
  handlePageChange: () => void
  plugins?: Plugin[]
  pluginsTotal?: number
  resetPlugins: () => void
  sort: PluginsSort
  handleSortChange: (sort: PluginsSort) => void
  handleQueryPlugins: () => void
  handleMoreClick: (searchParams: SearchParamsFromCollection) => void
  marketplaceCollectionsFromClient?: MarketplaceCollection[]
  setMarketplaceCollectionsFromClient: (collections: MarketplaceCollection[]) => void
  marketplaceCollectionPluginsMapFromClient?: Record<string, Plugin[]>
  setMarketplaceCollectionPluginsMapFromClient: (map: Record<string, Plugin[]>) => void
  isLoading: boolean
  isSuccessCollections: boolean
}

export const MarketplaceContext = createContext<MarketplaceContextValue>({
  searchPluginText: '',
  handleSearchPluginTextChange: noop,
  filterPluginTags: [],
  handleFilterPluginTagsChange: noop,
  activePluginType: 'all',
  handleActivePluginTypeChange: noop,
  page: 1,
  handlePageChange: noop,
  plugins: undefined,
  pluginsTotal: 0,
  resetPlugins: noop,
  sort: DEFAULT_SORT,
  handleSortChange: noop,
  handleQueryPlugins: noop,
  handleMoreClick: noop,
  marketplaceCollectionsFromClient: [],
  setMarketplaceCollectionsFromClient: noop,
  marketplaceCollectionPluginsMapFromClient: {},
  setMarketplaceCollectionPluginsMapFromClient: noop,
  isLoading: false,
  isSuccessCollections: false,
})

type MarketplaceContextProviderProps = {
  children: ReactNode
  searchParams?: SearchParams
  shouldExclude?: boolean
  scrollContainerId?: string
  showSearchParams?: boolean
}

export function useMarketplaceContext(selector: (value: MarketplaceContextValue) => any) {
  return useContextSelector(MarketplaceContext, selector)
}

export const MarketplaceContextProvider = ({
  children,
  searchParams,
  shouldExclude,
  scrollContainerId,
  showSearchParams,
}: MarketplaceContextProviderProps) => {
  const { data, isSuccess } = useInstalledPluginList(!shouldExclude)
  const exclude = useMemo(() => {
    if (shouldExclude)
      return data?.plugins.map(plugin => plugin.plugin_id)
  }, [data?.plugins, shouldExclude])
  const queryFromSearchParams = searchParams?.q || ''
  const tagsFromSearchParams = searchParams?.tags ? getValidTagKeys(searchParams.tags.split(',')) : []
  const hasValidTags = !!tagsFromSearchParams.length
  const hasValidCategory = getValidCategoryKeys(searchParams?.category)
  const categoryFromSearchParams = hasValidCategory || PLUGIN_TYPE_SEARCH_MAP.all
  const [searchPluginText, setSearchPluginText] = useState(queryFromSearchParams)
  const searchPluginTextRef = useRef(searchPluginText)
  const [filterPluginTags, setFilterPluginTags] = useState<string[]>(tagsFromSearchParams)
  const filterPluginTagsRef = useRef(filterPluginTags)
  const [activePluginType, setActivePluginType] = useState(categoryFromSearchParams)
  const activePluginTypeRef = useRef(activePluginType)
  const [sort, setSort] = useState(DEFAULT_SORT)
  const sortRef = useRef(sort)
  const {
    marketplaceCollections: marketplaceCollectionsFromClient,
    setMarketplaceCollections: setMarketplaceCollectionsFromClient,
    marketplaceCollectionPluginsMap: marketplaceCollectionPluginsMapFromClient,
    setMarketplaceCollectionPluginsMap: setMarketplaceCollectionPluginsMapFromClient,
    queryMarketplaceCollectionsAndPlugins,
    isLoading,
    isSuccess: isSuccessCollections,
  } = useMarketplaceCollectionsAndPlugins()
  const {
    plugins,
    total: pluginsTotal,
    resetPlugins,
    queryPlugins,
    queryPluginsWithDebounced,
    cancelQueryPluginsWithDebounced,
    isLoading: isPluginsLoading,
    fetchNextPage: fetchNextPluginsPage,
    hasNextPage: hasNextPluginsPage,
    page: pluginsPage,
  } = useMarketplacePlugins()
  const page = Math.max(pluginsPage || 0, 1)

  useEffect(() => {
    if (queryFromSearchParams || hasValidTags || hasValidCategory) {
      queryPlugins({
        query: queryFromSearchParams,
        category: hasValidCategory,
        tags: hasValidTags ? tagsFromSearchParams : [],
        sortBy: sortRef.current.sortBy,
        sortOrder: sortRef.current.sortOrder,
        type: getMarketplaceListFilterType(activePluginTypeRef.current),
      })
      const url = new URL(window.location.href)
      if (searchParams?.language)
        url.searchParams.set('language', searchParams?.language)
      history.replaceState({}, '', url)
    }
    else {
      if (shouldExclude && isSuccess) {
        queryMarketplaceCollectionsAndPlugins({
          exclude,
          type: getMarketplaceListFilterType(activePluginTypeRef.current),
        })
      }
    }
  }, [queryPlugins, queryMarketplaceCollectionsAndPlugins, isSuccess, exclude])

  const handleQueryMarketplaceCollectionsAndPlugins = useCallback(() => {
    queryMarketplaceCollectionsAndPlugins({
      category: activePluginTypeRef.current === PLUGIN_TYPE_SEARCH_MAP.all ? undefined : activePluginTypeRef.current,
      condition: getMarketplaceListCondition(activePluginTypeRef.current),
      exclude,
      type: getMarketplaceListFilterType(activePluginTypeRef.current),
    })
    resetPlugins()
  }, [exclude, queryMarketplaceCollectionsAndPlugins, resetPlugins])

  const debouncedUpdateSearchParams = useMemo(() => debounce(() => {
    updateSearchParams({
      query: searchPluginTextRef.current,
      category: activePluginTypeRef.current,
      tags: filterPluginTagsRef.current,
    })
  }, 500), [])

  const handleUpdateSearchParams = useCallback((debounced?: boolean) => {
    if (!showSearchParams)
      return
    if (debounced) {
      debouncedUpdateSearchParams()
    }
    else {
      updateSearchParams({
        query: searchPluginTextRef.current,
        category: activePluginTypeRef.current,
        tags: filterPluginTagsRef.current,
      })
    }
  }, [debouncedUpdateSearchParams, showSearchParams])

  const handleQueryPlugins = useCallback((debounced?: boolean) => {
    handleUpdateSearchParams(debounced)
    if (debounced) {
      queryPluginsWithDebounced({
        query: searchPluginTextRef.current,
        category: activePluginTypeRef.current === PLUGIN_TYPE_SEARCH_MAP.all ? undefined : activePluginTypeRef.current,
        tags: filterPluginTagsRef.current,
        sortBy: sortRef.current.sortBy,
        sortOrder: sortRef.current.sortOrder,
        exclude,
        type: getMarketplaceListFilterType(activePluginTypeRef.current),
      })
    }
    else {
      queryPlugins({
        query: searchPluginTextRef.current,
        category: activePluginTypeRef.current === PLUGIN_TYPE_SEARCH_MAP.all ? undefined : activePluginTypeRef.current,
        tags: filterPluginTagsRef.current,
        sortBy: sortRef.current.sortBy,
        sortOrder: sortRef.current.sortOrder,
        exclude,
        type: getMarketplaceListFilterType(activePluginTypeRef.current),
      })
    }
  }, [exclude, queryPluginsWithDebounced, queryPlugins, handleUpdateSearchParams])

  const handleQuery = useCallback((debounced?: boolean) => {
    if (!searchPluginTextRef.current && !filterPluginTagsRef.current.length) {
      handleUpdateSearchParams(debounced)
      cancelQueryPluginsWithDebounced()
      handleQueryMarketplaceCollectionsAndPlugins()
      return
    }

    handleQueryPlugins(debounced)
  }, [handleQueryMarketplaceCollectionsAndPlugins, handleQueryPlugins, cancelQueryPluginsWithDebounced, handleUpdateSearchParams])

  const handleSearchPluginTextChange = useCallback((text: string) => {
    setSearchPluginText(text)
    searchPluginTextRef.current = text

    handleQuery(true)
  }, [handleQuery])

  const handleFilterPluginTagsChange = useCallback((tags: string[]) => {
    setFilterPluginTags(tags)
    filterPluginTagsRef.current = tags

    handleQuery()
  }, [handleQuery])

  const handleActivePluginTypeChange = useCallback((type: string) => {
    setActivePluginType(type)
    activePluginTypeRef.current = type

    handleQuery()
  }, [handleQuery])

  const handleSortChange = useCallback((sort: PluginsSort) => {
    setSort(sort)
    sortRef.current = sort

    handleQueryPlugins()
  }, [handleQueryPlugins])

  const handlePageChange = useCallback(() => {
    if (hasNextPluginsPage)
      fetchNextPluginsPage()
  }, [fetchNextPluginsPage, hasNextPluginsPage])

  const handleMoreClick = useCallback((searchParams: SearchParamsFromCollection) => {
    setSearchPluginText(searchParams?.query || '')
    searchPluginTextRef.current = searchParams?.query || ''
    setSort({
      sortBy: searchParams?.sort_by || DEFAULT_SORT.sortBy,
      sortOrder: searchParams?.sort_order || DEFAULT_SORT.sortOrder,
    })
    sortRef.current = {
      sortBy: searchParams?.sort_by || DEFAULT_SORT.sortBy,
      sortOrder: searchParams?.sort_order || DEFAULT_SORT.sortOrder,
    }
    handleQueryPlugins()
  }, [handleQueryPlugins])

  useMarketplaceContainerScroll(handlePageChange, scrollContainerId)

  return (
    <MarketplaceContext.Provider
      value={{
        searchPluginText,
        handleSearchPluginTextChange,
        filterPluginTags,
        handleFilterPluginTagsChange,
        activePluginType,
        handleActivePluginTypeChange,
        page,
        handlePageChange,
        plugins,
        pluginsTotal,
        resetPlugins,
        sort,
        handleSortChange,
        handleQueryPlugins,
        handleMoreClick,
        marketplaceCollectionsFromClient,
        setMarketplaceCollectionsFromClient,
        marketplaceCollectionPluginsMapFromClient,
        setMarketplaceCollectionPluginsMapFromClient,
        isLoading: isLoading || isPluginsLoading,
        isSuccessCollections,
      }}
    >
      {children}
    </MarketplaceContext.Provider>
  )
}
