import { useMount } from "ahooks";
import { useContext, useState } from "react";
import { CSSTransition } from "react-transition-group";
import guangImg from "~/assets/images/map_bi/guang.png";
import backImg from "~/assets/images/public/bigScreen/back.png";
import imgOk from "~/assets/images/public/bigScreen/imgok.png";
import topImg from "~/assets/images/public/bigScreen/title.png";
import { Context } from "~/pages/Container/Map/js/context";
import mitt from "~/pages/Container/Map/js/mitt";
import {
  changePeopleTrajectorySelectVisibleMittKey,
  clickBackMittKey,
  clickBranchOfficePointMittKey,
  clickPortPointMittKey,
  deletePeoplePositionPointMittKey,
  initBottomUtilsMittKey,
  resetAllBottomUtilsCheckMittKey,
  resetBottomCurrentIndexMittKey,
} from "~/pages/Container/Map/js/mittKey";
import "./index.less";

function Header() {
  const { currentPort, currentBranchOffice, mapMethods, area } = useContext(Context);

  const [animationKey, setAnimationKey] = useState(0);

  useMount(() => {
    setAnimationKey(Math.random());
  });

  const onBack = () => {
    mitt.emit(deletePeoplePositionPointMittKey);
    if (currentPort !== "00003" && currentBranchOffice) {
      mitt.emit(clickPortPointMittKey, { id: "" });
      mitt.emit(clickBranchOfficePointMittKey, { id: "" });
      mapMethods.current.removeWall();
      mapMethods.current.removeFourColorDiagram();
      mapMethods.current.removeBranchOfficePoint();
      mapMethods.current.removeMarkPoint();
      mapMethods.current.flyTo();
      mapMethods.current.addPortPoint();
    }
    else if (currentBranchOffice) {
      mitt.emit(clickBranchOfficePointMittKey, { id: "" });
      mapMethods.current.removeBranchOfficePoint();
      mapMethods.current.removeMarkPoint();
      mapMethods.current.returnPreviousCenterPoint();
      setTimeout(() => {
        mapMethods.current.addBranchOfficePoint(area);
      }, 2000);
    }
    else if (currentPort) {
      mitt.emit(clickPortPointMittKey, { id: "" });
      mapMethods.current.removeWall();
      mapMethods.current.removeFourColorDiagram();
      mapMethods.current.removeBranchOfficePoint();
      mapMethods.current.removeMarkPoint();
      mapMethods.current.flyTo();
      mapMethods.current.addPortPoint();
    }
    mitt.emit(initBottomUtilsMittKey);
    mitt.emit(resetBottomCurrentIndexMittKey);
    mitt.emit(resetAllBottomUtilsCheckMittKey);
    mitt.emit(clickBackMittKey);
    mitt.emit(changePeopleTrajectorySelectVisibleMittKey, false);
  };

  return (
    <div className="header_container">
      <CSSTransition
        in={animationKey !== 0}
        timeout={1000}
        classNames={{
          enter: "animate__animated",
          enterActive: "animate__animated animate__fadeInDown",
          exit: "animate__animated",
          exitActive: "animate__animated animate__fadeOutUp",
        }}
        unmountOnExit
      >
        <header style={{ backgroundImage: `url(${topImg})` }}>
          <CSSTransition
            in={!!(currentPort || currentBranchOffice)}
            timeout={1000}
            classNames={{
              enter: "animate__animated",
              enterActive: "animate__animated animate__fadeInDown",
              exit: "animate__animated",
              exitActive: "animate__animated animate__fadeOutUp",
            }}
            unmountOnExit
          >

          </CSSTransition>
          <div
            className="back"
            onClick={onBack}
          >
            {" "}
            <img src={backImg} alt="返回" />
            {" "}
            返回
          </div>
          <div className="title">秦港股份安全监管平台</div>
          <div style={{ backgroundImage: `url(${guangImg})` }} className="guang" />
          <div className="time">
            {" "}
            <img src={imgOk} />
            {" "}
            企业已连续平安运行 1095 天
          </div>
        </header>
      </CSSTransition>
    </div>
  );
}

export default Header;
