import { Column } from 'element-plus'
import { CommonData, CommonValue } 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
}

export function useTableStyle(tableName?: string) {
  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[] = []

    const cellData = row[column.property]

    if (tableName?.includes('Airport')) {
      if ((['flightNO'] as CommonValue[]).includes(column.property)) {
        classes.push('cell-click')
        if (row.flightState === 'DLY') {
          classes.push('flight-state-delay')
        } else if (row.flightState === 'CAN') {
          classes.push('flight-state-cancel')
        }
      }
      if (
        (column.property === 'register' &&
          typeof cellData === 'string' &&
          cellData.split('/').some(char => Number(char) > 0)) ||
        ((['pullregisterTime', 'pullRegisterTime'] as CommonValue[]).includes(
          column.property
        ) &&
          typeof cellData === 'string')
      ) {
        classes.push('cell-warning')
      }
      if (
        (['resure', 'resureTime'] as CommonValue[]).includes(column.property) &&
        (row['planeDownTime'] ?? '') !== ''
      ) {
        const { stowage, resure } = row
        let stowageNum = 0,
          resureNum = 0
        if (typeof stowage === 'string') {
          stowageNum = Number(stowage.split('/')[0]) || 0
        }
        if (typeof resure === 'string') {
          resureNum = Number(resure.split('/')[0]) || 0
        }
        if (stowageNum !== resureNum) {
          classes.push('cell-warning')
        }
      }
      // if (column.property === 'loadPlaneTime') {
      //   if (row['warningState'] === 'alarm') {
      //     classes.push('cell-alarm')
      //   }
      //   if (row['warningState'] === 'warning') {
      //     classes.push('cell-warning')
      //   }
      // }
      if (['securityYes', 'securityTime'].includes(column.property)) {
        const { receiveSure, receiveSure1, securityYes } = row
        let receiveNum = 0,
          securityNum = 0
        if (tableName.includes('International')) {
          if (typeof receiveSure1 === 'string') {
            receiveNum = Number(receiveSure1.split('/')[0]) || 0
          }
        } else {
          if (typeof receiveSure === 'string') {
            receiveNum = Number(receiveSure.split('/')[0]) || 0
          }
        }
        if (typeof securityYes === 'string') {
          securityNum = Number(securityYes.split('/')[0]) || 0
        }
        if (receiveNum !== securityNum) {
          classes.push('cell-warning')
        }
      }

      if (column.property === 'loadPlaneSureTime') {
        const { planDepartureTime, loadPlaneSureTime } = row
        if (
          getTime(planDepartureTime) - getTime(loadPlaneSureTime) <
          5 * 60 * 1000
        ) {
          classes.push('cell-warning')
        }
      }
      if (column.property === 'loadPlaneTime') {
        const { planDepartureTime, loadPlaneTime } = row
        if (
          getTime(planDepartureTime) - getTime(loadPlaneTime) <
          2 * 60 * 1000
        ) {
          classes.push('cell-warning')
        }
      }
    }

    if (tableName?.includes('FlightContainer')) {
      if (['stowageNo'].includes(column.property)) {
        classes.push('cell-click')
      }
    }

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

    // if (tableName?.includes('WaybillGoods')) {
    //   if (['CargoSN'].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,
  }
}
