import React, { useMemo } from 'react'
import { VirtualBackgroundType } from '@zocks-communications/client-sdk-js'
import { MenuItem, SelectButton, getClassName, getMergedClassNames } from '../common'
import { VirtualBackgroundSelectorProps } from './models'
import zocksStyle from './VirtualBackgroundSelector.module.scss'

export const VirtualBackgroundSelector: React.FC<VirtualBackgroundSelectorProps> = ({
  availableVirtualBackgrounds,
  selectVirtualBackground,
  virtualBackgroundIsLoading,
  styles,
}) => {
  const NO_BACKGROUND = 'None'

  const backgroundMenuItems = useMemo(() => {
    return [
      { label: NO_BACKGROUND },
      ...availableVirtualBackgrounds.map((background) => ({ label: background.type, value: background.url })),
    ]
  }, [availableVirtualBackgrounds])

  const selectMenuItem = (menuItem: MenuItem) => {
    const { label, value } = menuItem
    if (label === NO_BACKGROUND) {
      selectVirtualBackground(null)
    } else {
      selectVirtualBackground({ type: label as VirtualBackgroundType, url: value })
    }
  }

  const mergedStyles = getMergedClassNames(zocksStyle, styles)
  return (
    <div className={getClassName('virtual-background-select-container', mergedStyles)}>
      <div className={getClassName('virtual-background-select-text', mergedStyles)}>Background</div>
      <SelectButton
        styles={mergedStyles}
        menuItems={backgroundMenuItems}
        onMenuItemSelected={selectMenuItem}
        disabled={virtualBackgroundIsLoading}
        buttonLabel='Background'
      />
    </div>
  )
}
