import { CellRendererProps } from '../types'
import { findMergedCellInfo, isInAnySelection } from '../utils'
import { Cell } from './Cell'

export const CellRenderer = ({
  columnIndex,
  rowIndex,
  style,
  data,
}: CellRendererProps) => {
  const {
    sheetType,
    widths,
    selectedCell,
    multiSelection,
    activeSheet,
    isEditing,
    editingValue,
    cachedData,
    mergedCells,
    highlightedCells,
    copiedCells,
    handleCellClick,
    handleCellDoubleClick,
    handleCellChange,
    handleCellReferenceClick,
    handleCellMouseDown,
    handleCellMouseEnter,
    handleCellMouseUp,
    isInEditMode,
    stopEditing,
    setActiveSheet,
    activeCellInputRef,
  } = data

  const isActiveSheet = activeSheet === sheetType
  const isCellSelected =
    selectedCell?.row === rowIndex &&
    selectedCell?.col === columnIndex &&
    isActiveSheet

  const isInMultiSelection =
    isActiveSheet && isInAnySelection(rowIndex, columnIndex, multiSelection)

  const multiSelectionActive = isActiveSheet && multiSelection.length > 0
  const isCellEditing = isCellSelected && isEditing

  const cellKey = `${rowIndex}-${columnIndex}`
  const cellData = cachedData[cellKey]

  const rawValue = isCellEditing ? editingValue : cellData?.rawValue || ''
  const displayValue = isCellEditing ? '' : cellData?.displayValue || ''

  // Подсветка ячеек для формул
  const currentSheetName = sheetType === 'report' ? 'L' : 'R'
  const highlightedCell = highlightedCells.find(
    highlight =>
      highlight.sheet === currentSheetName &&
      highlight.row === rowIndex &&
      highlight.col === columnIndex
  )

  // Объединенные ячейки только для листа отчета
  const mergedCellInfo =
    sheetType === 'report' && mergedCells
      ? findMergedCellInfo(rowIndex, columnIndex, mergedCells)
      : { isMerged: false, isMainCell: false }

  const mergedCellProps = mergedCellInfo.isMerged
    ? {
        isMerged: true,
        isMainCell: mergedCellInfo.isMainCell,
        rowSpan: mergedCellInfo.spans?.rowSpan,
        colSpan: mergedCellInfo.spans?.colSpan,
      }
    : undefined

  // Скопированные/вырезанные ячейки
  const isCopied = Boolean(
    copiedCells?.sourceSheet === sheetType &&
      copiedCells.data.some(
        cell => cell.row === rowIndex && cell.col === columnIndex
      )
  )
  const isCut = Boolean(isCopied && copiedCells?.isCut)

  return (
    <Cell
      style={style}
      sheetType={sheetType}
      rowIndex={rowIndex}
      colIndex={columnIndex}
      columnWidth={widths[columnIndex]}
      columnWidths={widths}
      isSelected={isCellSelected}
      isInMultiSelection={isInMultiSelection}
      multiSelectionActive={multiSelectionActive}
      isEditing={isCellEditing}
      displayValue={displayValue}
      rawValue={rawValue}
      isModified={cellData?.isModified || false}
      availablePx={cellData?.availablePx || widths[columnIndex]}
      onCellClick={handleCellClick}
      onCellDoubleClick={handleCellDoubleClick}
      onCellValueChange={handleCellChange}
      onCellReferenceClick={handleCellReferenceClick}
      onCellMouseDown={handleCellMouseDown}
      onCellMouseEnter={handleCellMouseEnter}
      onCellMouseUp={handleCellMouseUp}
      isInEditMode={isEditing}
      stopEditing={stopEditing}
      setActiveSheet={setActiveSheet}
      activeCellRef={activeCellInputRef}
      mergedCellInfo={mergedCellProps}
      highlightColor={highlightedCell?.color}
      isCopied={isCopied}
      isCut={isCut}
    />
  )
}
