import { useSlider } from 'features/StreamPlayer/hooks/useSlider'
import { TimeTooltip } from 'features/StreamPlayer/components/TimeTooltip'
import { Scrubber, ScrubberContainer } from 'features/StreamPlayer/components/ProgressBar/styled'

import { isIOS } from 'config/userAgent'

import { Chapters } from '../Chapters'
import type { Props } from './hooks'
import { useProgressBar } from './hooks'
import { ProgressBarList } from './styled'

export const ProgressBar = (props: Props) => {
  const { isScrubberVisible, onPlayedProgressChange } = props
  const progressBarRef = useSlider({ onChange: onPlayedProgressChange })
  const {
    calculatedChapters,
    playedProgressInPercent,
    time,
  } = useProgressBar(props)
  return (
    <ProgressBarList
      ref={progressBarRef}
      isIOS={isIOS}
    >
      <Chapters chapters={calculatedChapters} />
      {isScrubberVisible && (
        <ScrubberContainer>
          <Scrubber
            isIOS={isIOS}
            style={{ left: `${playedProgressInPercent}%` }}
          >
            <TimeTooltip time={time} />
          </Scrubber>
        </ScrubberContainer>
      )}
    </ProgressBarList>
  )
}
