import React, { useRef, useState } from "react";
import {
  AddIcon,
  ArrowIcon,
  RemoveIcon,
  ResizeHeightIcon,
} from "../../../icons/ExportCommonIcons";
import RenameInput from "../../../ui/inputs/RenameInput";
import { handleResize } from "../../../../functions/handleResizePannel";
import {
  useSelectedAsset,
  useSelectedProduct,
} from "../../../../store/simulation/useSimulationStore";
import { useProductStore } from "../../../../store/simulation/useProductStore";
import { generateUUID } from "three/src/math/MathUtils";
import RenderOverlay from "../../../templates/Overlay";
import EditWidgetOption from "../../../ui/menu/EditWidgetOption";
import { handleAddEventToProduct } from "../../../../modules/simulation/events/points/functions/handleAddEventToProduct";
import { useEventsStore } from "../../../../store/simulation/useEventsStore";
import { deleteEventDataApi } from "../../../../services/simulation/deleteEventDataApi";
import { upsertProductOrEventApi } from "../../../../services/simulation/UpsertProductOrEventApi";
import { deleteProductApi } from "../../../../services/simulation/deleteProductApi";

interface Event {
  pathName: string;
}

interface ListProps {
  val: Event;
}

const List: React.FC<ListProps> = ({ val }) => {
  return (
    <div className="process-container">
      <div className="value">{val.pathName}</div>
    </div>
  );
};

const Simulations: React.FC = () => {
  const productsContainerRef = useRef<HTMLDivElement>(null);
  const {
    products,
    addProduct,
    removeProduct,
    renameProduct,
    addEvent,
    removeEvent,
  } = useProductStore();
  const { selectedProduct, setSelectedProduct } = useSelectedProduct();
  const { getEventByModelUuid } = useEventsStore();
  const { selectedAsset, clearSelectedAsset } = useSelectedAsset();
  const email = localStorage.getItem("email");
  const organization = email!.split("@")[1].split(".")[0];
  const [openObjects, setOpenObjects] = useState(true);

  const handleAddProduct = () => {
    const id = generateUUID();
    const name = `Product ${products.length + 1}`;
    addProduct(name, id);
    upsertProductOrEventApi({
      productName: name,
      productId: id,
      organization: organization,
    });
  };

  const handleRemoveProduct = (productId: string) => {
    const currentIndex = products.findIndex((p) => p.productId === productId);
    const isSelected = selectedProduct.productId === productId;

    const updatedProducts = products.filter((p) => p.productId !== productId);

    if (isSelected) {
      if (updatedProducts.length > 0) {
        let newSelectedIndex = currentIndex;
        if (currentIndex >= updatedProducts.length) {
          newSelectedIndex = updatedProducts.length - 1;
        }
        setSelectedProduct(
          updatedProducts[newSelectedIndex].productId,
          updatedProducts[newSelectedIndex].productName
        );
      } else {
        setSelectedProduct("", "");
      }
    }

    removeProduct(productId);
    deleteProductApi(productId, organization);
  };

  const handleRenameProduct = (productId: string, newName: string) => {
    renameProduct(productId, newName);
    if (selectedProduct.productId === productId) {
      setSelectedProduct(productId, newName);
    }
  };

  const handleRemoveEventFromProduct = () => {
    if (selectedAsset) {
      const email = localStorage.getItem("email");
      const organization = email!.split("@")[1].split(".")[0];
      deleteEventDataApi({
        productId: selectedProduct.productId,
        modelUuid: selectedAsset.modelUuid,
        organization: organization,
      });
      removeEvent(selectedProduct.productId, selectedAsset.modelUuid);
      clearSelectedAsset();
    }
  };

  const selectedProductData = products.find(
    (product) => product.productId === selectedProduct.productId
  );

  const events: Event[] =
    selectedProductData?.eventDatas.map((event) => ({
      pathName: event.modelName,
    })) || [];

  return (
    <div className="simulations-container">
      <div className="header">Simulations</div>
      <div className="add-product-container">
        <div className="actions section">
          <div className="header">
            <div className="header-value">Products</div>
            <button className="add-button" onClick={handleAddProduct}>
              <AddIcon /> Add
            </button>
          </div>
          <div
            className="lists-main-container"
            ref={productsContainerRef}
            style={{ height: "120px" }}
          >
            <div className="list-container">
              {products.map((product, index) => (
                <div
                  key={product.productId}
                  className={`list-item ${
                    selectedProduct.productId === product.productId
                      ? "active"
                      : ""
                  }`}
                >
                  {/* eslint-disable-next-line */}
                  <div
                    className="value"
                    onClick={() =>
                      setSelectedProduct(product.productId, product.productName)
                    }
                  >
                    <input
                      type="radio"
                      name="products"
                      checked={selectedProduct.productId === product.productId}
                      readOnly
                    />
                    <RenameInput
                      value={product.productName}
                      onRename={(newName) =>
                        handleRenameProduct(product.productId, newName)
                      }
                    />
                  </div>
                  {products.length > 1 && (
                    <button
                      className="remove-button"
                      onClick={() => handleRemoveProduct(product.productId)}
                    >
                      <RemoveIcon />
                    </button>
                  )}
                </div>
              ))}
            </div>
            <button
              className="resize-icon"
              id="action-resize"
              onMouseDown={(e: any) => handleResize(e, productsContainerRef)}
            >
              <ResizeHeightIcon />
            </button>
          </div>
        </div>

        <div className="simulation-process section">
          <button
            className="collapse-header-container"
            onClick={() => setOpenObjects(!openObjects)}
          >
            <div className="header">Events</div>
            <div className="arrow-container">
              <ArrowIcon />
            </div>
          </button>
          {openObjects &&
            events.map((event, index) => (
              <List key={`${index}-${event.pathName}`} val={event} />
            ))}
        </div>

        <div className="compare-simulations-container">
          <div className="compare-simulations-header">
            Need to Compare Layout?
          </div>
          <div className="content">
            Click '<span>Compare</span>' to review and analyze the layout
            differences between them.
          </div>
          <div className="input">
            <input type="button" value={"Compare"} className="submit" />
          </div>
        </div>
      </div>

      {selectedAsset && (
        <RenderOverlay>
          <EditWidgetOption
            options={["Add to Product", "Remove from Product"]}
            onClick={(option) => {
              if (option === "Add to Product") {
                handleAddEventToProduct({
                  event: getEventByModelUuid(selectedAsset.modelUuid),
                  addEvent,
                  selectedProduct,
                  clearSelectedAsset,
                });
              } else {
                handleRemoveEventFromProduct();
              }
            }}
          />
        </RenderOverlay>
      )}
    </div>
  );
};

export default Simulations;
