import { NFTEventName, NFTFilterTypes } from '@uniswap/analytics-events'
import * as styles from 'nft/components/collection/Filters.css'
import { ChevronUpIcon } from 'nft/components/icons'
import { useCollectionFilters } from 'nft/hooks/useCollectionFilters'
import { TraitPosition, useTraitsOpen } from 'nft/hooks/useTraitsOpen'
import { getMarketplaceIcon } from 'nft/utils'
import { useEffect, useMemo, useState } from 'react'
import { ClickableTamaguiStyle, ThemedText } from 'theme/components'
import { Flex, LabeledCheckbox, Text } from 'ui/src'
import { sendAnalyticsEvent } from 'uniswap/src/features/telemetry/send'

export const MARKETPLACE_ITEMS = {
  x2y2: 'X2Y2',
  opensea: 'OpenSea',
  looksrare: 'LooksRare',
  sudoswap: 'SudoSwap',

  nftx: 'NFTX',
  nft20: 'NFT20',
  cryptopunks: 'LarvaLabs',
}

export const FilterItem = ({
  title,
  element,
  onClick,
}: {
  title: string | JSX.Element
  element: JSX.Element
  onClick: (e: any) => void
}) => {
  return (
    <Flex
      row
      onPress={onClick}
      justifyContent="space-between"
      px={10}
      pt="$padding16"
      pb="$padding12"
      cursor="pointer"
      borderRadius="$rounded12"
      hoverStyle={{
        backgroundColor: '$surface2',
      }}
    >
      <ThemedText.BodyPrimary>{title}</ThemedText.BodyPrimary>
      <ThemedText.SubHeaderSmall>{element}</ThemedText.SubHeaderSmall>
    </Flex>
  )
}

const MarketplaceItem = ({
  title,
  value,
  addMarket,
  removeMarket,
  isMarketSelected,
  count,
}: {
  title: string
  value: string
  addMarket: (market: string) => void
  removeMarket: (market: string) => void
  isMarketSelected: boolean
  count?: number
}) => {
  const [isCheckboxSelected, setCheckboxSelected] = useState(false)
  useEffect(() => {
    setCheckboxSelected(isMarketSelected)
  }, [isMarketSelected])
  const handleCheckbox = () => {
    if (!isCheckboxSelected) {
      addMarket(value)
      setCheckboxSelected(true)
    } else {
      removeMarket(value)
      setCheckboxSelected(false)
    }
    sendAnalyticsEvent(NFTEventName.NFT_FILTER_SELECTED, { filter_type: NFTFilterTypes.MARKETPLACE })
  }

  const checkbox = (
    <LabeledCheckbox
      variant="branded"
      checked={isCheckboxSelected}
      onCheckPressed={handleCheckbox}
      text={String(count)}
    />
  )

  const titleWithLogo = (
    <Flex row gap="$gap8">
      {getMarketplaceIcon(title, '16')}
      {title}
    </Flex>
  )

  return (
    <div key={value}>
      <FilterItem title={titleWithLogo} element={checkbox} onClick={handleCheckbox} />
    </div>
  )
}

export const FilterDropdown = ({
  title,
  items,
  onClick,
  isOpen,
}: {
  title: string
  items: JSX.Element[]
  onClick: (e: any) => void
  isOpen: boolean
}) => {
  return (
    <>
      <Flex
        my="$padding8"
        width="100%"
        borderColor="surface3"
        borderTopWidth={isOpen ? 1 : 0}
        opacity={isOpen ? 1 : 0}
      />
      <Flex {...ClickableTamaguiStyle} borderRadius={isOpen ? 0 : '$rounded12'}>
        <Flex
          row
          alignItems="center"
          justifyContent="space-between"
          p="$padding12"
          borderRadius="$rounded12"
          maxHeight={48}
          onPress={onClick}
          hoverStyle={{
            backgroundColor: '$surface3',
          }}
        >
          <Text variant="body2">{title}</Text>
          <Flex alignItems="center">
            <Flex
              className={styles.chevronContainer}
              style={{
                transform: `rotate(${isOpen ? 0 : 180}deg)`,
              }}
            >
              <ChevronUpIcon className={styles.chevronIcon} />
            </Flex>
          </Flex>
        </Flex>
        {isOpen && (
          <Flex pb="$padding8" pl={0}>
            {items}
          </Flex>
        )}
      </Flex>
    </>
  )
}

export const MarketplaceSelect = () => {
  const {
    addMarket,
    removeMarket,
    markets: selectedMarkets,
    marketCount,
  } = useCollectionFilters(({ markets, marketCount, removeMarket, addMarket }) => ({
    markets,
    marketCount,
    removeMarket,
    addMarket,
  }))

  const [isOpen, setOpen] = useState(!!selectedMarkets.length)
  const setTraitsOpen = useTraitsOpen((state) => state.setTraitsOpen)

  const MarketplaceItems = useMemo(
    () =>
      Object.entries(MARKETPLACE_ITEMS).map(([value, title]) => (
        <MarketplaceItem
          key={value}
          title={title}
          value={value}
          count={marketCount?.[value] || 0}
          {...{ addMarket, removeMarket, isMarketSelected: selectedMarkets.includes(value) }}
        />
      )),
    [addMarket, marketCount, removeMarket, selectedMarkets],
  )

  const onClick = (e: any) => {
    e.preventDefault()
    setOpen(!isOpen)
    setTraitsOpen(TraitPosition.MARKPLACE_INDEX, !isOpen)
  }

  return <FilterDropdown title="Marketplaces" items={MarketplaceItems} onClick={onClick} isOpen={isOpen} />
}
