// Ref.: https://deck.gl/docs/developer-guide/custom-layers/layer-lifecycle
import { ScatterplotLayer } from '@deck.gl/layers';

class DougalSequenceLayer extends ScatterplotLayer {
  static layerName = "DougalSequenceLayer";

  static defaultProps = {
    ...ScatterplotLayer.defaultProps,
    valueIndex: 0,
    radiusUnits: "pixels",
    radiusScale: 1,
    lineWidthUnits: "pixels",
    lineWidthScale: 1,
    stroked: false,
    filled: true,
    radiusMinPixels: 1,
    radiusMaxPixels: 50,
    lineWidthMinPixels: 1,
    lineWidthMaxPixels: 50,
    getPosition: { type: 'accessor', value: d => d.positions },
    getRadius: 5,
    getFillColor: [255, 0, 0, 200],
    getLineColor: [255, 0, 0, 200],
    getLineWidth: 2,
    pickable: true
  }

  constructor(props) {
    super(props);
  }

  initializeState(context) {
    super.initializeState(context);
  }

  getPickingInfo({ info, mode }) {
    const index = info.index;
    if (index >= 0) {
      const d = this.props.data.attributes;
      if (d) {
        if (d.udv == 0) {
          info.object = {
            udv: d.udv,
            i: d.value0.value[index],
            j: d.value1.value[index],
            ntba: d.value2.value[index] & 0x01,
            sailline_ntba: d.value2.value[index] & 0x02
          };
        } else if (d.udv == 1) {
          info.object = {
            udv: d.udv,
            i: d.value0.value[index],
            j: d.value1.value[index],
            sailline: d.value3.value[index],
            ntba: d.value2.value[index] & 0x01 ? true : false,
            sailline_ntba: d.value2.value[index] & 0x02 ? true : false
          };
        } else if (d.udv == 2) {
          info.object = {
            udv: d.udv,
            i: d.value0.value[index],
            j: d.value1.value[index],
            ts: Number(d.value2.value[index]),
            εi: d.value3.value[index] / 100,
            εj: d.value4.value[index] / 100,
            delta_μ: d.value5.value[index] / 10,
            delta_σ: d.value6.value[index] / 10,
            delta_R: d.value7.value[index] / 10,
            press_μ: d.value8.value[index],
            press_σ: d.value9.value[index],
            press_R: d.value10.value[index],
            depth_μ: d.value11.value[index] / 10,
            depth_σ: d.value12.value[index] / 10,
            depth_R: d.value13.value[index] / 10,
            fill_μ: d.value14.value[index],
            fill_σ: d.value15.value[index],
            fill_R: d.value16.value[index],
            delay_μ: d.value17.value[index] / 10,
            delay_σ: d.value18.value[index] / 10,
            delay_R: d.value19.value[index] / 10,
            nofire: d.value20.value[index] >> 4,
            autofire: d.value20.value[index] & 0xf
          };
        } else if (d.udv == 3) {
          info.object = {
            udv: d.udv,
            i: d.value0.value[index],
            j: d.value1.value[index],
            ts: Number(d.value2.value[index]),
            εi: d.value3.value[index] / 100,
            εj: d.value4.value[index] / 100,
            co_i: d.value5.value[index] / 100,
            co_j: d.value6.value[index] / 100,
          }
        } else {
          console.warn(`Unknown udv value ${d.udv}. No picking info`);
          info.object = {};
        }
        console.log(`Picked sequence ${info.object.i}, point ${info.object.j}, udv ${info.object.udv}`);
      } else {
        console.log(`No data found index = ${index}`);
      }
    }
    return info;
  }
}

export default DougalSequenceLayer;
