import PointsData from './spot.json';
import {MathUtil} from '@int/geotoolkit/util/MathUtil';
import {DataTable} from '@int/geotoolkit/data/DataTable';

const points = {};
points.getTestPoints = function () {
  if (points.test == null) {
    points.test = PointsData.test;

    const test = points.test;
    test.x = [];
    test.y = [];
    test.z = [];
    test.sizes = [];
    test.values = [];

    for (let i = 0; i < test.a.length; i++) {
      //第一个点
      test.x.push(Math.sqrt(test.a[i] * test.a[i] * 1200) - 100);
      test.y.push(Math.sqrt(test.b[i] * test.b[i] * 10) - 100);
      test.z.push(Math.sqrt(test.c[i] * test.c[i]) - 100);
      // test.x.push(test.a[i]);
      // test.x.push(test.b[i]);
      // test.x.push(test.c[i]);

      //点大小
      test.sizes.push(Math.abs(Math.cos(test.c[i]) * 4));
      //点颜色
      test.values.push(Math.sin(test.c[i] * 20) * 4);

      //第二个点
      // test.y.push(Math.sqrt(test.a[i] * test.a[i] * 1000) - 100);
      // test.x.push(Math.sqrt(test.b[i] * test.b[i] * 9) - 100);
      // test.z.push(Math.sqrt(test.c[i] * test.c[i]) - 100);
      // test.sizes.push(Math.abs(Math.cos(test.c[i]) * 4));
      // test.values.push(Math.sin(test.c[i] * 10) * 2);
    }
  }
  return points.test;
};
points.disposeTestPoints = function () {
  points.test = null;
};


// #INLINE XLINE Depth Intensity Pressure Timestamp
const microseismic1raw = PointsData.microseismic1raw;


points.microseismic1 = null;
points.getMicroSeismic1 = function () {
  if (points.microseismic1 == null) {
    const countFactor1 = 1;
    points.microseismic1 = {};
    const microseismic1 = points.microseismic1;
    microseismic1.x = [];
    microseismic1.y = [];
    microseismic1.z = [];
    microseismic1.intensity = [];
    microseismic1.pressure = [];
    microseismic1.timestamp = [];
    // #INLINE XLINE Depth Intensity Pressure Timestamp
    for (let i = 0; i < microseismic1raw.length * countFactor1; i++) {
      const ii = i % microseismic1raw.length;
      microseismic1.x.push(microseismic1raw[ii][0] + (countFactor1 > 1 ? (MathUtil.getSeededRandom() - 0.5) : 0));
      microseismic1.y.push(microseismic1raw[ii][1] + (countFactor1 > 1 ? (MathUtil.getSeededRandom() - 0.5) : 0));
      microseismic1.z.push(-microseismic1raw[ii][2] + (countFactor1 > 1 ? (MathUtil.getSeededRandom() - 0.5) : 0));
      microseismic1.intensity.push(microseismic1raw[ii][3]);
      microseismic1.pressure.push(microseismic1raw[ii][4]);
      microseismic1.timestamp.push(microseismic1raw[ii][5]);
    }
  }
  return points.microseismic1;
};

points.microseismic2 = null;
points.getMicroSeismic2 = function () {
  if (points.microseismic2 == null) {
    const countFactor2 = 1;
    points.microseismic2 = {};
    const microseismic2 = points.microseismic2;
    microseismic2.x = [];
    microseismic2.y = [];
    microseismic2.z = [];
    microseismic2.intensity = [];
    microseismic2.pressure = [];
    microseismic2.timestamp = [];
    // #INLINE XLINE Depth Intensity Pressure Timestamp
    for (let i = 0; i < microseismic1raw.length * countFactor2; i++) {
      const ii = i % microseismic1raw.length;
      microseismic2.x.push(microseismic1raw[ii][0] + (countFactor2 > 1 ? (MathUtil.getSeededRandom() - 0.5) : 0));
      microseismic2.y.push(microseismic1raw[ii][1] + (countFactor2 > 1 ? (MathUtil.getSeededRandom() - 0.5) : 0));
      microseismic2.z.push(-microseismic1raw[ii][2] - i * 2 + 900 + (countFactor2 > 1 ? (MathUtil.getSeededRandom() - 0.5) : 0));
      microseismic2.intensity.push(microseismic1raw[ii][3]);
      microseismic2.pressure.push(microseismic1raw[ii][4]);
      microseismic2.timestamp.push(microseismic1raw[ii][5] + i / 10);
    }
  }
  return points.microseismic2;
};

points.microseismic3 = null;
points.getMicroSeismic3 = function () {
  if (points.microseismic3 == null) {
    points.microseismic3 = {};
    const microseismic3 = points.microseismic3;
    microseismic3.x = [];
    microseismic3.y = [];
    microseismic3.z = [];
    microseismic3.size = [];
    microseismic3.value = [];

    // #INLINE XLINE Depth Intensity Pressure Timestamp
    for (let i = 0; i < microseismic1raw.length / 10; i++) {
      microseismic3.x.push(microseismic1raw[i][0] + 800);
      microseismic3.y.push((microseismic1raw[i][1] - 500) * 4);
      microseismic3.z.push((1900 - microseismic1raw[i][2]) * 3);
      microseismic3.size.push(5);
      microseismic3.value.push((microseismic1raw[i][5] + i) / 10);
    }
  }
  return points.microseismic3;
};


points.disposeMicroSeismic = function () {
  points.microseismic1 = null;
  points.microseismic2 = null;
  points.microseismic3 = null;
};

points.GarbageCollect = function () {
  points.disposeMicroSeismic();
  points.disposeTestPoints();
  points.disposeDataTable();
};

const randomPoints = PointsData.randomPoints;

points.dataTable = null;
points.getPointsAsDataTable = function () {
  if (points.dataTable != null) return points.dataTable;
  points.dataTable = new DataTable({
    cols: [
      {name: 'x', type: 'number'},
      {name: 'y', type: 'number'},
      {name: 'z', type: 'number'},
      {name: 'size', type: 'number'},
      {name: 'value', type: 'number'}
    ],
    colsdata: [
      randomPoints.x,
      randomPoints.y,
      randomPoints.z,
      randomPoints.size,
      randomPoints.value
    ]
  });
  return points.dataTable;
};

points.disposeDataTable = function () {
  points.dataTable = null;
};

const scatterpoints = PointsData.scatterpoints;
points.getScatterSet1 = function () {
  const newx = [], newy = [], newz = [], newvalue = [];
  for (let i = 0; i < scatterpoints.x.length; i += 2) {
    newx.push(scatterpoints.x[i]);
    newy.push(scatterpoints.y[i]);
    newz.push(scatterpoints.z[i]);
    newvalue.push(scatterpoints.value[i]);
  }
  return {
    x: newx,
    y: newy,
    z: newz,
    value: newvalue
  };
};

points.getScatterSet2 = function () {
  const newx = [], newy = [], newz = [], newvalue = [];
  for (let i = 0; i < scatterpoints.x.length; i++) {
    if (i >= 320 && i <= 479) continue;
    newx.push(scatterpoints.x[i]);
    newy.push(scatterpoints.y[i]);
    newz.push(scatterpoints.z[i]);
    newvalue.push(scatterpoints.value[i]);
  }
  return {
    x: newx,
    y: newy,
    z: newz,
    value: newvalue
  };
};

points.getScatterSet3 = function () {
  const newx = [], newy = [], newz = [], newvalue = [], newsize = [];
  for (let i = 0; i < scatterpoints.x.length; i++) {
    if (i >= 320 && i <= 479) {
      newx.push(scatterpoints.x[i]);
      newy.push(scatterpoints.y[i]);
      newz.push(scatterpoints.z[i]);
      newvalue.push(scatterpoints.value[i]);
      newsize.push(1.5);
    }
  }
  return {
    x: newx,
    y: newy,
    z: newz,
    value: newvalue,
    size: newsize
  };
};

export {points};
