import {useMemo} from 'react'
import {type TextStyle, View, type ViewStyle} from 'react-native'

import {type Interest, useInterestsDisplayNames} from '#/lib/interests'
import {capitalize} from '#/lib/strings/capitalize'
import {atoms as a, native, useTheme} from '#/alf'
import * as Toggle from '#/components/forms/Toggle'
import {Text} from '#/components/Typography'

export function InterestButton({interest}: {interest: Interest}) {
  const t = useTheme()
  const interestsDisplayNames = useInterestsDisplayNames()
  const ctx = Toggle.useItemContext()

  const styles = useMemo(() => {
    const hovered: ViewStyle[] = [
      {
        backgroundColor:
          t.name === 'light' ? t.palette.contrast_200 : t.palette.contrast_50,
      },
    ]
    const focused: ViewStyle[] = []
    const pressed: ViewStyle[] = []
    const selected: ViewStyle[] = [
      {
        backgroundColor: t.palette.contrast_900,
      },
    ]
    const selectedHover: ViewStyle[] = [
      {
        backgroundColor: t.palette.contrast_800,
      },
    ]
    const textSelected: TextStyle[] = [
      {
        color: t.palette.contrast_100,
      },
    ]

    return {
      hovered,
      focused,
      pressed,
      selected,
      selectedHover,
      textSelected,
    }
  }, [t])

  return (
    <View
      style={[
        {
          backgroundColor: t.palette.contrast_100,
          paddingVertical: 15,
        },
        a.rounded_full,
        a.px_2xl,
        ctx.hovered ? styles.hovered : {},
        ctx.focused ? styles.hovered : {},
        ctx.pressed ? styles.hovered : {},
        ctx.selected ? styles.selected : {},
        ctx.selected && (ctx.hovered || ctx.focused || ctx.pressed)
          ? styles.selectedHover
          : {},
      ]}>
      <Text
        style={[
          {
            color: t.palette.contrast_900,
          },
          a.font_semi_bold,
          native({paddingTop: 2}),
          ctx.selected ? styles.textSelected : {},
        ]}>
        {interestsDisplayNames[interest] || capitalize(interest)}
      </Text>
    </View>
  )
}
