import React, { useRef, useState } from "react";
import { TrashIcon } from "@/assets/svgs";
import { LazyLoad } from "@/components/LazyLoad";
import { StringCaser } from "@/utils/utils";
import { DropdownOption, DropdownOptions } from "@/components/DropdownOptions";
import { WorkflowWrapperModal } from "../WorkflowWrapperModal";
import { WorkflowTypes } from "@/utils/constants";

const WorkflowWrapper = ({
  segment,
  parentId,
  onDeleteWorkflowData,
  setWorkflowData,
  index,
  workflow,
  totalFlow,
  addVariable,
  variables,
  onSave,
  updateWorkflow,
}) => {
  const containerRef = useRef(null);
  const dropdownOptions = [
    {
      action: () => onDeleteWorkflowData(workflow?.id, segment, parentId),
      icon: <TrashIcon />,
    },
  ];
  const [open, setOpen] = useState(false);
  const [draggedOver, setDraggedOver] = useState(false);
  const [hovered, setHovered] = useState(false);

  const onDrop = (e) => {
    e.preventDefault();
    if (workflow?.name === WorkflowTypes.SuccessResponse) {
      return;
    }
    setWorkflowData(workflow?.id);
    setDraggedOver(false);
  };
  const onDragOver = (e) => {
    e.preventDefault();
    if (workflow?.name === WorkflowTypes.SuccessResponse) {
      return;
    }
    if (!draggedOver) {
      setDraggedOver(true);
    }
  };
  const handleMouseEnter = (e) => {
    e.preventDefault();
    if (workflow?.name === WorkflowTypes.SuccessResponse) {
      return;
    }
    if (!hovered) {
      setHovered(true);
    }
  };

  const handleMouseLeave = (e) => {
    e.preventDefault();
    if (workflow?.name === WorkflowTypes.SuccessResponse) {
      return;
    }
    setHovered(false);
  };

  const onDragLeave = (e) => {
    e.preventDefault();
    if (workflow?.name === WorkflowTypes.SuccessResponse) {
      return;
    }
    if (!hovered) {
      setDraggedOver(false);
    }
  };
  const onDragEnd = (e) => {
    e.preventDefault();
    if (workflow?.name === WorkflowTypes.SuccessResponse) {
      return;
    }
    if (!hovered) {
      setDraggedOver(false);
    }
  };

  const onToggleModal = (value, e) => {
    if (value && e && e.target?.classList?.contains("control")) {
      setOpen(true);
    } else if (!value && open) {
      setOpen(false);
    }
  };
  return (
    <div
      onMouseEnter={(e) => !parentId && handleMouseEnter(e)}
      onMouseLeave={(e) => !parentId && handleMouseLeave(e)}
      className={`relative flex h-fit max-h-fit min-h-fit w-fit select-none flex-col ${
        workflow?.name === WorkflowTypes.SuccessResponse ? "" : ""
      }`}
    >
      <div
        ref={containerRef}
        onDragLeave={(e) => !parentId && onDragLeave(e)}
        onDragOver={(e) => !parentId && onDragOver(e)}
        onDragEnd={(e) => !parentId && onDragEnd(e)}
        onDrop={(e) => !parentId && onDrop(e)}
        className={``}
      >
        <div
          onClick={(e) => onToggleModal(true, e)}
          className="control group relative flex h-[4.3438rem] max-h-[4.3375rem] min-h-[4.3438rem] w-[16.25rem] cursor-pointer flex-col gap-1 rounded-lg border border-solid border-primaryBlue bg-white p-5 px-3 py-2 shadow-md shadow-primaryBlue transition ease-in-out"
        >
          <div className={"flex h-7 max-h-7 w-full gap-3"}>
            <div className="control z-1000 flex grow items-center justify-start gap-2">
              <span className="control h-5 max-h-5 rounded-md bg-primaryBlue px-2 py-1 text-[.725rem] leading-snug tracking-widest text-white"></span>
              <span className="control">
                {StringCaser(workflow?.name, {
                  casetype: "capitalize",
                  separator: " ",
                })}
              </span>
            </div>
            {workflow?.name === WorkflowTypes.SuccessResponse &&
            !parentId ? null : (
              <LazyLoad>
                <DropdownOptions className={`hidden group-hover:block`}>
                  {dropdownOptions.map((item, index) => (
                    <DropdownOption
                      key={index}
                      icon={item.icon}
                      name={item.name}
                      onClick={item.action}
                    />
                  ))}
                </DropdownOptions>
              </LazyLoad>
            )}
          </div>
          <span className="control">{workflow?.action_name}</span>
        </div>
        <div>
          <LazyLoad count={5}>
            <WorkflowWrapperModal
              open={open}
              onToggleModal={onToggleModal}
              parentRef={containerRef}
              workflow={workflow}
              addVariable={addVariable}
              segment={segment}
              parentId={parentId}
              variables={variables}
              onSave={onSave}
              updateWorkflow={updateWorkflow}
            />
          </LazyLoad>
        </div>
      </div>

      {!parentId ? (
        <div
          className={`flex w-[16.25rem] flex-col items-center justify-start`}
        >
          <div
            onDragLeave={(e) => !parentId && onDragLeave(e)}
            onDragOver={(e) => !parentId && onDragOver(e)}
            onDragEnd={(e) => !parentId && onDragEnd(e)}
            onDrop={(e) => !parentId && onDrop(e)}
            className={`flex w-[16.25rem] justify-center`}
          >
            <div
              className={`${
                index + 1 < totalFlow ? "flex" : "hidden"
              }  h-[1.5rem] max-h-[1.5rem] min-h-[1.5rem] w-0 justify-center  border-2 border-[#e5e7eb] text-[#a8a8a8]`}
            ></div>
          </div>

          {[WorkflowTypes.SuccessResponse].includes(workflow?.name) ? null : (
            <div
              className={`flex h-fit w-[16.25rem] flex-col items-center justify-center`}
              onDragLeave={(e) => !parentId && onDragLeave(e)}
              onDragOver={(e) => !parentId && onDragOver(e)}
              onDragEnd={(e) => !parentId && onDragEnd(e)}
              onDrop={(e) => !parentId && onDrop(e)}
            >
              <div
                className={`${
                  draggedOver
                    ? "h-[4.3438rem] max-h-[4.3438rem] min-h-[4.3438rem] w-[16.25rem] border"
                    : "flex h-0 w-0 items-center justify-center border-2 "
                } relative cursor-pointer self-center rounded-lg border-solid border-primaryBlue bg-white text-primaryBlue transition-all`}
              >
                {!draggedOver && <span>+</span>}
              </div>
              <div
                className={`h-[1.5rem] max-h-[1.5rem] min-h-[1.5rem] w-0 items-start justify-center border-2 border-[#e5e7eb] text-[#a8a8a8]`}
              ></div>
            </div>
          )}
        </div>
      ) : null}
    </div>
  );
};

export default WorkflowWrapper;
