import { useState, memo } from 'react';
import './ProcessesViewer.css';
import { isFileBlob, type FileBlob } from '../sdk/models/Data';
import MessageBus from '../sdk/MessageBus';
import { iframeUrl } from '../App';

interface BlockState {
  commited_in: string;
  state_id: string;
  pcd_commitment: Record<string, string>;
  public_data: Record<string, any>;
  // Autres propriétés disponibles si nécessaires
}

interface Block {
  states: BlockState[];
}

interface Processes {
  [key: string]: Block;
}

interface ProcessesViewerProps {
  processes: Processes | null;
  myProcesses: string[];
  onProcessesUpdate?: (processes: Processes) => void;
}

const compareStates = (
  currentState: BlockState,
  index: number,
  previousState?: BlockState,
  currentPrivateData?: Record<string, any>,
  previousPrivateData?: Record<string, any>
) => {
  const result: Record<string, { 
    value: any, 
    status: 'unchanged' | 'modified',
    hash?: string,
    isPrivate: boolean,
    stateId: string
  }> = {};

  // Ajouter toutes les données publiques de l'état actuel
  Object.keys(currentState.public_data).forEach(key => {
    const currentValue = currentState.public_data[key];
    const previousValue = previousState?.public_data[key];
    const isModified = index > 0 && 
      previousValue !== undefined && 
      JSON.stringify(currentValue) !== JSON.stringify(previousValue);

    result[key] = {
      value: currentValue,
      status: isModified ? 'modified' : 'unchanged',
      hash: currentState.pcd_commitment[key],
      isPrivate: false,
      stateId: currentState.state_id
    };
  });

  // Gérer les données privées
  if (index === 0) {
    // Pour le premier état, on ajoute simplement les données privées actuelles
    if (currentPrivateData) {
      Object.entries(currentPrivateData).forEach(([key, value]) => {
        result[key] = {
          value,
          status: 'unchanged',
          hash: currentState.pcd_commitment[key],
          isPrivate: true,
          stateId: currentState.state_id
        };
      });
    }
  } else if (previousPrivateData) {
    // Pour les états suivants, on commence par les données privées de l'état précédent
    Object.entries(previousPrivateData).forEach(([key, value]) => {
      result[key] = {
        value,
        status: 'unchanged',
        hash: previousState?.pcd_commitment[key],
        isPrivate: true,
        stateId: previousState!.state_id
      };
    });

    // Puis on met à jour les données privées qui ont changé
    if (currentPrivateData) {
      Object.entries(currentPrivateData).forEach(([key, value]) => {
        result[key] = {
          value,
          status: 'modified',
          hash: currentState.pcd_commitment[key],
          isPrivate: true,
          stateId: currentState.state_id
        };
      });
    }
  }

  return result;
};

function ProcessesViewer({ processes, myProcesses, onProcessesUpdate }: ProcessesViewerProps) {
  const [expandedBlocks, setExpandedBlocks] = useState<string[]>([]);
  const [isFiltered, setIsFiltered] = useState<boolean>(false);
  const [privateData, setPrivateData] = useState<Record<string, Record<string, any>>>({});
  const [editingField, setEditingField] = useState<{
    processId: string;
    stateId: string;
    key: string;
    value: any;
  } | null>(null);
  const [tempValue, setTempValue] = useState<any>(null);

  const handleFilterClick = () => {
    setIsFiltered(prev => !prev);
  };

  // Si pas de données, afficher un message
  if (!processes || Object.keys(processes).length === 0) {
    return (
      <div className="processes-viewer-empty">
        <h3>Aucun processus disponible</h3>
        <p>Connectez-vous</p>
      </div>
    );
  }

  const toggleBlock = (blockId: string) => {
    setExpandedBlocks(prev =>
      prev.includes(blockId)
        ? prev.filter(id => id !== blockId)
        : [...prev, blockId]
    );
  };

  const fetchPrivateData = async (processId: string, stateId: string) => {
    if (!expandedBlocks.includes(processId) || !myProcesses.includes(processId)) {
      return;
    }
    try {
      const messageBus = MessageBus.getInstance(iframeUrl);
      await messageBus.isReady();

      const data = await messageBus.getData(processId, stateId);

      setPrivateData(prev => ({
        ...prev,
        [stateId]: data
      }));
    } catch (error) {
      console.error('Error fetching private data:', error);
    }
  };

  const handleDownload = (name: string | undefined, fileBlob: FileBlob) => {
    const blob = new Blob([fileBlob.data], { type: fileBlob.type });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = name || 'download';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    URL.revokeObjectURL(url);
  };

  const formatValue = (key: string, value: string | number[] | FileBlob) => {
    if (isFileBlob(value)) {
      return (
        <button 
          className="download-button"
          onClick={() => handleDownload(key, value)}
          title="Télécharger le fichier"
        >
          📥 Télécharger
        </button>
      );
    }
    return JSON.stringify(value || '');
  };

  const getDataIcon = (value: any) => {
    if (isFileBlob(value)) return '📄';
    if (typeof value === 'string') return '📝';
    if (typeof value === 'number') return '🔢';
    if (Array.isArray(value)) return '📋';
    if (typeof value === 'boolean') return '✅';
    return '📦'; // object
  };

  const handleFieldUpdate = async (processId: string, stateId: string, key: string, value: any) => {
    try {
      const messageBus = MessageBus.getInstance(iframeUrl);
      await messageBus.isReady();

      const updateData = {
        [key]: value
      };

      // First update the process
      const updatedProcess = await messageBus.updateProcess(processId, stateId, updateData, [], null);
      if (!updatedProcess) {
        throw new Error('No updated process found');
      }

      const newStateId = updatedProcess.diffs[0]?.state_id;

      if (!newStateId) {
        throw new Error('No new state id found');
      }
      
      // Then notify about the update
      await messageBus.notifyProcessUpdate(processId, newStateId);
      
      // Finally validate the state
      await messageBus.validateState(processId, newStateId);
      
      // Refresh the processes data
      const updatedProcesses = await messageBus.getProcesses();
      if (onProcessesUpdate) {
        onProcessesUpdate(updatedProcesses);
      }
      console.log('Process updated successfully');
    } catch (error) {
      console.error('Error updating field:', error);
      // You might want to show an error message to the user here
    }
  };

  const renderEditForm = (key: string, value: any, onSave: (newValue: any) => void, onCancel: () => void) => {
    // Initialize tempValue when editing starts
    if (tempValue === null) {
      setTempValue(value);
    }

    const handleFormClick = (e: React.MouseEvent) => {
      e.stopPropagation();
      e.preventDefault();
    };

    if (isFileBlob(value)) {
      return (
        <div className="edit-form">
          <input
            type="file"
            onChange={(e) => {
              e.stopPropagation();
              const file = e.target.files?.[0];
              if (file) {
                const reader = new FileReader();
                reader.onload = (event) => {
                  if (event.target?.result) {
                    const arrayBuffer = event.target.result as ArrayBuffer;
                    const uint8Array = new Uint8Array(arrayBuffer);
                    setTempValue({
                      type: file.type,
                      data: uint8Array
                    });
                  }
                };
                reader.readAsArrayBuffer(file);
              }
            }}
          />
          <div className="edit-form-actions">
            <button onClick={(e) => {
              e.stopPropagation();
              e.preventDefault();
              onSave(tempValue);
              setTempValue(null);
            }}>Sauvegarder</button>
            <button onClick={(e) => {
              e.stopPropagation();
              e.preventDefault();
              onCancel();
              setTempValue(null);
            }}>Annuler</button>
          </div>
        </div>
      );
    }

    if (typeof value === 'boolean') {
      return (
        <div className="edit-form" onClick={handleFormClick} onMouseDown={handleFormClick}>
          <select
            value={tempValue.toString()}
            onChange={(e) => {
              e.stopPropagation();
              setTempValue(e.target.value === 'true');
            }}
          >
            <option value="true">Vrai</option>
            <option value="false">Faux</option>
          </select>
          <div className="edit-form-actions">
            <button onClick={(e) => {
              e.stopPropagation();
              e.preventDefault();
              onSave(tempValue);
            }}>Sauvegarder</button>
            <button onClick={(e) => {
              e.stopPropagation();
              e.preventDefault();
              onCancel();
            }}>Annuler</button>
          </div>
        </div>
      );
    }

    if (Array.isArray(value)) {
      return (
        <div className="edit-form" onClick={handleFormClick} onMouseDown={handleFormClick}>
          <textarea
            value={JSON.stringify(tempValue, null, 2)}
            onChange={(e) => {
              e.stopPropagation();
              try {
                const newValue = JSON.parse(e.target.value);
                if (Array.isArray(newValue)) {
                  setTempValue(newValue);
                }
              } catch (error) {
                // Invalid JSON, ignore
              }
            }}
            onClick={handleFormClick}
            onMouseDown={handleFormClick}
            rows={4}
          />
          <div className="edit-form-actions">
            <button onClick={(e) => {
              e.stopPropagation();
              e.preventDefault();
              onSave(tempValue);
            }}>Sauvegarder</button>
            <button onClick={(e) => {
              e.stopPropagation();
              e.preventDefault();
              onCancel();
            }}>Annuler</button>
          </div>
        </div>
      );
    }

    return (
      <div className="edit-form" onClick={handleFormClick} onMouseDown={handleFormClick}>
        <input
          type={typeof value === 'number' ? 'number' : 'text'}
          value={tempValue}
          onChange={(e) => {
            e.stopPropagation();
            const newValue = typeof value === 'number' 
              ? parseFloat(e.target.value)
              : e.target.value;
            setTempValue(newValue);
          }}
          onClick={handleFormClick}
          onMouseDown={handleFormClick}
          autoFocus
        />
        <div className="edit-form-actions">
          <button onClick={(e) => {
            e.stopPropagation();
            e.preventDefault();
            onSave(tempValue);
          }}>Sauvegarder</button>
          <button onClick={(e) => {
            e.stopPropagation();
            e.preventDefault();
            onCancel();
          }}>Annuler</button>
        </div>
      </div>
    );
  };

  const renderDataField = (
    key: string,
    value: any,
    hash: string | undefined,
    isPrivate: boolean,
    processId: string,
    stateId: string,
    status: 'unchanged' | 'modified' = 'unchanged',
    originStateId?: string
  ) => {
    const isEditing = editingField?.key === key && 
                     editingField?.processId === processId && 
                     editingField?.stateId === stateId;

    const getStatusColor = (status: string) => {
      return status === 'modified' ? '#32a852' : 'transparent';
    };

    return (
      <div 
        className="data-field" 
        key={key}
        style={{ 
          backgroundColor: getStatusColor(status),
          transition: 'background-color 0.3s'
        }}
      >
        <div className="data-field-header">
          <span className="data-type-icon" title={isPrivate ? 'Donnée privée' : 'Donnée publique'}>
            {isPrivate ? '🔒' : '🌐'}
          </span>
          <span className="data-value-icon">{getDataIcon(value)}</span>
          <span className="data-label">{key}</span>
          {originStateId && originStateId !== stateId && (
            <span className="data-origin" title={`Propagé depuis l'état ${originStateId}`}>
              ↺
            </span>
          )}
          <button
            className="field-update-button"
            onClick={(e) => {
              e.stopPropagation();
              e.preventDefault();
              setEditingField({ processId, stateId, key, value });
            }}
            title="Mettre à jour cette valeur"
          >
            {isEditing ? '✕' : '🔄'}
          </button>
        </div>
        <div 
          className="data-field-content"
        >
          {isEditing ? (
            <div>
              {renderEditForm(
                key,
                value,
                async (newValue) => {
                  await handleFieldUpdate(processId, stateId, key, newValue);
                  setEditingField(null);
                  setTempValue(null);
                },
                () => {
                  setEditingField(null);
                  setTempValue(null);
                }
              )}
            </div>
          ) : (
            <>
              {formatValue(key, value)}
              {hash && (
                <div className="hash-tooltip" title={`Hash: ${hash}`}>
                  🔑
                </div>
              )}
            </>
          )}
        </div>
      </div>
    );
  };

  return (
    <div className="processes-viewer">
      <h2>Processus</h2>
      <button onClick={handleFilterClick}>
        {isFiltered ? 'Show All Processes' : 'Filter Processes'}
      </button>
      <p className="block-count">
        {isFiltered
          ? Object.keys(processes).filter(processId => myProcesses.includes(processId)).length
          : Object.keys(processes).length} processus disponible(s)
      </p>

      <div className="block-list">
        {Object.entries(processes).map(([processId, process]) => {
          if (isFiltered && !myProcesses.includes(processId)) {
            return null;
          }
          const isExpanded = expandedBlocks.includes(processId);
          const stateCount = process.states.length - 1; // We just ignore the last state, which is always empty
          // Le premier état est le plus récent

          return (
            <div key={processId} className="block-item">
              <div
                className={`block-header ${isExpanded ? 'expanded' : ''}`}
                onClick={() => toggleBlock(processId)}
              >
                <div className="block-id">{processId.substring(0, 8)}...{processId.substring(processId.length - 4)}</div>
                <div className="block-state-count">{stateCount} état(s)</div>
                <div className="block-toggle">{isExpanded ? '▼' : '▶'}</div>
              </div>

              {isExpanded && (
                <div className="block-details">
                  <div className="block-complete-id">
                    <strong>Process ID:</strong> {processId}
                  </div>

                  {process.states.map((state, index) => {
                    if (index === stateCount) return null;
                    
                    // Fetch private data if needed
                    if (myProcesses.includes(processId) && !privateData[state.state_id]) {
                      console.log('Fetching private data for state:', state.state_id);
                      setTimeout(() => {
                        fetchPrivateData(processId, state.state_id);
                      }, 0);
                    }

                    const statePrivateData = privateData[state.state_id] || {};
                    
                    // On utilise compareStates avec l'état précédent et les données privées
                    const stateData = compareStates(
                      state,
                      index,
                      index > 0 ? process.states[index - 1] : undefined,
                      statePrivateData,
                      index > 0 ? privateData[process.states[index - 1].state_id] : undefined
                    );

                    return (
                      <div key={`${processId}-state-${index}`} className="state-item">
                        <h4>État {index + 1}</h4>
                        <div className="state-detail">
                          <strong>TransactionId:</strong> {state.commited_in}
                        </div>
                        <div className="state-detail">
                          <strong>Empreinte totale de l'état:</strong> {state.state_id}
                        </div>

                        <div className="data-fields-container">
                          {/* Toutes les données (publiques et privées) */}
                          {Object.entries(stateData).map(([key, { value, status, hash, isPrivate, stateId }]) => 
                            renderDataField(key, value, hash, isPrivate, processId, stateId, status, state.state_id)
                          )}

                          {/* Message de chargement pour les données privées si nécessaire */}
                          {myProcesses.includes(processId) && Object.keys(statePrivateData).length === 0 && (
                            <div className="loading-message">Chargement des données privées...</div>
                          )}
                          
                          {/* Message si pas d'accès aux données privées */}
                          {!myProcesses.includes(processId) && (
                            <div className="no-access-message">
                              🔒 Vous n'avez pas accès aux données privées de ce processus
                            </div>
                          )}
                        </div>
                      </div>
                    );
                  })}
                </div>
              )}
            </div>
          );
        })}
      </div>
    </div>
  );
}

ProcessesViewer.displayName = 'ProcessesViewer';
export default memo(ProcessesViewer);
