import { v4 as uuidv4 } from "uuid";
import _ from "lodash";
import { MenuOptions } from "../Data/Menu";

const position = { x: 0, y: 0 };

const getTitleAndDescription = (type) => {
  let title = [];

  MenuOptions.forEach((option) => {
    option.list_option.forEach((item) => {
      if (type === item.key) {
        title.push(item.label);
      }
    });
  });

  return { title };
};

const getUpdatedElementsAfterActionNodeAddition = ({
  elements,
  newNodeId,
  targetNodeId,
  onAddNodeCallback,
}) => {
  const clonedElements = _.cloneDeep(elements);
  const newEdge = {
    id: uuidv4(),
    source: newNodeId,
    target: targetNodeId,
    type: "condition",
    data: { onAddNodeCallback },
  };
  clonedElements.push(newEdge);
  return clonedElements;
};

const getUpdatedElementsAfterEndNodeAddition = () => {};

const getUpdatedElementsAfterRuleNodeAdditon = ({
  elements,
  newNodeId,
  targetNodeId,
  onAddNodeCallback,
}) => {
  const clonedElements = _.cloneDeep(elements);
  const emptyNode1Id = uuidv4();
  const emptyNode2Id = uuidv4();
  const endNodeId = uuidv4();

  // Define the end node for 'no' branch
  const endNode = {
    id: endNodeId,
    type: "end",
    data: {
      title: "End",
      description: "Automation ends.",
      stats: {
        completed: 14,
      },
    },
    position, // Node end sẽ ở vị trí của nhánh no
  };

  // Move the existing target node to the 'yes' branch
  const ruleNodeToYesBranchEdge = {
    id: uuidv4(),
    source: newNodeId,
    target: targetNodeId, // Existing target node moves to yes branch
    type: "condition",
    sourceHandle: "yes",
    data: { onAddNodeCallback },
  };

  // Connect the rule node to the empty node in 'no' branch, which ends in the end node
  const ruleNodeToNoBranchEdge = {
    id: uuidv4(),
    source: newNodeId,
    target: endNodeId, // This goes to the 'no' branch
    type: "condition",
    sourceHandle: "no",
    data: { onAddNodeCallback },
  };

  // Add everything to cloned elements
  clonedElements.push(ruleNodeToYesBranchEdge, ruleNodeToNoBranchEdge, endNode);

  return clonedElements;
};

const getUpdatedElementsAfterNodeAddition = ({
  elements,
  targetEdgeId,
  type,
  onDeleteNodeCallback,
  onNodeClickCallback,
  onAddNodeCallback,
  position,
}) => {
  const newNodeId = uuidv4();
  const { title } = getTitleAndDescription(type);
  const newNode = {
    id: newNodeId,
    type,
    data: {
      title,
      onNodeClickCallback,
      onDeleteNodeCallback,
    },

    position,
  };
  const clonedElements = _.cloneDeep(elements);
  const targetEdgeIndex = clonedElements.findIndex(
    (x) => x.id === targetEdgeId
  );

  if (targetEdgeIndex === -1) {
    console.error("Target edge is undefined.");
    return elements;
  }

  const targetEdge = elements[targetEdgeIndex];

  // Check if targetEdge is defined before accessing its properties
  if (targetEdge) {
    const { target: targetNodeId } = targetEdge;
    const updatedTargetEdge = { ...targetEdge, target: newNodeId };
    clonedElements[targetEdgeIndex] = updatedTargetEdge;
    clonedElements.push(newNode);

    console.log(position);

    console.log(updatedTargetEdge);

    switch (type) {
      case "end":
        // return getUpdatedElementsAfterEndNodeAddition();

        return getUpdatedElementsAfterActionNodeAddition({
          elements: clonedElements,
          newNodeId,
          newNode,
          targetNodeId,
          onAddNodeCallback,
        });

      case "waitThenCheck":
        return getUpdatedElementsAfterRuleNodeAdditon({
          elements: clonedElements,
          newNodeId,
          targetNodeId,
          onAddNodeCallback,
        });
      default:
        return getUpdatedElementsAfterActionNodeAddition({
          elements: clonedElements,
          newNodeId,
          newNode,
          targetNodeId,
          onAddNodeCallback,
        });
    }
  } else {
    // Handle the case when targetEdge is undefined
    console.error("Target edge is undefined.");
    return elements; // Return the original elements array
  }
};

// const getUpdatedElementsAfterNodeAddition = ({
//   elements,
//   targetEdgeId,
//   type,
//   onDeleteNodeCallback,
//   onNodeClickCallback,
//   onAddNodeCallback,
// }) => {
//   const newNodeId = uuidv4();
//   const { title, description } = getTitleAndDescription(type);
//   const newNode = {
//     id: newNodeId,
//     type,
//     data: {
//       title,
//       description,
//       onNodeClickCallback,
//       onDeleteNodeCallback,
//     },
//     position,
//   };
//   const clonedElements = _.cloneDeep(elements);
//   const targetEdgeIndex = clonedElements.findIndex(
//     (x) => x.id === targetEdgeId
//   );
//   const targetEdge = elements[targetEdgeIndex];
//   const { target: targetNodeId } = targetEdge;
//   const updatedTargetEdge = { ...targetEdge, target: newNodeId };
//   clonedElements[targetEdgeIndex] = updatedTargetEdge;
//   clonedElements.push(newNode);

//   switch (type) {
//     case "end":
//       return getUpdatedElementsAfterEndNodeAddition();
//     case "waitThenCheck":
//       return getUpdatedElementsAfterRuleNodeAdditon({
//         elements: clonedElements,
//         newNodeId,
//         targetNodeId,
//         onAddNodeCallback,
//       });
//     default:
//       return getUpdatedElementsAfterActionNodeAddition({
//         elements: clonedElements,
//         newNodeId,
//         newNode,
//         targetNodeId,
//         onAddNodeCallback,
//       });
//   }
// };

export { getUpdatedElementsAfterNodeAddition };
