<template>
  <!--结果对比 -->
  <div id="right" class="resultRight">
    <!-- <h1>我是resultRightt组件</h1> -->
  </div>
</template>
<script setup>
import * as d3 from "d3-scale";
import { formatDefaultLocale } from "d3-format";
import { onMounted, onUnmounted, onBeforeUnmount, ref } from "vue";
import vtkGenericRenderWindow from '@kitware/vtk.js/Rendering/Misc/GenericRenderWindow';
import vtkRenderWindowWithControlBar from "@kitware/vtk.js/Rendering/Misc/RenderWindowWithControlBar";
import vtkActor from "@kitware/vtk.js/Rendering/Core/Actor";
import vtkConeSource from "@kitware/vtk.js/Filters/Sources/ConeSource";
import vtkMapper from "@kitware/vtk.js/Rendering/Core/Mapper";
import { createFireControl } from "@/control/fireControl.js";
import { Representation } from "@kitware/vtk.js/Rendering/Core/Property/Constants";
import vtkScalarBarActor from "@kitware/vtk.js/Rendering/Core/ScalarBarActor";
import vtkColorTransferFunction from "@kitware/vtk.js/Rendering/Core/ColorTransferFunction";
import vtkDataArray from "@kitware/vtk.js/Common/Core/DataArray.js";
let vtkObj = {};
let resetCameraFlag = false;
let max, min;
let initFlag = false;
const props = defineProps({
  region: "Temperature",
  count: 50,
  aid: 49,
});
watch(
  () => [props.region, props.count, props.aid],
  (newVal, oldVal) => {
    if (initFlag) {
      if (newVal[1] != oldVal[1]) {
        console.log("步数改变", newVal[1], oldVal[1]);
        if (vtkObj.fcon) {
          vtkScalarRead(props.count); //步数
        }
      }
      if (newVal[0] != oldVal[0]) {
        console.log("标量改变", newVal[0], oldVal[0]);
        if (vtkObj.fcon) {
          vtkShow();
        }
      }
    }
    // if (newVal[2] != oldVal[2]) {
    //   console.log("aid改变", newVal[0], oldVal[0]);
    //   if (vtkObj.fcon) {
    //     vtkGridRead(props.aid);
    //   }
    // }
  },
  { deep: true }
); //深度监视
onMounted(() => {
  // vtkObj.renderWindow.getRenderer().resetCamera();
  // vtkObj.renderWindow.getRenderWindow().render();
  // vtkGridRead(7);
  //  console.log("事故ID改变", newVal[1], oldVal[1]);
  const rootContainer = document.getElementById("right");
  rootContainer.style.position = "relative";
  rootContainer.style.width = "100%";
  rootContainer.style.height = "100%";
  vtkObj.fcon = createFireControl();
  vtkObj.renderWindow = vtkGenericRenderWindow.newInstance();
  vtkObj.renderWindow.setContainer(rootContainer);
  vtkObj.jgMapper = vtkMapper.newInstance();
  vtkObj.jgActor = vtkActor.newInstance();
  vtkObj.jgActor.getProperty().setRepresentation(Representation.SURFACE); //面
  vtkObj.jgActor.setMapper(vtkObj.jgMapper);
  // vtkObj.renderWindow.getRenderer().addActor(vtkObj.jgActor);
    vtkObj.renderWindow.getRenderer().setBackground(0.0, 0.0, 0.0, 0.0);

  vtkObj.scalarBarActor = vtkScalarBarActor.newInstance();
  vtkObj.scalarBarActor.setGenerateTicks(generateTicks(5));
  vtkObj.scalarBarActor.setDrawAboveRangeSwatch(true);
  vtkObj.scalarBarActor.setDrawNanAnnotation(false);
  vtkObj.scalarBarActor.setBoxPosition([1, 0]);
  // 修改设条颜色
  const ctf = vtkColorTransferFunction.newInstance();
  ctf.addRGBPoint(0.0, 0.0, 0.0, 1.0);
  ctf.addRGBPoint(1.0, 0.0, 1.0, 0.5);
  ctf.addRGBPoint(2.0, 0.0, 1.0, 0.0);
  ctf.addRGBPoint(3.0, 1.0, 0.5, 0.0);
  ctf.addRGBPoint(4.0, 1.0, 0.0, 0.0);
  vtkObj.jgMapper.setLookupTable(ctf);
  const lut = vtkObj.jgMapper.getLookupTable();
  vtkObj.scalarBarActor.setScalarsToColors(lut);
   vtkObj.renderWindow.resize();
  // vtkObj.renderWindow.getRenderer().addActor(vtkObj.scalarBarActor);
  // vtkGridRead(props.aid);
  // vtkObj.renderWindow.getRenderer().resetCamera();
  // vtkObj.renderWindow.getRenderWindow().render();
});
function generateTicks(numberOfTicks) {
  return (helper) => {
    const lastTickBounds = helper.getLastTickBounds();
    // compute tick marks for axes
    const scale = d3
      .scaleLinear()
      .domain([0.0, 1.0])
      .range([lastTickBounds[0], lastTickBounds[1]]);
    const samples = scale.ticks(numberOfTicks);
    const ticks = samples.map((tick) => scale(tick));
    // Replace minus "\u2212" with hyphen-minus "\u002D" so that parseFloat() works
    formatDefaultLocale({ minus: "\u002D" });
    const format = scale.tickFormat(ticks[0], ticks[ticks.length - 1], numberOfTicks);
    const tickStrings = ticks
      .map(format)
      .map((tick) => Number(parseFloat(tick).toPrecision(12)).toPrecision()); // d3 sometimes adds unwanted whitespace
    helper.setTicks(ticks);
    helper.setTickStrings(tickStrings);
  };
}

function vtkGridRead(aid) {
  const fcon = vtkObj.fcon;
  fcon.aid = aid;
  fcon
    .initGemetry()
    .then((result) => {
      console.log(fcon.polydata);
      vtkScalarRead(props.count); //步数
    })
    .catch((err) => {});
}

function vtkScalarRead(step) {
  const fcon = vtkObj.fcon;
  fcon
    .getScalrsByStep(step)
    .then((result) => {
      console.log(fcon.scalar);
      vtkShow();
    })
    .catch((err) => {
      console.log(err);
    });
}
function vtkShow() {
  console.log("执行了");
  const scalarBarActor = vtkObj.scalarBarActor;
  const mapper = vtkObj.jgMapper;
  const actor = vtkObj.jgActor;
  const fcon = vtkObj.fcon;
  console.log(props.region);
  const scalarArray = fcon.scalar.get(props.region);
  if (!scalarArray || !fcon.polydata) {
    return;
  }
  // console.log("scalarArray:", scalarArray);
  const dataArray = vtkDataArray.newInstance({
    name: props.region,
    size: fcon.polydata.getNumberOfPoints(),
  });
  // console.log(dataArray);
  dataArray.setData(scalarArray);
  fcon.polydata.getPointData().setScalars(dataArray);
  mapper.setInputData(fcon.polydata);
  getMinMax(scalarArray);
  mapper.setScalarRange(parseFloat(min.toFixed(3)), parseFloat(max.toFixed(3))); //设置范围
  scalarBarActor.setAxisLabel(props.region);

  mapper.clearColorArrays(); //强制重建颜色
  actor.getProperty().setOpacity(props.count); //设置错误的透明度使得页面重新加载  不设置不刷新页面
  // vtkmodel.renderer.resetCamera();
  vtkObj.renderWindow.getRenderer().addActor(scalarBarActor);
  vtkObj.renderWindow.getRenderer().addActor(actor);
  if (!resetCameraFlag) {
    vtkObj.renderWindow.getRenderer().resetCamera();
    resetCameraFlag = true;
  }
  vtkObj.renderWindow.resize();
  vtkObj.renderWindow.getRenderWindow().render();

  initFlag = true;
}
function getMinMax(scalars) {
  // console.log("getMinMax:",scalars);
  min = scalars[0];
  max = scalars[0];
  for (let index = 0; index <= scalars.length; index++) {
    let scalar = scalars[index];
    if (min > scalar) {
      min = scalar;
    }
    if (max < scalar) {
      max = scalar;
    }
  }
  // console.log("max，min:", max.value, min.value);
}
onBeforeUnmount(() => {
  vtkObj.scalarBarActor.delete();
  vtkObj.jgMapper.delete();
  vtkObj.jgActor.delete();
  vtkObj.renderWindow.delete();
  vtkObj = null;
});
const rightChangAid = (rightAid, step, region) => {
  console.log(rightAid, step, region);
  if (vtkObj.fcon) {
    console.log(props);
    vtkGridRead(rightAid);
  }
};
defineExpose({ rightChangAid });
</script>
<style scoped></style>
