import type { Recordable } from '@/types/global'
import { ElMessage } from 'element-plus'
const luckysheet = (window as any).luckysheet

export function chatatABC(n: number) {
  var orda = 'a'.charCodeAt(0)

  var ordz = 'z'.charCodeAt(0)

  var len = ordz - orda + 1

  var s = ''

  while (n >= 0) {
    s = String.fromCharCode((n % len) + orda) + s

    n = Math.floor(n / len) - 1
  }

  return s.toUpperCase()
}

export function getRangetxt(row0: number, row1: number, column0: number, column1: number) {
  if (row0 == '' && row1 == '' && column0 == '' && column1 == '') {
    return ''
  }
  if (row0 == null && row1 == null) {
    return chatatABC(column0) + ':' + chatatABC(column1)
  } else if (column0 == null && column1 == null) {
    return row0 + 1 + ':' + (row1 + 1)
  } else {
    if (column0 == column1 && row0 == row1) {
      return chatatABC(column0) + (row0 + 1)
    } else {
      return chatatABC(column0) + (row0 + 1) + ':' + chatatABC(column1) + (row1 + 1)
    }
  }
}

export function getRangeState(range: any) {
  const sheet = luckysheet.getSheet()
  const [beginRow, endRow] = sheet.luckysheet_select_save[0].row
  const [beginColum, endColum] = sheet.luckysheet_select_save[0].column
  let rowLen = range.length
  let columnLen = range[0].length
  let isOneRow = !(rowLen > 1)
  let isOneColumn = !(columnLen > 1)

  return {
    rowLen,
    columnLen,
    beginRow,
    endRow,
    sheetNum: sheet.order,
    beginColum,
    endColum,
    isOneRow,
    isOneColumn
  }
}

export function handleRangeTitle(range: any, dataArea: Recordable) {
  const {
    beginColum,
    beginRow,
    endRow,
    endColum,
    sheetNum,
    rowLen,
    columnLen
  } = getRangeState(range)
  const titles: Recordable[] = []
  let isVertical = false

  // 只有一行，只能横向
  if (rowLen == 1) {
    isVertical = false
  } else if (
    rowLen - 1 === endRow - beginRow &&
    (columnLen - 1 !== endColum - beginColum || columnLen == 1)
  ) {
    isVertical = true
  }

  /**
   * 取最后一个单元格的坐标，值为最后一个单元格，如果当前单元格没有值，继承前一个单元格值
   *
   * 横向：以列为单位取值，多值取最后有值单元格
   *
   * 纵向：以行为单位，多值取最后有值单元格
   */
  for (let c = 0; c < (isVertical ? rowLen : columnLen); c++) {
    const title = {
      title: '',
      sheet: sheetNum,
      row: 0,
      colum: 0
    }
    for (let r = 0; r < (isVertical ? columnLen : rowLen); r++) {
      const cell = isVertical ? range[c][r] : range[r][c]
      let v = ''
      // 如果获取值报错，那么断定单元格值为空，直接取上一个单元格的值
      try {
        v = cell.v || cell?.ct?.s.map((itm) => itm.v).join('')
      } catch {
        v = title.title
      }
      // 如果有单元格合并，跳过单元格
      if (cell?.mc) {
        r += isVertical ? cell.mc.cs - 1 : cell.mc.rs - 1
        c += isVertical ? cell.mc.rs - 1 : cell.mc.cs - 1
      }
      title.title = v.replaceAll(/(\s+)/g, '')
      title.colum = +beginColum + (isVertical ? r : c)
      title.row = +beginRow + (isVertical ? c : r)
    }
    titles.push(title)
  }

  return titles
}

/**
 * 获取当前选取的坐标
 * @returns
 */
export function getRangePosition() {
  const sheet = luckysheet.getSheet()
  if (sheet.luckysheet_select_save.length == 1) {
    const [beginRow, endRow] = sheet.luckysheet_select_save[0].row
    const [beginColum, endColum] = sheet.luckysheet_select_save[0].column
    return {
      endRow,
      beginRow,
      endColum,
      beginColum,
      sheetNum: sheet.order
    }
  } else {
    ElMessage.error('请选择一个区域！')
    return
  }
}
