import { useVueFlow } from '@vue-flow/core';
import { ref, watch } from 'vue';
import { v4 as uuidv4 } from 'uuid';


const getId = (type) => {
  return `${type}_${uuidv4().replaceAll('-', '_')}`;
};


const getOption = (e) => {
  if (e === 'line' || e === 'multiLines') {
    return { title: '', yNames: [], gridTop: 30, gridLeft: 5, gridBottom: 10, gridRight: 10, xName: '', yName: '', tooltip:true, legend:true };
  } else if (e === 'bar' || e === 'multiBars') {
    return { title: '', yNames: [], gridTop: 30, gridLeft: 5, gridBottom: 10, gridRight: 10, xName: '', yName: '', tooltip:true, legend:true };
  } else if (e === 'curve' || e === 'multiCurves') {
    return { title: '', yNames: [], gridTop: 30, gridLeft: 5, gridBottom: 10, gridRight: 10, xName: '', yName: '', tooltip:true, legend:true };
  } else if (e === 'pie') {
    return { title: '', yNames: [], tooltip:true, legend:true,label:true };
  } else if (e === 'customBoard') {
    return { title: '', yNames: [] };
  } else if (e === 'data') {
    return { timeout: 5000 };
  } else if (e === 'staticData') {
    return { defaultInputArea: '', field: 'input' };
  } else if (e === 'customData') {
    return { timeout: 5000 };
  } else if (e === 'map') {
    return { dataMap: [] };
  } else if (e === 'inputNode') {
    return { field: 'input', defaultInput: '' };
  } else if (e === 'time') {
    return { startTimeId: 'startTime', endTimeId: 'endTime', defaultTime: [], format: '' };
  } else if (e === 'text') {
    return { text: '文字', align: '', color: '#fff' };
  } else if (e === 'img') {
    return { imgSrc: '' };
  } else if (e === 'scrollTable' || e === 'table') {
    return {
      tableOptions: [],
      tableClassOption: {},
      thHeight: '20px',
      tdHeight: '20px',
      thColor: '#fff',
      tdColor: ['#fff'],
      thBgColor: '#fff',
      tdBgColor: ['#000']
    };
  } else {
    return {};
  }
};
const getNodeSize = (e) => {
  if (e === 'line' || e === 'multiLines' || e === 'bar' || e === 'multiBars' || e === 'curve' || e === 'multiCurves' || e === 'customBoard' || e === 'pie') {
    return { width: 300, height: 150 };
  } else if (e === 'data' || e === 'customData') {
    return { width: 150, height: 50 };
  } else if (e === 'inputNode') {
    return { width: 100, height: 30 };
  } else if (e === 'staticData') {
    return { width: 100, height: 60 };
  } else if (e === 'text') {
    return { width: 100, height: 30 };
  } else if (e === 'time') {
    return { width: 200, height: 30 };
  } else if (e === 'img') {
    return { width: 300, height: 300 };
  } else if (e === 'map') {
    return { width: 100, height: 30 };
  } else if (e === 'scrollTable' || e === 'table') {
    return { width: 500, height: 300 };
  } else {
    return { width: 100, height: 100 };
  }
};

const tool = () => {
  const nodeType = ref('');
  const customData = ref({ script: '' });
  const { addNodes, screenToFlowCoordinate, onNodesInitialized, updateNode } = useVueFlow();
  const onDragStart = (event, type, data) => {
    if (event.dataTransfer) {
      event.dataTransfer.setData('application/vueflow', type);
      event.dataTransfer.effectAllowed = 'move';
    }

    nodeType.value = type;
    customData.value = { ...customData.value, ...(data || {}) };
    document.addEventListener('drop', onDragEnd);
  };
  const onDragEnd = () => {
    nodeType.value = null;
    document.removeEventListener('drop', onDragEnd);
  };
  const onDrop = (event) => {
    const dimensions = getNodeSize(nodeType.value);

    const position = screenToFlowCoordinate({
      x: event.clientX, y: event.clientY
    });

    const nodeId = getId(nodeType.value);
    const newNode = {
      id: nodeId,
      name: nodeType.value,
      type: nodeType.value,
      dimensions,
      position,
      data: { options: getOption(nodeType.value), outputData: {}, customData: customData.value }
    };


    const { off } = onNodesInitialized(() => {
      updateNode(nodeId, (node) => ({
        position: { x: node.position.x - node.dimensions.width / 2, y: node.position.y - node.dimensions.height / 2 }
      }));

      off();
    });

    addNodes(newNode);

  };
  const onDragOver = (event) => {
    event.preventDefault();

    if (nodeType.value) {

      if (event.dataTransfer) {
        event.dataTransfer.dropEffect = 'move';
      }
    }
  };
  return {
    onDragStart, onDragEnd, onDrop, onDragOver
  };
};
export default tool;
export const options = {
  isD: true, isJSON: (str) => {
    if (typeof str === 'string') {
      try {
        JSON.parse(str);
        return true;
      } catch (e) {
        return false;
      }
    }
    return false;
  },getId
};
