import { useState, useRef, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { createPortal } from 'react-dom';

import Grid from '@mui/material/Grid'
import SmallMenuBar from './SmallMenuBar'
import SearchLists from './SearchLists';
import SearchBar from './SearchBar'
import ChatsList from './ChatsList'
import MenuBar from './MenuBar'
import ContactsList from './ContactsList'
import AddContact from './AddContact'
import SettingsBar from './SettingsBar';
import EditBar from './EditBar';
import { getState } from '../../../redux/authorization/selector'

const LeftBar = () => {
  const { sort,nightMode } = useSelector(getState)
  const [isSearch, setIsSearch] = useState<boolean>(false)
  const [isMenu, setIsMenu] = useState<boolean>(false)
  const [isMenuSm, setIsMenuSm] = useState<boolean>(false);
  const [selectedIndex, setSelectedIndex] = useState<number | null>(null)
  const [value, setValue] = useState<string>('')
  const modalRoot = useRef<HTMLDivElement|null>(null);
  const handleFocus = (): void => setIsSearch(true)
  const handleClick = (): void => {
    setValue('')
    if (selectedIndex) setSelectedIndex(null)
    if(!isSearch) return setIsMenu(!isMenu)
    setIsSearch(false)
  }
  const handleSearch = (e: React.ChangeEvent<HTMLInputElement>):void => setValue(e.target.value)
  const handleEnterOpenMenuSm = (): void => {
    if(!isMenuSm) setIsMenuSm(true)
  }
  const handleLeaveCloseMenuSm = (e: any): void => {
    if(isMenuSm&&e.pageX > 480) setIsMenuSm(false)
  }
  const handleSelectedMenu = (i: number) => {
    setIsSearch(true)
    setIsMenu(false)
    setIsMenuSm(false)
    setValue('')
    setSelectedIndex(i)
  }
  
  useEffect(() => {
    const handleCloseModal = (e:any) => {
      if (e.target.id === 'overlay') {
        setIsMenu(false)
        setIsMenuSm(false)
      }
    }
    if (!modalRoot.current) {
    const modal = document.getElementById('modal-root') as HTMLDivElement | null
      if (modal) {
      modal.addEventListener('click',handleCloseModal)
      modalRoot.current = modal
       }     
    }
    return () => {
      if (modalRoot.current) {
      modalRoot.current.removeEventListener('click',handleCloseModal)
      modalRoot.current = null        
      }
    }
  }, [])

    return (
      <Grid item lg={3} onMouseEnter={handleEnterOpenMenuSm} onMouseLeave={handleLeaveCloseMenuSm}>
        {selectedIndex !== 2 && selectedIndex !== 4 &&
          <SearchBar handleClick={handleClick} handleFocus={handleFocus} sort={sort}
          handleSearch={handleSearch} isSearch={isSearch} selectedIndex={selectedIndex} value={value}/>}
        {!selectedIndex&&isSearch && <SearchLists value={value} setValue={setValue} sort={sort}/>}
        {!selectedIndex&&!isSearch &&<ChatsList sort={sort}/>}
        {!selectedIndex && isMenuSm && !isSearch && <SmallMenuBar
          handleSelectedMenu={handleSelectedMenu} setIsMenuSm={setIsMenuSm} />}
        {isMenu && modalRoot.current &&
          createPortal(<MenuBar handleSelectedMenu={handleSelectedMenu} nightMode={nightMode}/>, modalRoot.current)}
        {selectedIndex === 1 && <ContactsList handleClick={handleClick} value={value} sort={sort}/>}
        {selectedIndex === 2 && <SettingsBar setSelectedIndex={setSelectedIndex} handleClick={handleClick}/>}
        {selectedIndex === 3 && <AddContact setSelectedIndex={setSelectedIndex} />}
        {selectedIndex === 4 && <EditBar setSelectedIndex={setSelectedIndex}/>}
    </Grid>
    )
}

export default LeftBar