import { Column } from 'element-plus'
import { CommonData, CommonValue, MaybeRef, WarningNode } from '~/common'
import { RowClassGetter } from '../type'

type CellClassGetter = (params: {
  column: Column<CommonData>
  columns: Column<CommonData>[]
  columnIndex: number
  cellData: CommonValue
  rowData: CommonData
  rowIndex: number
}) => string

// function getTime(time: string | null) {
//   const timeString = time?.replace('T', ' ')
//   const datetime = timeString ? new Date(timeString).getTime() : Date.now()
//   return datetime
// }

// function getDuration(
//   name: string,
//   takeOffStand: CommonValue
// ): { warning: number | null; alarm: number | null } {
//   const subMap =
//     HANDOVER_WARNING_MAP[
//       name.includes('International') ? 'internationalDeparture' : 'departure'
//     ]
//   const stand = parseInt(String(takeOffStand))
//   const matchedKey = Object.keys(subMap).find(key => {
//     if (key.includes('-')) {
//       const [start, end] = key.split('-')
//       const min = Number(start)
//       const max = Number(end)
//       if (!Number.isNaN(min) && !Number.isNaN(max)) {
//         return stand >= min && stand <= max
//       }
//     }
//     return key === takeOffStand
//   })
//   let { warning, alarm } = subMap[matchedKey ?? 'all'] ?? {}
//   if (typeof warning === 'number') {
//     warning *= 60 * 1000
//   } else {
//     warning = null
//   }
//   if (typeof alarm === 'number') {
//     alarm *= 60 * 1000
//   } else {
//     alarm = null
//   }
//   return {
//     warning,
//     alarm,
//   }
// }

export function useTableStyle(
  tableName: string,
  warningNodesMap?: {
    [rowKey: string]: { warningNodes: WarningNode[]; alarmNodes: WarningNode[] }
  }
) {
  const rowClass = ({ row, rowIndex }) => {
    const classes: string[] = []
    if (tableName?.includes('Airport')) {
      if (row.hasTakenOff === 'Y' || row.hasLanded === 'Y') {
        classes.push('bg-gray')
      } else if (row.loadPlaneSureTime) {
        classes.push('bg-light')
      }
    }
    return classes.join(' ')
  }

  const cellClass = ({ row, column, rowIndex, columnIndex }) => {
    const classes: string[] = []

    if (tableName?.includes('Airport')) {
      if (column.property === 'flightNO') {
        classes.push('cell-click')
        if (row.flightState === 'DLY') {
          classes.push('flight-state-delay')
        } else if (row.flightState === 'CAN') {
          classes.push('flight-state-cancel')
        }
      }

      if (!tableName?.includes('Transfer')) {
        if (warningNodesMap && warningNodesMap[row.rowKey]) {
          const { warningNodes, alarmNodes } = warningNodesMap[row.rowKey]
          const hasWarningNode = warningNodes.some(
            (node) => node.node_code === column.property
          )
          const hasAlarmNode = alarmNodes.some(
            (node) => node.node_code === column.property
          )
          if (hasWarningNode) {
            classes.push('cell-warning')
          }
          if (hasAlarmNode) {
            classes.push('cell-alarm')
          }
        }
      }
    }

    if (tableName?.includes('FlightContainer')) {
      if (['stowageNo'].includes(column.property)) {
        classes.push('cell-click')
      }
      if (
        [
          'tally',
          'pull',
          'wait',
          'stowage',
          'depot',
          'resure',
          'planeDown',
          'loadPlane',
          'pullSure',
          'dlv',
          'jxjj',
          'hzjj',
          'unload',
          'anjian',
        ].includes(column.property)
      ) {
        classes.push('align-left')
      }
    }

    if (tableName?.includes('FlightWaybill')) {
      if (
        [
          'stockCode',
          // 'stowageNum'
        ].includes(column.property)
      ) {
        classes.push('cell-click')
      }
    }

    return classes.join(' ')
  }

  const rowClassV2: RowClassGetter = ({ columns, rowData, rowIndex }) => {
    const classes: string[] = []
    return classes.join(' ')
  }

  const cellClassV2: CellClassGetter = ({
    column,
    columns,
    columnIndex,
    cellData,
    rowData,
    rowIndex,
  }) => {
    const classes: string[] = []
    return classes.join(' ')
  }

  return {
    rowClass,
    cellClass,
    rowClassV2,
    cellClassV2,
  }
}
