import "./Sidebar.css";
import React, { useState, useEffect } from "react";
import { AnimatePresence, motion } from "framer-motion";

import { UserList } from "components/pages/Stream/UserList/UserList";
import { FullscreenButton } from "components/pages/Stream/FullscreenButton/FullscreenButton";
import { PopupShare } from "components/pages/Stream/PopupShare/PopupShare";
import { PopupExit } from "components/pages/Stream/PopupExit/PopupExit";
import { ControlPanel } from "../ControlPanel/ControlPanel";
import { WideSidebarButton } from "components/pages/Stream/WideSidebarButton/WideSidebarButton";

import {
  sidebarVariants,
  popupAnimation,
  wideSidebarVariants,
  wideSidebarAdminVariants,
} from "utils/animationProps";

export const Sidebar: React.FC<any> = ({ closeStream, exitPopup }) => {
  const [open, setOpen] = useState(false);
  const [popup, setPopup] = useState({
    popup1: false,
    popup2: false,
  });


  useEffect(() => {
    console.log('test')
  }, [exitPopup])

  const isAdmin = true;

  const [selected, setSelected] = useState(false);
  const [wideSidebar, setWideSidebar] = useState(false);

  const [isMuted, setMuted] = useState(true);

  const handleMuteClick = () => {
    setMuted((prev) => !prev);
  };

  function handleClosePopups() {
    setPopup({
      popup1: false,
      popup2: false,
    });
  }

  function handleOpenSharePopup() {
    setPopup({
      popup1: true,
      popup2: false,
    });
  }

  function handleOpenExitPopup() {
    setPopup({
      popup1: false,
      popup2: true,
    });
  }

  function closeSideBar() {
    setSelected(false);
    setOpen(false);
    setWideSidebar(false);
  }

  useEffect(() => () => unmountComponent(), []);


  function unmountComponent() {
    setOpen(false);
    setPopup({
      popup1: false,
      popup2: false,
    });
  }

  const setAnimation = () => {
    if (isAdmin) return wideSidebarAdminVariants;
    else {
      return wideSidebarVariants;
    }
  };

  return (
    <>
      <motion.div
       /// onHoverEnd={() => /// closeSideBar()}
        initial={false}
        animate={open ? "open" : "closed"}
        variants={wideSidebar ? setAnimation() : sidebarVariants}
        className="toolbar-container"
      >
        <div
          style={wideSidebar ? { overflow: "hidden" } : { overflow: "visible" }}
          className="toolbar-field"
        >
          <div className="toolbar-field-part">
            <FullscreenButton isSidebarWide={wideSidebar}> </FullscreenButton>
            <div className="toolbar-button-container-border-line"></div>
            <UserList
              closeSidebar={closeSideBar}
              isSidebarWide={wideSidebar} /// this is for disable showhing button's caption
              isAdmin={isAdmin}
            ></UserList>
          </div>
          <motion.div
            onHoverStart={() => setWideSidebar(true)}
            className="toolbar-field-part"
          >
            <WideSidebarButton
              isSidebarWide={wideSidebar}
              close={closeSideBar}
            ></WideSidebarButton>
          </motion.div>
          <ControlPanel
            isSidebarWide={wideSidebar}
            isMuted={isMuted}
            handleMuteClick={handleMuteClick}
            handleOpenSharePopup={handleOpenSharePopup}
            handleOpenExitPopup={handleOpenExitPopup}
          ></ControlPanel>
        </div>
        <AnimatePresence>
          {!open && (
            <motion.div
              variants={popupAnimation}
              initial={"hidden"}
              animate={"show"}
              exit={"hidden"}
              onClick={() => setOpen(true)}
              className="toolbar-open-button"
            >
              <span className="toolbar-open-button-icon"></span>
            </motion.div>
          )}
        </AnimatePresence>
      </motion.div>
      <AnimatePresence>
        {popup.popup1 && (
          <motion.div
            key={1}
            variants={popupAnimation}
            initial={"hidden"}
            animate={"show"}
            exit={"hidden"}
          >
            <PopupShare setClose={handleClosePopups}></PopupShare>
          </motion.div>
        )}

        {popup.popup2 && (
          <motion.div
            key={2}
            variants={popupAnimation}
            initial={"hidden"}
            animate={"show"}
            exit={"hidden"}
          >
            <PopupExit
              onExit={closeStream}
              setClose={handleClosePopups}
            ></PopupExit>
          </motion.div>
        )}
      </AnimatePresence>
    </>
  );
};
function getElementById(arg0: string) {
  throw new Error("Function not implemented.");
}

