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

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')
      }
    }
    // if (tableName?.includes('FlightContainer')) {
    //   if (rowIndex === 1) {
    //     classes.push('row-warning')
    //   }
    // }
    // if (tableName?.includes('FlightWaybill')) {
    //   if (rowIndex === 0) {
    //     classes.push('row-alarm')
    //   }
    //   if (rowIndex === 1) {
    //     classes.push('row-warning')
    //   }
    // }
    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 (
        tableName?.includes('DepartureAirport') &&
        (['depotJoin', 'resure'] as any[]).includes(column.property) &&
        (cellData ?? '') !== ''
      ) {
        if (cellData === row['stowage']) {
          classes.push('cell-success')
        } else if ((row['planeDownTime'] ?? '') !== '') {
          classes.push('cell-alarm')
        }
      }
      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, securityYes } = row
        let receiveNum = 0,
          securityNum = 0
        if (typeof receiveSure === 'string') {
          receiveNum = Number(receiveSure.split('/')[1]) || 0
        }
        if (typeof securityYes === 'string') {
          securityNum = Number(securityYes.split('/')[1]) || 0
        }
        if (receiveNum !== securityNum) {
          classes.push('cell-striking')
        }
      }
    }

    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')
    //   }
    // }

    if (tableName?.includes('ArrivalWaybillGoods')) {
      if (['C1'].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[] = []
    if ((['flightNO'] as any[]).includes(column.dataKey)) {
      classes.push('cell-click')
    }
    if (
      (column.dataKey === 'register' &&
        typeof cellData === 'string' &&
        cellData.split('/').some(char => Number(char) > 0)) ||
      ((['pullregisterTime', 'pullRegisterTime'] as any[]).includes(
        column.dataKey
      ) &&
        typeof cellData === 'string')
    ) {
      classes.push('cell-warning')
    }
    if (
      tableName?.includes('DepartureAirport') &&
      (['depotJoin', 'resure'] as any[]).includes(column.dataKey) &&
      (cellData ?? '') !== ''
    ) {
      if (cellData === rowData['stowage']) {
        classes.push('cell-success')
      } else if ((rowData['planeDownTime'] ?? '') !== '') {
        classes.push('cell-alarm')
      }
    }
    if (column.dataKey === 'loadPlaneTime') {
      if (rowData['warningState'] === 'alarm') {
        classes.push('cell-alarm')
      }
      if (rowData['warningState'] === 'warning') {
        classes.push('cell-warning')
      }
    }
    return classes.join(' ')
  }

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