import React, { useRef } from "react";
import {
  AddIcon,
  RemoveIcon,
  ResizeHeightIcon,
} from "../../../../../icons/ExportCommonIcons";
import RenameInput from "../../../../../ui/inputs/RenameInput";
import { handleResize } from "../../../../../../functions/handleResizePannel";
import {
  useSelectedAction,
} from "../../../../../../store/simulation/useSimulationStore";
import { upsertProductOrEventApi } from "../../../../../../services/simulation/products/UpsertProductOrEventApi";
import { useProductContext } from "../../../../../../modules/simulation/products/productContext";
import { useParams } from "react-router-dom";
import { useVersionContext } from "../../../../../../modules/builder/version/versionContext";
import { useSceneContext } from "../../../../../../modules/scene/sceneContext";

interface ActionsListProps {
  selectedPointData: any;
  multipleAction?: boolean;
  handleAddAction?: () => void;
  handleDeleteAction?: (actionUuid: string) => void;
}

const ActionsList: React.FC<ActionsListProps> = ({
  selectedPointData,
  multipleAction = false,
  handleAddAction,
  handleDeleteAction,
}) => {
  const actionsContainerRef = useRef<HTMLDivElement>(null);

  // store

  const { productStore } = useSceneContext();
  const { renameAction } = productStore();
  const { selectedProductStore } = useProductContext();
  const { selectedProduct } = selectedProductStore();
  const { selectedAction, setSelectedAction } = useSelectedAction();
  const { selectedVersionStore } = useVersionContext();
  const { selectedVersion } = selectedVersionStore();
  const { projectId } = useParams();

  const handleRenameAction = (newName: string) => {
    if (!selectedAction.actionId) return;
    const event = renameAction(
      selectedProduct.productUuid,
      selectedAction.actionId,
      newName
    );
    setSelectedAction(selectedAction.actionId, newName);
    if (event) {
      upsertProductOrEventApi({
        productName: selectedProduct.productName,
        productUuid: selectedProduct.productUuid,
        projectId,
        eventDatas: event,
        versionId: selectedVersion?.versionId || '',
      });
    }
  };

  const handleActionSelect = (actionUuid: string, actionName: string) => {
    setSelectedAction(actionUuid, actionName);
  };

  return (
    <div className="actions-list-container">
      <div className="actions">
        <div className="header">
          <div className="header-value">Actions</div>

          <button
            id="add-action-button"
            className="add-button"
            onClick={() => {
              if (handleAddAction) {
                handleAddAction();
              }
            }}
            disabled={!multipleAction}
          >
            <AddIcon /> Add
          </button>
        </div>
        <div
          className="lists-main-container"
          ref={actionsContainerRef}
          style={{ height: "120px" }}
        >
          <div className="list-container">
            {multipleAction &&
              selectedPointData?.actions?.map((action: any) => (
                <div
                  key={action.actionUuid}
                  className={`list-item ${selectedAction.actionId === action.actionUuid
                    ? "active"
                    : ""
                    }`}
                >
                  <button
                    id="action-button"
                    className="value"
                    onClick={() =>
                      handleActionSelect(action.actionUuid, action.actionName)
                    }
                  >
                    <RenameInput
                      value={action.actionName}
                      onRename={(value) => handleRenameAction(value)}
                    />
                  </button>
                  {selectedPointData?.actions?.length > 1 && (
                    <button
                      id="remove-action-button"
                      className="remove-button"
                      onClick={() => {
                        if (handleDeleteAction) {
                          handleDeleteAction(action.actionUuid);
                        }
                      }}
                    >
                      <RemoveIcon />
                    </button>
                  )}
                </div>
              ))}
            {!multipleAction && selectedPointData?.action && (
              <div
                key={selectedPointData.action.actionUuid}
                className={`list-item active`}
              >
                <button
                  id="action-button"
                  className="value"
                  onClick={() =>
                    handleActionSelect(
                      selectedPointData.action.actionUuid,
                      selectedPointData.action.actionName
                    )
                  }
                >
                  <RenameInput
                    value={selectedPointData.action.actionName}
                    onRename={handleRenameAction}
                  />
                </button>
              </div>
            )}
          </div>
          {multipleAction && (
            <button
              className="resize-icon"
              id="action-resize"
              onMouseDown={(e: any) => handleResize(e, actionsContainerRef)}
            >
              <ResizeHeightIcon />
            </button>
          )}
        </div>
      </div>
    </div>
  );
};

export default ActionsList;
