import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray.js';
import vtkPolyData from '@kitware/vtk.js/Common/DataModel/PolyData.js';
import vtkPoints from '@kitware/vtk.js/Common/Core/Points.js';
import vtkPointSet from '@kitware/vtk.js/Common/DataModel/PointSet.js';
import vtkCellArray from '@kitware/vtk.js/Common/Core/CellArray.js';
const METHOD_MAPPING = {
  POINTS: 'getPoints',
  POINT_DATA: 'getPointData',
  CELLS:'getCellData',
  CELL_TYPES: 'getCellTypes'
};
const DATATYPES = {
  bit: Uint8Array,
  unsigned_char: Uint8Array,
  char: Int8Array,
  unsigned_short: Uint16Array,
  short: Int16Array,
  unsigned_int: Uint32Array,
  int: Int32Array,
  unsigned_long: Uint32Array,
  long: Int32Array,
  float: Float32Array,
  double: Float64Array
};
const REGISTER_MAPPING = {
  SCALARS: 'addArray'
};

function createArrayHandler(array, setData, nbComponents) {
  let offset = 0;
  function fillWith(line) {
    line.split(' ').forEach(token => {
      if (token.length) {
        array[offset++] = Number(token);
      }
    });
    if (offset < array.length) {
      return true;
    }
    setData(array, nbComponents);
    return false;
  }
  return fillWith;
}
const TYPE_PARSER = {
  DATASET: {
    init(line, datamodel) {
      const type = line.split(' ')[1];
      switch (type) {
        case 'UNSTRUCTURED_GRID':
          datamodel.dataset = vtkPolyData.newInstance();
          // datamodel.scalars=new Map();
          break;
        default:
          console.error(`Dataset of type ${type} not supported`);
      }
      return false;
    },
    parse(line, datamodel) {
      return false;
    }
  },
  POINTS: {
    init(line, dataModel) {
      const [name, size, type] = line.split(' ');
      const array = type === 'float' ? new Float32Array(3 * Number(size)) : new Float64Array(3 * Number(size));
      const dataArray = dataModel.dataset.getPoints();
      dataArray.setName(name);
      dataModel.arrayHandler = createArrayHandler(array, dataArray.setData, 3);
      return true;
    },
    parse(line, dataModel) {
      return dataModel.arrayHandler(line);
    }
  },
  CELLS: {
    init(line, dataModel) {
      const [name, nbCells, nbValues] = line.split(' ');
      const cellArray = dataModel.dataset.getPolys();
      // const cellArray = dataModel.dataset.getLines();
      cellArray.set({
        numberOfCells: Number(nbCells)
      }, true); // Force numberOfCells update
      dataModel.arrayHandler = createArrayHandler(new Uint32Array(Number(nbValues)), cellArray.setData, 1);
      return true;
    },
    parse(line, dataModel) {
      return dataModel.arrayHandler(line);
    }
  },
  POINT_DATA: {
    init(line, dataModel) {
      dataModel.POINT_DATA = Number(line.split(' ')[1]);
      dataModel.activeFieldLocation = 'POINT_DATA';
      return false;
    },
    parse(line, dataModel) {
      return false;
    }
  },
  SCALARS: {
    init(line, dataModel) {
      // SCALARS Temperature float
      // LOOKUP_TABLE default
      // 300.00
      // 300.00
      // 300.00
      // 300.00
      // 300.00
      if(!dataModel.scalars){
        dataModel.scalars=new Map();
      }
      
      const [type, name, dataType] = line.split(' ');
      const numOfComp = 1;
      const array = new DATATYPES[dataType](dataModel.POINT_DATA);
      const dataArray = vtkDataArray.newInstance({
        name:name,
        size:dataModel.POINT_DATA
      });
      dataModel.scalars.set(name,array);
      
      // dataModel.dataset.getPointData().setScalars(dataArray);
      dataModel.arrayHandler = createArrayHandler(array, dataArray.setData, numOfComp);
      return true;
    },
 
    parse(line, dataModel) {
      if (line.split(' ')[0] === 'LOOKUP_TABLE') {
        return true;
      }
      return dataModel.arrayHandler(line);
    }
  }
};
function getParser(line, dataModel) {
  const tokens = line.split(' ');
  return TYPE_PARSER[tokens[0]];
}
function parseLegacyASCII(content) {
  let dataModel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  let parser = null;
  const separatorRegExp = /\r?\n/;
  const separatorRes = separatorRegExp.exec(content);
  const separator = separatorRes !== null ? separatorRes[0] : null;
  content.split(separator).forEach((line, index) => {
    
    if (index < 0) {
      return;
    }
    if (!parser) {
      parser = getParser(line);
      if (!parser) {
        return;
      }
      parser = parser.init(line, dataModel) ? parser : null;
      return;
    }
    if (parser && !parser.parse(line, dataModel)) {
      parser = null;
    }
  });
  return dataModel;
}
var vtkLegacyAsciiParser = {
  parseLegacyASCII
};

export { vtkLegacyAsciiParser as default };
