"use client"; // Falls notwendig
import React, { useState, useEffect } from "react";
import { Icon } from "@iconify/react";
import DigitalOutputs from "../components/main/einausgaenge/DigitalOutputs";
import DigitalInputs from "../components/main/einausgaenge/DigitalInputs";
import { useLoadScript } from "../hooks/useLoadScript";

function EinAusgaenge() {
  // Digitale Ausgänge (Hooks müssen immer initialisiert werden)
  const [digitalOutputs, setDigitalOutputs] = useState([
    { id: 1, description: "Ausgang1", toggle: true },
    { id: 2, description: "Ausgang2", toggle: false },
    { id: 3, description: "Ausgang3", toggle: true },
    { id: 4, description: "Ausgang4", toggle: false },
  ]);

  // Modal-Zustände
  const [selectedInput, setSelectedInput] = useState(null);
  const [selectedOutput, setSelectedOutput] = useState(null);
  const [isInputModalOpen, setIsInputModalOpen] = useState(false);
  const [isOutputModalOpen, setIsOutputModalOpen] = useState(false);

  const { mockData, isLoading } = useLoadScript();

  const toggleSwitch = (id) => {
    setDigitalOutputs((prevOutputs) =>
      prevOutputs.map((output) =>
        output.id === id ? { ...output, toggle: !output.toggle } : output
      )
    );
  };

  const openInputModal = (input) => {
    setSelectedInput(input);
    setIsInputModalOpen(true);
  };

  const closeInputModal = () => {
    setSelectedInput(null);
    setIsInputModalOpen(false);
  };

  const openOutputModal = (output) => {
    setSelectedOutput(output);
    setIsOutputModalOpen(true);
  };

  const closeOutputModal = () => {
    setSelectedOutput(null);
    setIsOutputModalOpen(false);
  };

  const digitalInputs = mockData.win_de.map((status, index) => ({
    id: index + 1,
    status: status === 1 ? "active" : "inactive",
    description: `DE${index + 1}`,
    isInverted: false,
  }));

  const inputsGroup1 = digitalInputs.slice(0, 16);
  const inputsGroup2 = digitalInputs.slice(16);

  return (
    <div className="p-4">
      <h1 className="text-lg font-semibold mb-4">Ein- und Ausgänge</h1>
      <div className="flex gap-4">
        {/* Digitale Eingänge */}
        <DigitalInputs
          inputsGroup1={inputsGroup1}
          inputsGroup2={inputsGroup2}
          openInputModal={openInputModal}
        />

        {/* Digitale Ausgänge */}
        <DigitalOutputs
          digitalOutputs={digitalOutputs}
          openOutputModal={openOutputModal}
          toggleSwitch={toggleSwitch}
        />
      </div>

      {/* Modal für Eingänge */}
      {isInputModalOpen && (
        <div className="fixed top-0 left-0 w-full h-full bg-black bg-opacity-50 flex justify-center items-center z-50">
          <div className="bg-white rounded-lg shadow-lg p-6 w-1/3">
            <h2 className="text-lg font-bold mb-4">
              Details für Eingang {selectedInput.id}
            </h2>
            <p>
              <strong>Status:</strong>{" "}
              {selectedInput.status === "active" ? "Aktiv" : "Inaktiv"}
            </p>
            <p>
              <strong>Beschreibung:</strong> {selectedInput.description}
            </p>
            <button
              onClick={closeInputModal}
              className="mt-4 px-4 py-2 bg-blue-500 text-white rounded-lg"
            >
              Schließen
            </button>
          </div>
        </div>
      )}

      {/* Modal für Ausgänge */}
      {isOutputModalOpen && (
        <div className="fixed top-0 left-0 w-full h-full bg-black bg-opacity-50 flex justify-center items-center z-50">
          <div className="bg-white rounded-lg shadow-lg p-6 w-1/3">
            <h2 className="text-lg font-bold mb-4">
              Details für Ausgang {selectedOutput.id}
            </h2>
            <p>
              <strong>Bezeichnung:</strong> {selectedOutput.description}
            </p>
            <p>
              <strong>Status:</strong>{" "}
              {selectedOutput.toggle ? "Eingeschaltet" : "Ausgeschaltet"}
            </p>
            <button
              onClick={closeOutputModal}
              className="mt-4 px-4 py-2 bg-blue-500 text-white rounded-lg"
            >
              Schließen
            </button>
          </div>
        </div>
      )}
    </div>
  );
}

export default EinAusgaenge;
