<template>
  <div class="row">
    <div class="col-1"></div>
    <div class="col-10 q-pt-md">
      <div ref="div" style="width: 100%; background-color: #646464; height: 40vh">
        <div style="position: absolute">
          <Vue3DraggableResizable v-show="now_v_h == 'vertical'" :class="[options_model_v == index ? 'action' : 'point']"
            v-for="(item, index) in value_v_point.length" :initW="point.w" :initH="point.h" :resizable="false" :x="0"
            v-model:y="value_v_point[index].y" @activated="options_model_v = index; now_v_h = 'vertical'"
            @click="options_model_v = index; now_v_h = 'vertical'" @dragging="dragStartHandle($event, index, 'v')">
            <span>v{{
              index + 1
            }}</span>
          </Vue3DraggableResizable>
          <Vue3DraggableResizable v-show="now_v_h == 'vertical'" :class="[options_model_v == index ? 'action' : 'point']"
            v-for="(item, index) in value_v_point.length" :initW="point.w" :initH="point.h" :resizable="false"
            :x="max.x - 20" @click="options_model_v = index; now_v_h = 'vertical'" v-model:y="value_v_point[index].y"
            @activated="options_model_v = index; now_v_h = 'vertical'" @dragging="dragStartHandle($event, index, 'v')">
            <span>v{{
              index + 1
            }}</span>
          </Vue3DraggableResizable>
          <Vue3DraggableResizable v-show="now_v_h == 'level'" :class="[options_model_h == index ? 'action' : 'point']"
            v-for="(item, index) in value_h_point.length" :initW="point.w" :initH="point.h" :resizable="false"
            v-model:x="value_h_point[index].x" :y="0" @activated="options_model_h = index; now_v_h = 'level'"
            @click="options_model_h = index; now_v_h = 'level'" @dragging="dragStartHandle($event, index, 'h')">
            <span>h{{
              index + 1
            }}</span>
          </Vue3DraggableResizable>
          <Vue3DraggableResizable v-show="now_v_h == 'level'" :class="[options_model_h == index ? 'action' : 'point']"
            v-for="(item, index) in value_h_point.length" :initW="point.w" :initH="point.h" :resizable="false"
            v-model:x="value_h_point[index].x" @activated="options_model_h = index; now_v_h = 'level'" :y="max.y - 20"
            @click="options_model_h = index; now_v_h = 'level'" @dragging="dragStartHandle($event, index, 'h')">
            <span>h{{
              index + 1
            }}</span>
          </Vue3DraggableResizable>
        </div>
      </div>
    </div>
  </div>
  <div>
    <div class="row">
      <div class="col-1"></div>
      <div class="col-5">
        <q-radio v-model="now_v_h" val="level" :label="$t('level')" />
      </div>
      <div class="col-5"><q-radio v-model="now_v_h" val="vertical" :label="$t('vertical')" /></div>
    </div>

    <div>
      <div class="row q-py-md" v-show="now_v_h == 'vertical'">
        <div class="col-1"></div>
        <div class="col-2 q-px-md"> <q-select class="q-pt-md" :label="$t('vertical point')"
            @update:model-value="(val) => { chang_point_amount(val, 'v') }" :dense="true" filled
            v-model="value_v_point_amount" :options="options_value_v_point_amount" emit-value map-options /></div>
        <div class="col-2 q-pt-md  q-px-md"><q-btn color="white" @click="()=>{add_dialog=true;value_point_amount_tmp_v=value_v_point_amount}" text-color="black"
            :label="$t('add control points')" /></div>
        <div class="col-2 q-pt-md q-px-md"><q-btn color="white" @click="() => { del_dialog = true }"
            :disable="value_v_point_amount <= 5" text-color="black" :label="$t('reduce control points')" /></div>
            <div class="q-pt-md q-px-md col-5"> <q-btn color="white" @click="resetall_all" text-color="black"
            :label="$t('Reset all program control points')" /></div>
      </div>

      <div class="row" v-show="now_v_h == 'vertical'">
        <div class="col-1"></div>
        <div class="col-2 q-px-md" @mousewheel="details_selsect_v"> <q-select class="q-pt-md " :label="$t('vertical')"
            @update:model-value="(val) => { options_v = val }" :dense="true" filled v-model="options_model_v"
            :options="options_v" emit-value map-options /></div>
        <div class="col-2 q-px-md" @mousewheel="details_selsect_val($event, 'v')"> <q-input v-if="value_v[options_model_v]"
            filled type="number" class="q-pt-md" :dense="true" v-model="value_v[options_model_v].y"
            @update:model-value="chang(options_model_v, $event, 'v')" lazy-rules />
        </div>
        <div class="q-pt-md  col-2">
          <q-btn color="white" @click="reset(options_model_v, true, 'v')" text-color="black" :label="$t('reset')" />
        </div>

            <div class="q-pt-md q-px-md col-5"> <q-btn color="white" @click="resetall" text-color="black"
            :label="$t('reset all')+value_v_point_amount+$t('point')" /></div>
      </div>
    </div>

    <div>
      <div class="row q-py-md" v-show="now_v_h == 'level'">
        <div class="col-1"></div>
        <div class="col-2 q-px-md"> <q-select class="q-pt-md" :label="$t('level point')"
            @update:model-value="(val) => { chang_point_amount(val, 'h') }" :dense="true" filled
            v-model="value_h_point_amount" :options="options_value_h_point_amount" emit-value map-options /></div>
        <div class="col-2 q-pt-md "><q-btn color="white" @click="add_dialog = true;value_point_amount_tmp_h=value_h_point_amount" text-color="black"
            :label="$t('add control points')" /></div>
        <div class="col-2 q-pt-md "><q-btn color="white" @click="() => { del_dialog = true }"
            :disable="value_h_point_amount <= 5" text-color="black" :label="$t('reduce control points')" /></div>
            <div class="q-pt-md q-px-md col-5"> <q-btn color="white" @click="resetall_all" text-color="black"
            :label="$t('Reset all program control points')" /></div>
      </div>

      <div class="row" v-show="now_v_h == 'level'">
        <div class="col-1"></div>
        <div class="col-2 q-px-md" @mousewheel="details_selsect_h"> <q-select class="q-pt-md" :label="$t('level')"
            @update:model-value="(val) => { options_h = val }" :dense="true" filled v-model="options_model_h"
            :options="options_h" emit-value map-options /></div>
        <div class="col-2 q-px-md" @mousewheel="details_selsect_val($event, 'h')"> <q-input v-if="value_h[options_model_h]"
            filled type="number" class="q-pt-md" :dense="true" v-model="value_h[options_model_h].x"
            @update:model-value="chang(options_model_h, $event, 'h')" lazy-rules />
        </div>
        <div class="q-pt-md col-2">
          <q-btn color="white" @click="reset(options_model_h, true, 'h')" text-color="black" :label="$t('reset')" />
        </div>

            <div class="q-pt-md q-px-md col-5"> <q-btn color="white" @click="resetall" text-color="black"
            :label="$t('reset all')+value_h_point_amount+$t('point')" /></div>
      </div>
    </div>
  </div>
  <div style="position: absolute;bottom: 0;">
    <q-checkbox v-model="auxiliary_line" @update:model-value="(value, evt) => { send_auxiliary_line(value) }"
      :label="$t('auxiliary lines')" />
  </div>


  <q-dialog v-model="del_dialog">
    <q-card style="width: 700px; max-width: 80vw;">
      <q-card-section>
        <div class="text-h6">{{ $t('confirm delete') }}</div>
      </q-card-section>
      <q-card-section class="q-pt-none">
        {{ $t('OK to delete you will lose the data you adjusted now') }}
      </q-card-section>
      <q-card-actions align="right" class="bg-white text-teal">
        <q-btn flat :label="$t('Cancel')" color="primary" v-close-popup />
        <q-btn flat :label="$t('ok')" @click="del" color="primary" v-close-popup />
      </q-card-actions>
    </q-card>
  </q-dialog>

  <!-- <q-dialog v-model="add_dialog">
    <q-card style="width: 700px; max-width: 80vw;">
      <q-card-section>
        <div class="text-h6">{{ $t('Whether to add control points') }}</div>
      </q-card-section>
      <q-card-section class="q-pt-none">
        <q-input filled v-model="now_amount" type="number" :label="$t('Please enter the points you need to add')" />
      </q-card-section>
      <q-card-actions align="right" class="bg-white text-teal">
        <q-btn flat :label="$t('Cancel')" color="primary" v-close-popup />
        <q-btn flat :label="$t('ok')" @click="add" color="primary" v-close-popup />
      </q-card-actions>
    </q-card>
  </q-dialog> -->
  <q-dialog position="bottom" v-model="add_dialog">
    <q-card style="width: 700px; max-width: 80vw;">
      <q-card-section>
        <div class="text-h6">{{ $t('Whether to add control points') }}</div>
      </q-card-section>
      <q-card-section class="q-pt-none">
        <div class="row">
          <div v-if="now_v_h=='vertical'" class="col-10"><q-input filled v-model="value_v_point_amount" min="3" disable type="number"
              :label="$t('Please enter the points you need to add')" @update:model-value="(val: any) => {  }" />
          </div>
          <div v-else class="col-10"><q-input filled v-model="value_h_point_amount" min="3" disable type="number"
              :label="$t('Please enter the points you need to add')" @update:model-value="(val: any) => {  }" />
          </div>
          <div class="col-1 q-pa-sm"> <q-btn color="white" text-color="black" label="↑"
              @click="() => {now_v_h=='vertical'?preview_v(Number(value_v_point_amount)+1):preview_h(Number(value_h_point_amount)+1)}" /> </div>
          <div class="col-1 q-pa-sm"> <q-btn color="white" text-color="black" label="↓"
              @click="() => {now_v_h=='vertical'?preview_v(Number(value_v_point_amount)-1):preview_h(Number(value_h_point_amount)-1)}" /></div>
        </div>

      </q-card-section>
      <q-card-actions align="right" class="bg-white text-teal">
        <q-btn flat :label="$t('Cancel')" @click="()=>{now_v_h=='vertical'?preview_v(value_point_amount_tmp_v):preview_h(value_point_amount_tmp_h)}" color="primary" v-close-popup />
        <q-btn flat :label="$t('ok')" @click="add" color="primary" v-close-popup />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<style scoped>
.action {
  background-color: rgb(27, 180, 111);
  border-radius: 50%;
}

.point {
  background-color: rgb(186, 245, 245);
  border-radius: 50%;
}
</style>

<script lang="ts">
import {
  defineComponent,
  onMounted,
  ref,
  computed,
  watch,
  nextTick,
  onBeforeUnmount,
  defineExpose
} from "vue";
import { useStore } from "src/store";
import { useI18n } from "vue-i18n";
import Vue3DraggableResizable from 'vue3-draggable-resizable'
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css'


import DensityCorrection from "src/entities/DensityCorrection";
import DensityCorrectionPoint from "src/entities/DensityCorrectionPoint";
import { config } from "process";
import { QSelect, QInput, QBtn } from "quasar";
import { div } from "zrender/lib/core/vector";
import GlobalData from "src/common/GlobalData";

export default defineComponent({
  name: "ComponentDensityCorrection",
  components: {
    Vue3DraggableResizable
  },
  setup() {
    let set = GlobalData.getInstance().getCurrentClient();
    let $store = useStore();
    let $t = useI18n();
    const ste_status = ref(0);
    let config = JSON.parse($store.state.fusion_configuration).projectors[0];
    const configselsect = computed(() => {
      return $store.state.selected_projector;
    });

    //用于计算当前投影仪的索引
    let allconfig = JSON.parse($store.state.fusion_configuration);
    let serverconfig = JSON.parse($store.state.fusion_configuration);
    const selectedprojector = computed(() => {
      return (
        $store.getters.GetTheCurrentlySelectedCamera[0] * serverconfig.col +
        $store.getters.GetTheCurrentlySelectedCamera[1]
      );
    });
    const now_v_h = ref("")
    const value_h_point_amount = ref(5)
    const value_v_point_amount = ref(5)
    const add_dialog = ref(false)
    const now_amount = ref(0)

    const options_value_h_point_amount = ref(<string[]>[])
    const options_value_v_point_amount = ref(<string[]>[])
    const deepcopy = (o1: any, o2: any) => {
      for (let k in o2) {
        if (typeof o2[k] === "object") {
          o1[k] = {};
          deepcopy(o1[k], o2[k]);
        } else {
          o1[k] = o2[k];
        }
      }
    };

    const use_server_config = () => {
      value_h_point_amount.value = 5
      value_v_point_amount.value = 5
      let a = 5
      value_h.value = []
      value_h_point.value = []
      for (let index = 0; index < config.multi_hor_density[a].length; index++) {
        let tmp: DensityCorrectionPoint = config.multi_hor_density[a][index];
        value_h.value.push(tmp)
        let x_y = coordinate_transformation_value_to_xy(tmp.x, tmp.y)
        let def_x_f = coordinate_transformation_value_to_xy(tmp.def_x, tmp.def_y)
        let tmp_point: DensityCorrectionPoint = {
          control_point: tmp.control_point,
          x: x_y.x,
          y: x_y.y,
          def_x: def_x_f.x,
          def_y: def_x_f.y
        }

        value_h_point.value.push(tmp_point)
      }


      value_v.value = []
      value_v_point.value = []
      for (let index = 0; index < config.multi_ver_density[a].length; index++) {
        let tmp: DensityCorrectionPoint = config.multi_ver_density[a][index];
        value_v.value.push(tmp)
        let x_y = coordinate_transformation_value_to_xy(tmp.x, tmp.y)
        let def_x_f = coordinate_transformation_value_to_xy(tmp.def_x, tmp.def_y)
        let tmp_point: DensityCorrectionPoint = {
          control_point: tmp.control_point,
          x: x_y.x,
          y: x_y.y,
          def_x: def_x_f.x,
          def_y: def_x_f.y
        }
        value_v_point.value.push(tmp_point)
      }

    }

    const use_set_cache = () => {
      if (set_cache.value[selectedprojector.value] != null) {
        let tmp = JSON.parse(set_cache.value[selectedprojector.value]);
        value_h.value = []
        value_v.value = []
        value_h_point.value = []
        value_v_point.value = []
        deepcopy(value_h.value, tmp.value_h)
        deepcopy(value_v.value, tmp.value_v)
        value_h_point_amount.value = tmp.value_h_point_amount
        value_v_point_amount.value = tmp.value_v_point_amount
        deepcopy(value_h_point.value, tmp.value_h_point)
        deepcopy(value_v_point.value, tmp.value_v_point)
        recalculate_coordinates()
      }
    }


    const save_set_cache = () => {
      let tmp = { value_h: value_h.value, value_v: value_v.value, value_v_point_amount: value_v_point_amount.value, value_h_point_amount: value_h_point_amount.value, value_v_point: value_v_point.value, value_h_point: value_h_point.value }
      set_cache.value[selectedprojector.value] = JSON.stringify(tmp);
    }




    const auxiliary_line = ref(false)




    watch(
      () => configselsect,
      (newVal, oldVal) => {
        let tmp = JSON.parse($store.state.fusion_configuration);
        let fortmp = null;
        let i;
        for (i of tmp.projectors) {
          if (
            i.col === $store.getters.GetTheCurrentlySelectedCamera[1] &&
            i.row === $store.getters.GetTheCurrentlySelectedCamera[0]
          ) {
            fortmp = JSON.parse(JSON.stringify(i));
          }
        }
        ste_status.value = 1;
        config = JSON.parse(JSON.stringify(fortmp));
        use_server_config();
        use_set_cache();
        options_value_h_point_amount.value = Object.keys(config.multi_hor_density)
        options_value_v_point_amount.value = Object.keys(config.multi_ver_density)
        setTimeout(() => {
          ste_status.value = 0;
        }, 100);
      },
      { deep: true }
    );



    const set_cache: any = ref([]);
    //值
    let value_h = ref(<DensityCorrection[]>[]);
    let value_v = ref(<DensityCorrection[]>[]);
    //位置
    let value_h_point = ref(<DensityCorrectionPoint[]>[]);
    let value_v_point = ref(<DensityCorrectionPoint[]>[]);
    /**
     * 当前选中的点的索引
     */
    const options_model_v = ref(0)
    const options_model_h = ref(0)

    const div = ref();
    const max = ref({ x: 0, y: 0 })
    const point = ref({ w: 20, h: 20 })


    watch(() => now_v_h, (newval, oldval) => {
      if (newval.value == 'level') {
        set?.SetBlendingOption("blending_grids_select_ui", "hor_density")
        set?.SetBlendingOption("blending_grids_control_point_count", `${value_h_point_amount.value}`)
        set?.SetBlendingOption("blending_grids_select_point", `${options_model_h.value + 1}`)
      } else {
        set?.SetBlendingOption("blending_grids_select_ui", "ver_density")
        set?.SetBlendingOption("blending_grids_control_point_count", `${value_v_point_amount.value}`)
        set?.SetBlendingOption("blending_grids_select_point", `${options_model_v.value + 1}`)
      }
    }, { deep: true })
    watch(() => value_v_point_amount, (newval, oldval) => {
      set?.SetBlendingOption("blending_grids_control_point_count", `${newval.value}`)
      if (now_v_h.value == 'level') {
        set?.SetBlendingOption("blending_grids_select_ui", "hor_density")
      } else {
        set?.SetBlendingOption("blending_grids_select_ui", "ver_density")
      }
      save_set_cache()
    }, { deep: true })
    watch(() => value_h_point_amount, (newval, oldval) => {
      set?.SetBlendingOption("blending_grids_control_point_count", `${newval.value}`)
      if (now_v_h.value == 'level') {
        set?.SetBlendingOption("blending_grids_select_ui", "hor_density")
      } else {
        set?.SetBlendingOption("blending_grids_select_ui", "ver_density")
      }
      save_set_cache()
    }, { deep: true })

    watch(() => options_model_h, (newval, oldval) => {
      set?.SetBlendingOption("blending_grids_select_point", `${value_h_point.value[newval.value].control_point}`)
    }, { deep: true })
    watch(() => options_model_v, (newval, oldval) => {
      set?.SetBlendingOption("blending_grids_select_point", `${value_v_point.value[newval.value].control_point}`)
    }, { deep: true })

    /**
     * 分辨率和页面的比例
     */
    let Proportion = ref({ x: 100, y: 1000 });
    nextTick(() => {
      max.value.x = div.value.offsetWidth
      max.value.y = div.value.offsetHeight
    })


    const chang_point_amount = (val: number, type: string) => {
      start_point(val, type)
      save_set_cache()
    }

    const dragStartHandle = ($event: any, index: number, type: string) => {
      let obj_x = $event.x
      let obj_y = $event.y
      let tmp = coordinate_transformation_xy_to_value(obj_x, obj_y)
      if (type == 'v') {
        value_v.value[index].y = Math.trunc(tmp.y)
        send_value(index, type, Math.trunc(tmp.y))
      }
      if (type == 'h') {
        value_h.value[index].x = Math.trunc(tmp.x)
        send_value(index, type, Math.trunc(tmp.x))
      }
      save_set_cache()
    }

    const chang = (index: number, $event: any, type: string, send?: boolean) => {
      $event = Number($event)
      if (type == 'v') {
        let tmp = coordinate_transformation_value_to_xy(0, $event)
        value_v_point.value[index].y = Math.trunc(tmp.y)
        send_value(index, type, Math.trunc(value_v.value[index].y), send)
      }
      if (type == 'h') {
        let tmp = coordinate_transformation_value_to_xy($event, 0)
        value_h_point.value[index].x = Math.trunc(tmp.x)
        send_value(index, type, Math.trunc(value_h.value[index].x), send)
      }
      save_set_cache()
    }
    const options_v = computed(() => {
      let tmp = []
      for (let index = 0; index < Number(value_v_point_amount.value); index++) {
        tmp.push({
          label: `v${index + 1}`,
          value: index,
        })
      }
      return tmp
    })
    const options_h = computed(() => {
      let tmp = []
      for (let index = 0; index < Number(value_h_point_amount.value); index++) {
        tmp.push({
          label: `h${index + 1}`,
          value: index,
        })
      }
      return tmp
    })

    /**
     * 将分辨率的坐标转换为 页面的坐标
     * @param x x坐标
     * @param y y坐标
     */
    const coordinate_transformation_value_to_xy = (x: number, y: number): { x: number, y: number } => {
      x = x / Proportion.value.x
      y = (allconfig.projector_height - y) / Proportion.value.y
      return { x, y }
    }


    /**
     * 将页面的坐标转换为 分辨率的坐标
     * @param x x坐标
     * @param y y坐标
     */
    const coordinate_transformation_xy_to_value = (x: number, y: number): { x: number, y: number } => {
      x = x * Proportion.value.x
      y = (max.value.y - y) * Proportion.value.y
      return { x, y }
    }

    const start_point = (a: number, type: string) => {
      if (type == 'h' || type == 'all') {
        value_h.value = []
        value_h_point.value = []
        for (let index = 0; index < config.multi_hor_density[a].length; index++) {
          let tmp: DensityCorrectionPoint = config.multi_hor_density[a][index];
          value_h.value.push(tmp)
          let x_y = coordinate_transformation_value_to_xy(tmp.x, tmp.y)
          let def_x_f = coordinate_transformation_value_to_xy(tmp.def_x, tmp.def_y)
          let tmp_point: DensityCorrectionPoint = {
            control_point: tmp.control_point,
            x: x_y.x,
            y: x_y.y,
            def_x: def_x_f.x,
            def_y: def_x_f.y
          }
          value_h_point.value.push(tmp_point)
        }
      }
      if (type == 'v' || type == 'all') {
        value_v.value = []
        value_v_point.value = []
        for (let index = 0; index < config.multi_ver_density[a].length; index++) {
          let tmp: DensityCorrectionPoint = config.multi_ver_density[a][index];
          value_v.value.push(tmp)
          let x_y = coordinate_transformation_value_to_xy(tmp.x, tmp.y)
          let def_x_f = coordinate_transformation_value_to_xy(tmp.def_x, tmp.def_y)
          let tmp_point: DensityCorrectionPoint = {
            control_point: tmp.control_point,
            x: x_y.x,
            y: x_y.y,
            def_x: def_x_f.x,
            def_y: def_x_f.y
          }
          value_v_point.value.push(tmp_point)
        }
      }

    }


    const start = () => {
      let tmp: DensityCorrectionPoint = { control_point: 0, x: 0, y: 0, def_x: 0, def_y: 0 };
      value_v.value.push(tmp)
      value_h.value.push(tmp)
      value_v_point.value.push(tmp)
      value_h_point.value.push(tmp)
    }
    start()
    onMounted(() => {
      Proportion.value.x = allconfig.projector_width / (div.value.offsetWidth - point.value.w)
      Proportion.value.y = allconfig.projector_height / (div.value.offsetHeight - point.value.h)
      options_value_h_point_amount.value = Object.keys(config.multi_hor_density)
      options_value_v_point_amount.value = Object.keys(config.multi_ver_density)
      let tmp = JSON.parse($store.state.fusion_configuration).options;
      auxiliary_line.value = tmp.blending_grids_density_assistant_lines == "1"
      now_v_h.value = "level"
      start_point(Number(options_value_h_point_amount.value[0]), 'h')
      start_point(Number(options_value_v_point_amount.value[0]), 'v')
      if (
        sessionStorage.DensityCorrection &&
        sessionStorage.DensityCorrection.length > 0
      ) {
        set_cache.value = JSON.parse(sessionStorage.DensityCorrection);
        use_set_cache();
      }
      window.onresize = () => {
        return (() => {
          if (div != null) {
            recalculate_coordinates()
          }
        })();
      };
    })

    onBeforeUnmount(() => {
      sessionStorage.DensityCorrection = JSON.stringify(set_cache.value);
    });

    const reset = (index: number, send: boolean, type: string) => {
      if (type == 'v') {
        value_v.value[index].x = value_v.value[index].def_x
        value_v.value[index].y = value_v.value[index].def_y
        chang(index, value_v.value[index].def_y, type, send)
      }
      if (type == 'h') {
        value_h.value[index].x = value_h.value[index].def_x
        value_h.value[index].y = value_h.value[index].def_y
        chang(index, value_h.value[index].def_x, type, send)
      }
      save_set_cache()
    }
    const send_value = (index: number, type: string, value: number, send: boolean = true) => {
      if (!send) return;
      let row = $store.getters.GetTheCurrentlySelectedCamera[0]
      let col = $store.getters.GetTheCurrentlySelectedCamera[1]
      if (type == 'v') {
        set?.SetBlendingVerDensity(row, col, Number(value_v_point_amount.value), index + 1, Number(value));
      }
      if (type == 'h') {
        set?.SetBlendingHorDensity(row, col, Number(value_h_point_amount.value), index + 1, Number(value));
      }
    }
    const resetall = () => {
      switch (now_v_h.value) {
        case 'vertical':
          for (let index = 0; index < value_v_point_amount.value; index++) {
            reset(index, false, 'v')
          }
          send_value(-1, 'v', 0);
          break;
        case 'level':
          for (let index = 0; index < value_h_point_amount.value; index++) {
            reset(index, false, 'h')
          }
          send_value(-1, 'h', 0);
          break;
        default:
          break;
      }
      send_value(-1, 'v', 0);
      save_set_cache()
    }

    /**
     * 重置全部为默认值
     */
    const resetall_all = () => {
      switch (now_v_h.value) {
        case 'vertical':
          let tmp =value_v_point_amount.value
          for (let key in config.multi_ver_density) {
            for (let index = 0; index < config.multi_ver_density[key].length; index++) {
              config.multi_ver_density[key][index].x = config.multi_ver_density[key][index].def_x
              config.multi_ver_density[key][index].y = config.multi_ver_density[key][index].def_y
            }
            start_point(Number(key), 'v')
          }
          value_v_point_amount.value = tmp
          start_point(Number(tmp), 'v')
          send_value(-2, 'v', 0);
          break;
        case 'level':
        let tmpp =value_h_point_amount.value
          for (let key in config.multi_hor_density) {
            for (let index = 0; index < config.multi_hor_density[key].length; index++) {
              config.multi_hor_density[key][index].x = config.multi_hor_density[key][index].def_x
              config.multi_hor_density[key][index].y = config.multi_hor_density[key][index].def_y
            }
            start_point(Number(key), 'h')
          }
          value_h_point_amount.value = tmpp
          start_point(Number(tmpp), 'h')
          send_value(-2, 'h', 0);
          break;
        default:
          break;
      }
    }
    const del_dialog = ref(false)
    const add_control_points = (amount: number, type: string) => {
      if (amount < 5) return
      let tmpconfig: any = null
      let row = $store.getters.GetTheCurrentlySelectedCamera[0]
      let col = $store.getters.GetTheCurrentlySelectedCamera[1]


      switch (type) {
        case 'v':
          set?.AddBlendingCtrlPoint('ver_density', row, col, Number(amount)).then((res) => {
            if (res == null || typeof (res) == "undefined") return
            update(res?.config)
            let tmp = JSON.parse(res?.config);
            let fortmp = null;
            let i;
            for (i of tmp.projectors) {
              if (
                i.col === $store.getters.GetTheCurrentlySelectedCamera[1] &&
                i.row === $store.getters.GetTheCurrentlySelectedCamera[0]
              ) {
                fortmp = JSON.parse(JSON.stringify(i));
              }
            }
            ste_status.value = 1;
            tmpconfig = JSON.parse(JSON.stringify(fortmp));
            config = JSON.parse(JSON.stringify(fortmp));
            options_value_v_point_amount.value = []
            for (let key in tmpconfig.multi_ver_density) {
              options_value_v_point_amount.value.push(key)
            }

            value_v_point_amount.value = amount
            $store.commit("setfusion_configuration", res?.config);
            value_v.value = []
            value_v_point.value = []
            for (let index = 0; index < tmpconfig.multi_ver_density[amount].length; index++) {
              let tmp: DensityCorrectionPoint = tmpconfig.multi_ver_density[amount][index];
              value_v.value.push(tmp)
              let x_y = coordinate_transformation_value_to_xy(tmp.x, tmp.y)
              let def_x_f = coordinate_transformation_value_to_xy(tmp.def_x, tmp.def_y)
              let tmp_point: DensityCorrectionPoint = {
                control_point: tmp.control_point,
                x: x_y.x,
                y: x_y.y,
                def_x: def_x_f.x,
                def_y: def_x_f.y
              }
              value_v_point.value.push(tmp_point)
            }
          });
          break;
        case 'h':
          set?.AddBlendingCtrlPoint('hor_density', row, col, Number(amount)).then((res) => {
            if (res == null || typeof (res) == "undefined") return
            update(res?.config)
            let tmp = JSON.parse(res?.config);
            let fortmp = null;
            let i;
            for (i of tmp.projectors) {
              if (
                i.col === $store.getters.GetTheCurrentlySelectedCamera[1] &&
                i.row === $store.getters.GetTheCurrentlySelectedCamera[0]
              ) {
                fortmp = JSON.parse(JSON.stringify(i));
              }
            }
            ste_status.value = 1;
            tmpconfig = JSON.parse(JSON.stringify(fortmp));
            config = JSON.parse(JSON.stringify(fortmp));
            options_value_h_point_amount.value = []
            for (let key in tmpconfig.multi_hor_density) {
              options_value_h_point_amount.value.push(key)
            }
            value_h_point_amount.value = amount
            $store.commit("setfusion_configuration", res?.config);
            value_h.value = []
            value_h_point.value = []
            for (let index = 0; index < tmpconfig.multi_hor_density[amount].length; index++) {
              let tmp: DensityCorrectionPoint = tmpconfig.multi_hor_density[amount][index];
              value_h.value.push(tmp)
              let x_y = coordinate_transformation_value_to_xy(tmp.x, tmp.y)
              let def_x_f = coordinate_transformation_value_to_xy(tmp.def_x, tmp.def_y)
              let tmp_point: DensityCorrectionPoint = {
                control_point: tmp.control_point,
                x: x_y.x,
                y: x_y.y,
                def_x: def_x_f.x,
                def_y: def_x_f.y
              }
              value_h_point.value.push(tmp_point)
            }
          });
          break;
        default:
          break;
      }

    }

    /**
     * 更新比例 更新坐标
     */
    const recalculate_coordinates = () => {
      try {
        Proportion.value.x = allconfig.projector_width / (div.value.offsetWidth - point.value.w)
        Proportion.value.y = allconfig.projector_height / (div.value.offsetHeight - point.value.h)
        max.value.x = div.value.offsetWidth
        max.value.y = div.value.offsetHeight
        for (let index = 0; index < value_v_point_amount.value; index++) {
          let x_y = coordinate_transformation_value_to_xy(value_v.value[index].x, value_v.value[index].y)
          let def_x_f = coordinate_transformation_value_to_xy(value_v.value[index].def_x, value_v.value[index].def_y)
          value_v_point.value[index].x = x_y.x
          value_v_point.value[index].y = x_y.y
          value_v_point.value[index].def_x = def_x_f.x
          value_v_point.value[index].def_y = def_x_f.y
        }
        for (let index = 0; index < value_h_point_amount.value; index++) {
          let x_y = coordinate_transformation_value_to_xy(value_h.value[index].x, value_h.value[index].y)
          let def_x_f = coordinate_transformation_value_to_xy(value_h.value[index].def_x, value_h.value[index].def_y)
          value_h_point.value[index].x = x_y.x
          value_h_point.value[index].y = x_y.y
          value_h_point.value[index].def_x = def_x_f.x
          value_h_point.value[index].def_x = def_x_f.y
        }
      } catch (error) {

      }
    }


    const reduce_control_points = (amount: number, type: string) => {
      if (amount <= 5) return
      let tmpconfig: any = null
      let row = $store.getters.GetTheCurrentlySelectedCamera[0]
      let col = $store.getters.GetTheCurrentlySelectedCamera[1]
      switch (type) {
        case 'v':
          set?.DelBlendingCtrlPoint('ver_density', row, col, Number(amount)).then((res) => {
            if (res == null || typeof (res) == "undefined") return
            let tmp = JSON.parse(res?.config);
            let fortmp = null;
            let i;
            for (i of tmp.projectors) {
              if (
                i.col === $store.getters.GetTheCurrentlySelectedCamera[1] &&
                i.row === $store.getters.GetTheCurrentlySelectedCamera[0]
              ) {
                fortmp = JSON.parse(JSON.stringify(i));
              }
            }
            ste_status.value = 1;
            options_value_v_point_amount.value = []
            tmpconfig = JSON.parse(JSON.stringify(fortmp));
            config = JSON.parse(JSON.stringify(fortmp));
            for (let key in tmpconfig.multi_ver_density) {
              options_value_v_point_amount.value.push(key)
            }
            value_v_point_amount.value = Number(options_value_v_point_amount.value[options_value_v_point_amount.value.length - 1])
            amount = Number(options_value_v_point_amount.value[options_value_v_point_amount.value.length - 1])
            $store.commit("setfusion_configuration", res?.config);
            value_v.value = []
            value_v_point.value = []

            for (let index = 0; index < tmpconfig.multi_ver_density[amount].length; index++) {
              let tmp: DensityCorrectionPoint = config.multi_ver_density[amount][index];
              value_v.value.push(tmp)
              let x_y = coordinate_transformation_value_to_xy(tmp.x, tmp.y)
              let def_x_f = coordinate_transformation_value_to_xy(tmp.def_x, tmp.def_y)
              let tmp_point: DensityCorrectionPoint = {
                control_point: tmp.control_point,
                x: x_y.x,
                y: x_y.y,
                def_x: def_x_f.x,
                def_y: def_x_f.y
              }
              value_v_point.value.push(tmp_point)
            }
            options_model_v.value = 0
          });
          break;
        case 'h':
          set?.DelBlendingCtrlPoint('hor_density', row, col, Number(amount)).then((res) => {
            if (res == null || typeof (res) == "undefined") return
            let tmp = JSON.parse(res?.config);
            let fortmp = null;
            let i;
            for (i of tmp.projectors) {
              if (
                i.col === $store.getters.GetTheCurrentlySelectedCamera[1] &&
                i.row === $store.getters.GetTheCurrentlySelectedCamera[0]
              ) {
                fortmp = JSON.parse(JSON.stringify(i));
              }
            }
            ste_status.value = 1;
            tmpconfig = JSON.parse(JSON.stringify(fortmp));
            config = JSON.parse(JSON.stringify(fortmp));
            options_value_h_point_amount.value = []
            for (let key in tmpconfig.multi_hor_density) {
              options_value_h_point_amount.value.push(key)
            }
            value_h_point_amount.value = Number(options_value_h_point_amount.value[options_value_h_point_amount.value.length - 1])
            amount = Number(options_value_h_point_amount.value[options_value_h_point_amount.value.length - 1])
            //value_h_point_amount.value = amount
            $store.commit("setfusion_configuration", res?.config);
            value_h.value = []
            value_h_point.value = []
            // console.log(tmpconfig.multi_hor_density)

            for (let index = 0; index < tmpconfig.multi_hor_density[amount].length; index++) {
              let tmp: DensityCorrectionPoint = tmpconfig.multi_hor_density[amount][index];
              value_h.value.push(tmp)
              let x_y = coordinate_transformation_value_to_xy(tmp.x, tmp.y)
              let def_x_f = coordinate_transformation_value_to_xy(tmp.def_x, tmp.def_y)
              let tmp_point: DensityCorrectionPoint = {
                control_point: tmp.control_point,
                x: x_y.x,
                y: x_y.y,
                def_x: def_x_f.x,
                def_y: def_x_f.y
              }
              value_h_point.value.push(tmp_point)
            }
            options_model_h.value = 0
          });
          break;
        default:
          break;
      }

    }

    const update = (config: string) => {
      let tmp = JSON.parse($store.state.fusion_configuration);
      let fortmp = null;
      let i;
      for (i of tmp.projectors) {
        if (
          i.col === $store.getters.GetTheCurrentlySelectedCamera[1] &&
          i.row === $store.getters.GetTheCurrentlySelectedCamera[0]
        ) {
          fortmp = JSON.parse(JSON.stringify(i));
        }
      }
      ste_status.value = 1;
      config = JSON.parse(JSON.stringify(fortmp));
      use_server_config();
      use_set_cache();
      setTimeout(() => {
        ste_status.value = 0;
      }, 100);
    }
    const send_auxiliary_line = (val: boolean) => {
      set?.SetBlendingOption("blending_grids_density_assistant_lines", val ? "1" : "0");
    }
    const del = () => {
      let tmp = now_v_h.value == "vertical" ? "v" : "h"
      let amount = now_v_h.value == "vertical" ? value_v_point_amount.value : value_h_point_amount.value
      reduce_control_points(Number(amount), tmp)
    }
    const add = () => {
      let tmp = now_v_h.value == "vertical" ? "v" : "h"
      let amount = now_v_h.value == "vertical" ? value_v_point_amount.value : value_h_point_amount.value
      add_control_points(Number(amount), tmp);
    }

    /**
     *  滚轮切换
     * @param details
     */
    const details_selsect_v = (details: any) => {
      let spt = details.deltaY / 100
      if (spt > 0) {
        if (options_model_v.value >= value_v_point.value.length - 1) {
          options_model_v.value = 0
        } else {
          options_model_v.value++
        }
      } else {
        if (options_model_v.value <= 0) {
          options_model_v.value = value_v_point.value.length - 1
        } else {
          options_model_v.value--
        }
      }
    }
    const details_selsect_h = (details: any) => {
      let spt = details.deltaY / 100
      if (spt > 0) {
        if (options_model_h.value >= value_h_point.value.length - 1) {
          options_model_h.value = 0
        } else {
          options_model_h.value++
        }
      } else {
        if (options_model_h.value <= 0) {
          options_model_h.value = value_h_point.value.length - 1
        } else {
          options_model_h.value--
        }
      }
    }

    /**
     * v数据
     */

    const details_selsect_val = (details: any, type: string) => {
      let spt = details.deltaY / 100
      switch (type) {
        case 'v':
          if (spt < 0) {
            value_v.value[options_model_v.value].y++
          } else {
            value_v.value[options_model_v.value].y--
          }
          chang(options_model_v.value, value_v.value[options_model_v.value].y, "v")
          break;
        default:
          if (spt < 0) {
            value_h.value[options_model_h.value].x++
          } else {
            value_h.value[options_model_h.value].x--
          }
          chang(options_model_h.value, value_h.value[options_model_h.value].x, "h")
          break;
      }

    }

    const preview_v = (val: number) => {
      val = Number(val)
      if (val < 5) return
      let width = allconfig.projector_width / (val - 1)
      let height = allconfig.projector_height / (val - 1)
      value_v.value = []
      value_v_point.value = []
      for (let index = 0; index < val; index++) {

          let y = allconfig.projector_height - height * index
          let x = width * index
          let tmp: DensityCorrectionPoint = { x, y, def_x: x, def_y: y, control_point: index + 1 };
          value_v.value.push(tmp)
          let x_y = coordinate_transformation_value_to_xy(tmp.x, tmp.y)
          let def_x_f = coordinate_transformation_value_to_xy(tmp.def_x, tmp.def_y)
          let tmp_point: DensityCorrectionPoint = {
            control_point: tmp.control_point,
            x: x_y.x,
            y: x_y.y,
            def_x: def_x_f.x,
            def_y: def_x_f.y
          }
          value_v_point.value.push(tmp_point)

      }
      value_v_point_amount.value = val
      options_model_v.value = 1
    }
    const preview_h = (val: number) => {
      val = Number(val)
      if (val < 5) return
      let width = allconfig.projector_width / (val - 1)
      let height = allconfig.projector_height / (val - 1)
      value_h.value = []
      value_h_point.value = []
        for (let index = 0; index < val; index++) {
          let y = allconfig.projector_height - height * index
          let x = width * index
          let tmp: DensityCorrectionPoint = { x, y, def_x: x, def_y: y, control_point:index + 1 };
          value_h.value.push(tmp)
          let x_y = coordinate_transformation_value_to_xy(tmp.x, tmp.y)
          let def_x_f = coordinate_transformation_value_to_xy(tmp.def_x, tmp.def_y)
          let tmp_point: DensityCorrectionPoint = {
            control_point: tmp.control_point,
            x: x_y.x,
            y: x_y.y,
            def_x: def_x_f.x,
            def_y: def_x_f.y
          }
          value_h_point.value.push(tmp_point)
        }
      value_h_point_amount.value = val
      options_model_h.value = 1
    }
  /**
  * 在增加时 实现回退
  */
    const value_point_amount_tmp_v = ref(0)
    const value_point_amount_tmp_h = ref(0)
    defineExpose({
      now_v_h,
      options_model_v,
      options_model_h
    });
    return {
      config,
      value_point_amount_tmp_v,
      value_point_amount_tmp_h,
      preview_v,
      preview_h,
      resetall,
      div,
      max,
      point,
      options_model_v,
      options_model_h,
      value_h,
      value_v,
      value_h_point,
      value_v_point,
      dragStartHandle,
      chang,
      options_v,
      options_h,
      reset,
      options_value_h_point_amount,
      options_value_v_point_amount,
      value_h_point_amount,
      value_v_point_amount,
      chang_point_amount,
      add_control_points,
      now_v_h,
      reduce_control_points,
      send_auxiliary_line,
      auxiliary_line,
      del,
      add,
      del_dialog,
      add_dialog,
      now_amount,
      set_cache,
      resetall_all,
      details_selsect_h,
      details_selsect_v,
      details_selsect_val
    }
  }
})

</script>
