<template>
  <div
    class="go-text-box"
    @click="clickBtn"
    @dblclick="dblclickBtn"
    @contextmenu="rightclickBtn"
    @mouseenter="mouseenterBtn"
    @mouseleave="mouseleaveBtn"
  >
    <n-gradient-text :size="size" :gradient="gradient">
      {{ option.dataset }}
    </n-gradient-text>
  </div>
</template>
<script setup lang="ts">
  import { PropType, toRefs, shallowReactive, watch, onMounted } from 'vue';
  import { CreateComponentType } from '@/packages/index.d';
  import { useChartDataFetch } from '@/hooks';
  import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore';
  import { option as configOption } from './config';
  import { eventHandlerHook } from '@/hooks/eventHandler.hook';
  import { EventBus } from '@/utils/eventBus';

  const chartEditStore = useChartEditStore();
  const props = defineProps({
    chartConfig: {
      type: Object as PropType<CreateComponentType>,
      required: true,
    },
  });

  const option = shallowReactive({
    dataset: configOption.dataset,
  });

  const { w, h } = toRefs(props.chartConfig.attr);
  const { size, gradient } = toRefs(props.chartConfig.option);

  watch(
    () => props.chartConfig.option.dataset,
    (newData: any) => {
      option.dataset = newData;
    },
    {
      immediate: true,
      deep: false,
    },
  );

  useChartDataFetch(props.chartConfig, useChartEditStore, (newData: any) => {
    option.dataset = newData;
  });

  const clickBtn = (val) => {
    eventHandlerHook(
      chartEditStore.getComponentList,
      props.chartConfig.events.interactConfigEvents,
      'click',
      val,
    );
  };
  const dblclickBtn = (val) => {
    eventHandlerHook(
      chartEditStore.getComponentList,
      props.chartConfig.events.interactConfigEvents,
      'dblclick',
      val,
    );
  };
  const rightclickBtn = (event) => {
    event.preventDefault(); // 阻止默认的右键菜单
    eventHandlerHook(
      chartEditStore.getComponentList,
      props.chartConfig.events.interactConfigEvents,
      'rightclick',
    );
  };
  const mouseenterBtn = (val) => {
    eventHandlerHook(
      chartEditStore.getComponentList,
      props.chartConfig.events.interactConfigEvents,
      'mousein',
      val,
    );
  };
  const mouseleaveBtn = (val) => {
    eventHandlerHook(
      chartEditStore.getComponentList,
      props.chartConfig.events.interactConfigEvents,
      'mouseout',
      val,
    );
  };
  onMounted(() => {
    // 获取eventBus传过来的值
    EventBus.on(props.chartConfig.id + 'dataupdate', (data) => {
      console.log('data', data);
    });
    // websocket传过来的值
    EventBus.on(props.chartConfig.id + 'websocket', (data) => {
      console.log('data', data);
    });
  });
</script>

<style lang="scss" scoped>
  @include go('text-box') {
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    .n-gradient-text {
      white-space: initial;
    }
  }
</style>
