"use client"; // /components/main/settingsPageComponents/OPCUAInterfaceSettings.tsx
import React, { useState } from "react";
import Image from "next/image";
import { useSelector, useDispatch } from "react-redux";
import { RootState } from "../../../redux/store";
import { toggleOpcUaServer } from "../../../redux/slices/opcuaSettingsSlice";

export default function OPCUAInterfaceSettings() {
  const dispatch = useDispatch();
  const opcuaSettings = useSelector(
    (state: RootState) => state.opcuaSettingsSlice
  );

  // Anzahl der aktuellen OPC-Clients (Mock, bis Backend liefert)
  const opcUaActiveClientCount = opcuaSettings.opcUaActiveClientCount ?? 3; // 3 als Beispielwert

  // Lokale Zustände für das neue Benutzerformular

  const [nodesetName, setNodesetName] = useState(
    opcuaSettings.opcUaNodesetName
  );

  return (
    <div className="p-6 md:p-3 bg-gray-100 max-w-5xl mr-auto ">
      <div className="flex justify-between items-center mb-3">
        <Image
          src="/images/OPCUA.jpg"
          alt="OPCUA Logo"
          width={48}
          height={48}
          className="h-12 w-auto"
        />
        <Image
          src="/images/OPCUA.jpg"
          alt="OPCUA Logo"
          width={48}
          height={48}
          className="h-12 w-auto"
        />
      </div>

      {/* ✅ Server Aktivierung */}
      <div className="mb-3 flex flex-wrap items-center">
        <label className="mr-3 font-medium text-sm">Server Status:</label>
        <button
          onClick={() => dispatch(toggleOpcUaServer())}
          className={`px-3 py-1 rounded text-sm ${
            opcuaSettings.isEnabled ? "bg-littwin-blue" : "bg-gray-300"
          } text-white`}
        >
          {opcuaSettings.isEnabled ? "Aktiviert" : "Deaktiviert"}
        </button>
      </div>

      {/* ✅ Verschlüsselung */}

      {/* 
                 <div className="mb-3">
        <label className="block font-medium text-sm mb-1">
          Verschlüsselung
        </label>
        <select
          value={opcuaSettings.encryption}
          onChange={(e) => dispatch(setOpcUaEncryption(e.target.value))}
          className="w-full p-1 border border-gray-300 rounded-md text-sm"
        >
          <option value="None">Keine</option>
          <option value="Basic256">Basic256</option>
          <option value="Basic256Sha256">Basic256Sha256</option>
        </select>
      </div>
           */}

      {/* ✅ OPCUA Zustand */}
      <div className="mb-3">
        <label className="block font-medium text-sm mb-1">OPCUA Zustand</label>
        <div className="p-1 border border-gray-300 rounded-md bg-white text-sm">
          {opcuaSettings.opcUaZustand}
        </div>
      </div>

      {/* ✅ Nodeset Name */}
      <div className="mb-3">
        <label className="block font-medium text-sm mb-1">Nodeset Name</label>
        <div className="flex">
          <input
            type="text"
            className="flex-grow p-1 border border-gray-300 rounded-l-md text-sm"
            value={nodesetName}
            onChange={(e) => setNodesetName(e.target.value)}
            disabled={opcuaSettings.isEnabled} // Disable input when server is enabled
          />
          {/*
            <button
              onClick={handleNodesetUpdate}
              className="px-3 py-1 bg-littwin-blue text-white rounded-r-md text-sm"
            >
              Übernehmen
            </button>
          */}
        </div>
      </div>

      {/* ✅ Anzahl der aktuellen OPC-Clients */}
      <div className="mb-3">
        <label className="block font-medium text-sm mb-1">
          Aktuelle OPC-Clients
        </label>
        <div className="p-1 border border-gray-300 rounded-md bg-white text-sm">
          {opcUaActiveClientCount}
        </div>
      </div>

      {/* ✅ Benutzerverwaltung */}
      {/* 
       
             <div className="mb-3">
        <h3 className="text-base font-semibold mb-2">Benutzer</h3>
        <ul className="space-y-1">
          {opcuaSettings.users.map((user) => (
            <li
              key={user.id}
              className="p-1 bg-white shadow-sm rounded-md flex justify-between items-center text-sm"
            >
              <span className="font-medium">{user.username}</span>
              <button
                onClick={() => dispatch(removeOpcUaUser(user.id))}
                className="text-red-500"
              >
                Löschen
              </button>
            </li>
          ))}
        </ul>

    
        <div className="mt-2 flex flex-wrap gap-2">
          <input
            type="text"
            placeholder="Benutzername"
            value={newUsername}
            onChange={(e) => setNewUsername(e.target.value)}
            className="p-1 border rounded flex-grow text-sm"
          />
          <input
            type="password"
            placeholder="Passwort"
            value={newPassword}
            onChange={(e) => setNewPassword(e.target.value)}
            className="p-1 border rounded flex-grow text-sm"
          />
          <button
            onClick={handleAddUser}
            className="bg-littwin-blue text-white p-1 rounded text-sm"
          >
            Hinzufügen
          </button>
        </div>
      </div>
       
       */}
    </div>
  );
}
