import { Fragment } from 'react'
import type { ReactNode } from 'react'
import { Link } from 'react-router-dom'

import {
  PAGES,
  SportTypes,
  ProfileTypes,
} from 'config'

import { client } from 'config/clients'
import { isMobileDevice } from 'config/userAgent'

import { Menu } from 'features/Menu'
import { Search } from 'features/Search'
import { ScoreSwitch } from 'features/MatchSwitches'

import { isMatchPage } from 'helpers/isMatchPage'

import { usePageParams } from 'hooks/usePageParams'
import { useProfileColor } from './hooks'

import {
  HeaderStyled,
  HeaderGroup,
  HeaderLogo,
  Position,
} from './styled'

type Props = {
  children?: ReactNode,
  color?: string,
  headerImage?: string | null,
  height?: number,
  profileId?: number,
}

export const ProfileHeader = ({
  children,
  color: headerColor,
  headerImage,
  height,
  profileId,
}: Props) => {
  const {
    profileType,
    sportType,
  } = usePageParams()

  const color = useProfileColor(profileId)
  const imageHeader = (profileId === 5704 || 316)
    && sportType === SportTypes.FOOTBALL
    && profileType === ProfileTypes.TOURNAMENTS ? `/images/${profileId}` : headerImage

  return (
    <Fragment>
      <HeaderStyled
        headerImage={imageHeader}
        color={headerColor || color}
        height={height}
        isMatchPage={isMatchPage()}
      >
        <Position
          isMatchPage={isMatchPage()}
          top={client.styles.logoTop}
          left={client.styles.logoLeft}
        >
          <HeaderGroup>
            <Link to={PAGES.home}>
              <HeaderLogo isMatchPage={isMatchPage()} />
            </Link>

            {client.showSearch && <Search />}
          </HeaderGroup>
        </Position>

        {children}

        <Position top={isMobileDevice ? client.styles.logoTop : undefined} right={0.71}>
          <HeaderGroup>
            {!isMobileDevice ? <ScoreSwitch /> : null}
            <Menu />
          </HeaderGroup>
        </Position>
      </HeaderStyled>
    </Fragment>
  )
}
