import {
  useEffect,
  useRef,
  useState,
} from 'react'

import type { TCircleAnimation, TSetCircleAnimation } from 'features/CircleAnimationBar'
import type { PlaylistOption } from 'features/MatchPage/types'
import { Tab, TabsGroup } from 'features/Common'
import { T9n } from 'features/T9n'
import { useMatchPageStore } from 'features/MatchPage/store'

import { useEventListener } from 'hooks'

import { isIOS } from 'config/userAgent'

import { Tabs } from './config'
import { TabEvents } from './components/TabEvents'
import { TabWatch } from './components/TabWatch'
import { TabVideo } from './components/TabVideo'
import { useMatchSidePlaylists } from './hooks'
import {
  Wrapper,
  TabsWrapper,
  Container,
} from './styled'

const tabPanes = {
  [Tabs.WATCH]: TabWatch,
  [Tabs.EVENTS]: TabEvents,
  [Tabs.VIDEO]: TabVideo,
}

type Props = {
  circleAnimation?: TCircleAnimation,
  onSelect: (option: PlaylistOption) => void,
  selectedPlaylist?: PlaylistOption,
  setCircleAnimation?: TSetCircleAnimation,
}

export const MatchSidePlaylists = ({
  circleAnimation,
  onSelect,
  selectedPlaylist,
  setCircleAnimation,
}: Props) => {
  const {
    hideProfileCard,
    matchPlaylists: playlists,
    profile,
    showProfileCard,
    tournamentData,
  } = useMatchPageStore()

  const {
    isEventTabVisible,
    isVideoTabVisible,
    isWatchTabVisible,
    onTabClick,
    selectedTab,
  } = useMatchSidePlaylists()

  const TabPane = tabPanes[selectedTab]

  const containerRef = useRef<HTMLDivElement | null>(null)
  const tabPaneContainerRef = useRef<HTMLDivElement | null>(null)

  const [hasTabPaneScroll, setTabPaneScroll] = useState(false)

  useEffect(() => {
    const {
      clientHeight = 0,
      scrollHeight = 0,
    } = tabPaneContainerRef.current || {}
    const hasScroll = scrollHeight > clientHeight

    setTabPaneScroll(hasScroll)
  }, [
    playlists,
    selectedTab,
    tabPaneContainerRef.current?.clientHeight,
  ])

  useEventListener({
    callback: () => {
      const screenLandscape = isIOS ? window.orientation : window.screen.orientation.type
      const yOffset = containerRef.current?.scrollTop
      const isScreenLandscape = isIOS
        ? (screenLandscape === 90 || screenLandscape === -90)
        : (screenLandscape === 'landscape-primary' || screenLandscape === 'landscape-secondary')

      if (Number(yOffset) < 10) showProfileCard()
      if (Number(yOffset) > 10 && !isScreenLandscape) hideProfileCard()
    },
    event: 'scroll',
    target: containerRef,
  })

  return (
    <Wrapper ref={containerRef}>
      <TabsWrapper>
        <TabsGroup>
          {isWatchTabVisible ? (
            <Tab
              selected={selectedTab === Tabs.WATCH}
              onClick={() => onTabClick(Tabs.WATCH)}
            >
              <T9n t='watch' />
            </Tab>
          ) : null}
          {isEventTabVisible ? (
            <Tab
              selected={selectedTab === Tabs.EVENTS}
              onClick={() => onTabClick(Tabs.EVENTS)}
            >
              <T9n t='actions' />
            </Tab>
          ) : null}
          {isVideoTabVisible ? (
            <Tab
              selected={selectedTab === Tabs.VIDEO}
              onClick={() => onTabClick(Tabs.VIDEO)}
            >
              <T9n t='video' />
            </Tab>
          ) : null}
        </TabsGroup>

      </TabsWrapper>
      <Container
        hasScroll={hasTabPaneScroll}
        ref={tabPaneContainerRef}
        forVideoTab={selectedTab === Tabs.VIDEO}
      >
        <TabPane
          setCircleAnimation={setCircleAnimation}
          circleAnimation={circleAnimation}
          tournamentData={tournamentData}
          onSelect={onSelect}
          playlists={playlists}
          profile={profile}
          selectedPlaylist={selectedPlaylist}
        />
      </Container>
    </Wrapper>
  )
}
