import * as React from 'react';
import SpreadSheet from '../../SpreadSheetProvider';
import { Props, IRowWithCell } from '../../SpreadSheetProvider/index.data';
import { FormulaManagerConfig, ID, ICell } from '../../index.data';
import { initStoreState, makeStoreState } from '../../SpreadSheetProvider/helper';
import { attachCellLabel } from '../../helper';
import * as R from 'rambda';

const externalSheet1 = {
  id: '1',
  code: 'sheet1',
  columns: [{ width: 90, type: 'numeric' }],
  rows: [
    {
      id: 1,
      type: 'body' as any,
      area: 'flatten' as any,
      level: 1,
      cells: [{ id: 11, value: 10, formula: 'x', formulaDisabled: false, i: 0, j: 0 }],
    },
  ],
};
const externalSheet2 = {
  id: '2',
  code: 'sheet2',
  columns: [{ width: 90, type: 'numeric' }],
  rows: [
    {
      id: 1,
      type: 'body' as any,
      area: 'flatten' as any,
      level: 1,
      cells: [{ id: 11, value: 100, formula: 'x', formulaDisabled: false, i: 0, j: 0 }],
    },
  ],
};

const fakeState1 = attachCellLabel(makeStoreState(externalSheet1));
const fakeState2 = attachCellLabel(makeStoreState(externalSheet2));

const getExternalFakeState = (sheetId: ID) => {
  return [fakeState1, fakeState2].find(it => it.id === sheetId)!;
};
const findExternalFakeStateByCode = (code: string) => {
  return [fakeState1, fakeState2].find(it => (it as any).code === code)!;
};

const formulaConfig: FormulaManagerConfig = {
  // 获取外部单元格
  getExternalCell: (sheetId: ID, cellId: ID): ICell => {
    return getExternalFakeState(sheetId).cells[cellId];
  },
  // sheetId => sheetCode
  getSheetCodeById: (id: ID): string => {
    return (getExternalFakeState(id) as any).code;
  },
  // sheetCode => sheetId
  getSheetIdByCode: (code: string): ID => {
    return findExternalFakeStateByCode(code).id;
  },
  // cellId => cellLabel ('A1'之类,  不含sheetCode)
  getExternalCellLabelById: (sheetId: ID, cellId: ID): string => {
    return getExternalFakeState(sheetId).cells[cellId].label;
  },
  /// cellCode => cellId (不含sheetId)
  getExternalCellIdByLabel: (sheetCode: string, cellLabel: string): ID => {
    const cells = findExternalFakeStateByCode(sheetCode).cells;
    const cellId = Object.keys(cells).find(cellId => (cells[cellId] as any).label === cellLabel);
    if (!cellId) throw new Error('单元格不存在');
    return cells[cellId].id;
  },
};

const Story: React.FC<{ env?: 'test' }> = ({ env }) => {
  const sheet: Props = {
    usage: 'editor',
    columns: [{ width: 90, type: 'numeric' }],
    rows: [
      {
        id: 1,
        type: 'body',
        area: 'flatten',
        level: 1,
        cells: [{ id: 11, value: 1, formula: '{{id1!11}}+{{id2!11}}', formulaDisabled: false }],
      },
      {
        id: 2,
        type: 'body',
        area: 'flatten',
        level: 1,
        cells: [{ id: 21, value: 1, formula: '{{id1!11}}+{{id11}}', formulaDisabled: false }],
      },
    ],
    formulaConfig: { userFormula: 'byPosition', ...formulaConfig },
  };
  return <SpreadSheet {...sheet} env={env} />;
};

export default Story;
