import { onMounted, onUnmounted, watchEffect } from "vue";

export function dragSplitPane() {
  let pos = { y: 0 };
  const dragSpliter = $ref<HTMLDivElement>();
  let topPercent = $ref(40);

  const mouseDownHandler = function (e: MouseEvent) {
    // console.log(e);
    if (e.target !== dragSpliter) return;
    e.preventDefault();
    pos = {
      // Get the current mouse position
      y: e.clientY,
    };
    // topPercent = 100 * e.offsetTop / e.target.parentElement.style.height;
    if (dragSpliter) {
      // dragSpliter.style.cursor = "row-resize";
      // console.log("add");

      document.addEventListener("mousemove", mouseMoveHandler);
      document.addEventListener("mouseup", mouseUpHandler);
    }
    return false;
  };

  const mouseMoveHandler = function (e: MouseEvent) {
    e.preventDefault();
    const dy = e.clientY - pos.y;

    // // Scroll the element
    // // dragSpliter.style.marginTop =
    // //   (parseFloat(dragSpliter.style.marginTop) || 0) + dy + "px";
    dragSpliter.style.marginTop = `${dy}px`;
    // console.log(dragSpliter.style.marginTop);
    // const target = e.target as HTMLElement;
    // const parent = target.parentElement;
    // console.log(getComputedStyle(e.target.parentElement).height);
    // console.log({
    //   offsetTop: target.offsetTop,
    //   dy: dy,
    //   movementY: e.movementY,
    // });
    // if (parent)
    //   topPercent =
    //     (100 * target.offsetTop) / parseFloat(getComputedStyle(parent).height);
    // console.log("topPercent", topPercent);
    return false;
  };

  const mouseUpHandler = function (e: MouseEvent) {
    // const dy = e.clientY - pos.y;

    // Scroll the element
    // dragSpliter.style.marginTop =
    //   (parseFloat(dragSpliter.style.marginTop) || 0) + dy + "px";
    // dragSpliter.style.marginTop = dy + "px";
    // console.log(dragSpliter.style.marginTop);
    const target = e.target as HTMLElement;
    let parent = target.parentElement;
    // console.log(getComputedStyle(e.target.parentElement).height);
    // console.log("offsetTop", target.offsetTop);
    // console.log(target.offsetTop, parent);
    // 鼠标超出了分隔条的parent，则撤销拖动效果
    if (parent !== dragSpliter.parentElement) {
      parent = null;
    }
    if (parent)
      topPercent =
        (100 * target.offsetTop) / parseFloat(getComputedStyle(parent).height);
    // console.log({
    //   topPercent: topPercent,
    //   "parseFloat(getComputedStyle(parent).height)": parseFloat(
    //     getComputedStyle(parent).height
    //   ),
    //   "target.offsetTop": target.offsetTop,
    // });

    // console.log("removed");
    if (dragSpliter) {
      document.removeEventListener("mousemove", mouseMoveHandler);
      document.removeEventListener("mouseup", mouseUpHandler);
      // dragSpliter.style.cursor = "auto";
      dragSpliter.style.marginTop = "0";
    }
  };

  onMounted(() => {
    watchEffect(() => {
      if (dragSpliter) {
        // console.log("watchEffect");
        document.addEventListener("mousedown", mouseDownHandler);
      }
    });
  });
  onUnmounted(() => {
    if (dragSpliter) {
      document.removeEventListener("mousedown", mouseDownHandler);
      document.removeEventListener("mousemove", mouseMoveHandler);
      document.removeEventListener("mouseup", mouseUpHandler);
    }
  });

  return $$({ dragSpliter, topPercent });
}
