import { useMemo, useState } from 'react'

import includes from 'lodash/includes'
import filter from 'lodash/filter'
import isEmpty from 'lodash/isEmpty'
import size from 'lodash/size'
import uniq from 'lodash/uniq'
import difference from 'lodash/difference'
import map from 'lodash/map'
import every from 'lodash/every'

import type { Events } from 'requests'
import type { Playlists } from 'features/MatchPage/types'

type TTogglePlayers = {
  id: Number,
  team: String,
}

type TFilters = {
  events: Array<Number>,
  players: Array<Number>,
}

type Props = {
  events: Events,
  matchPlaylists: Playlists,
}

export const useFiltersPopup = ({
  events,
  matchPlaylists,
}: Props) => {
  const [isOpen, setIsOpen] = useState(false)
  const [activeEvents, setActiveEvents] = useState<Array<Number>>([])
  const [activeFirstTeamPlayers, setActiveFirstTeamPlayers] = useState<Array<Number>>([])
  const [activeSecondTeamPlayers, setActiveSecondTeamPlayers] = useState<Array<Number>>([])
  const [activeFilters, setActiveFilters] = useState<TFilters>({ events: [], players: [] })

  const currentEvents = filter(events, (event) => event.pl !== undefined
    && event.t !== undefined)

  const uniqEvents = uniq(map(currentEvents, ({ l }) => l))
  const uniqPlayersTeam1 = uniq(map(matchPlaylists.players.team1, ({ id }) => id))
  const uniqPlayersTeam2 = uniq(map(matchPlaylists.players.team2, ({ id }) => id))

  const isAllActionsChecked = every(uniqEvents, (el) => (includes(activeEvents, el)))
  const isFirstTeamPlayersChecked = every(
    uniqPlayersTeam1, (el) => (includes(activeFirstTeamPlayers, el)),
  )
  const isSecondTeamPlayersChecked = every(
    uniqPlayersTeam2, (el) => (includes(activeSecondTeamPlayers, el)),
  )

  const toggle = () => {
    setIsOpen(!isOpen)
  }

  const close = () => {
    setIsOpen(false)
  }

  const toggleActiveEvents = (id: number) => () => {
    setActiveEvents(includes(activeEvents, id)
      ? [...filter(activeEvents, (item) => item !== id)]
      : [...activeEvents, id])
  }

  const toggleActivePlayers = ({ id, team }: TTogglePlayers) => () => {
    const teamState = team === 'team1'
      ? activeFirstTeamPlayers
      : activeSecondTeamPlayers

    const setterTeamState = team === 'team1'
      ? setActiveFirstTeamPlayers
      : setActiveSecondTeamPlayers

    setterTeamState(includes(teamState, id)
      ? [...filter(teamState, (item) => item !== id)]
      : [...teamState, id])
  }

  const allPlayersToggle = (team: string) => () => {
    const setterTeamState = team === 'team1'
      ? setActiveFirstTeamPlayers
      : setActiveSecondTeamPlayers

    const uniqValues = team === 'team1'
      ? uniqPlayersTeam1
      : uniqPlayersTeam2

    const isAllPlayersChecked = team === 'team1'
      ? isFirstTeamPlayersChecked
      : isSecondTeamPlayersChecked

    isAllPlayersChecked
      ? setterTeamState((currentValues) => difference(currentValues, uniqValues))
      : setterTeamState((currentValues) => uniq([...currentValues, ...uniqValues]))
  }

  const allActionsToggle = () => {
    isAllActionsChecked
      ? setActiveEvents((currentValues) => difference(currentValues, uniqEvents))
      : setActiveEvents((currentValues) => uniq([...currentValues, ...uniqEvents]))
  }

  const applyFilters = () => {
    setActiveFilters({
      events: [...activeEvents],
      players: [...activeFirstTeamPlayers, ...activeSecondTeamPlayers],
    })
    close()
  }

  const isEmptyFilters = useMemo(() => (
    isEmpty(activeFilters.events) && isEmpty(activeFilters.players)), [activeFilters])

  const countOfFilters = useMemo(() => (
    size(activeFilters.events) + size(activeFilters.players)), [activeFilters])

  return {
    activeEvents,
    activeFirstTeamPlayers,
    activeSecondTeamPlayers,
    allActionsToggle,
    allPlayersToggle,
    applyFilters,
    close,
    countOfFilters,
    filters: activeFilters,
    isAllActionsChecked,
    isEmptyFilters,
    isFirstTeamPlayersChecked,
    isOpen,
    isSecondTeamPlayersChecked,
    toggle,
    toggleActiveEvents,
    toggleActivePlayers,
    uniqEvents,
  }
}
