import * as Cesium from 'cesium'
import {
  createCurvePolygonPositions,
  createStraightArrowPositions,
  createWideArrowPositions,
  createAttackArrowPositions,
  createDoubleArrowPositions
} from '@/components/CesiumMap/mixins/useDrawTool';
import drawLocationIcon from '@/assets/icons/draw_location.png';

// 绘制位置
const drawLocation = (options) => {
	const { toolbarLayer, drawTool, measureTool } = options;
  if(!drawTool) {
    console.error('绘制工具未初始化');
    return;
  }

  drawTool.abort();
  measureTool.abort();
  drawTool.drawPoint().then(position => {
    toolbarLayer?.entities.add({
      position,
      billboard: {
        image: drawLocationIcon,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      }
    });
  });
};

// 绘制线
const drawPolyline = (options) => {
	const { toolbarLayer, drawTool, measureTool } = options;
  if(!drawTool) {
    console.error('绘制工具未初始化');
    return;
  }
  drawTool.abort();
  measureTool.abort();
  drawTool.drawPolyline().then((positions) => {
    toolbarLayer?.entities.add({
      polyline: {
        positions,
        width: 2,
        material: Cesium.Color.YELLOW,
        clampToGround: true,
      },
    })
  });
};

// 绘制面
const drawPolygon = (options) => {
	const { toolbarLayer, drawTool, measureTool } = options;
  if(!drawTool) {
    console.error('绘制工具未初始化');
    return;
  }
  drawTool.abort();
  measureTool.abort();
  drawTool.drawPolygon().then((positions) => {
    toolbarLayer?.entities.add({
      polyline: {
        positions:[...positions, positions[0]], // 闭合多边形
        width: 2,
        material: Cesium.Color.YELLOW,
        clampToGround: true,
      },
      polygon: {
        hierarchy: positions,
        material: Cesium.Color.YELLOW.withAlpha(0.5),
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      },
    });
  });
};

// 绘制曲面
const drawCurvePolygon = (options) => {
	const { toolbarLayer, drawTool, measureTool } = options;
  if(!drawTool) {
    console.error('绘制工具未初始化');
    return;
  }
  drawTool.abort();
  measureTool.abort();
  drawTool.drawCurvePolygon().then((positions) => {
    if (positions.length < 2 || !positions[0]) return

    const fullPositions = createCurvePolygonPositions(positions)
    toolbarLayer?.entities.add({
      polyline: {
        positions: fullPositions,
        width: 2,
        material: Cesium.Color.YELLOW,
        clampToGround: true,
      },
      polygon: {
        hierarchy: fullPositions,
        material: Cesium.Color.YELLOW.withAlpha(0.5),
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      },
    });
  });
};

// 绘制直箭头
const drawStraightArrow = (options) => {
	const { toolbarLayer, drawTool, measureTool } = options;
  if(!drawTool) {
    console.error('绘制工具未初始化');
    return;
  }
  drawTool.abort();
  measureTool.abort();
  drawTool.drawStraightArrow().then((positions) => {
    if (!positions[0] || !positions[1]) return

    const fullPositions = createStraightArrowPositions(positions)
    toolbarLayer?.entities.add({
      polyline: {
        positions: [...fullPositions, fullPositions[0]],
        width: 2,
        material: Cesium.Color.YELLOW,
        clampToGround: true,
      },
      polygon: {
        hierarchy: fullPositions,
        material: Cesium.Color.YELLOW.withAlpha(0.5),
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      },
    })
  });
};

// 绘制宽箭头
const drawWideArrow = (options) => {
	const { toolbarLayer, drawTool, measureTool } = options;
  if(!drawTool) {
    console.error('绘制工具未初始化');
    return;
  }
  drawTool.abort();
  measureTool.abort();
  drawTool.drawWideArrow().then((positions) => {
    if (positions.length < 2) return

    const fullPositions = createWideArrowPositions(positions)
    toolbarLayer?.entities.add({
      polyline: {
        positions: [...fullPositions, fullPositions[0]],
        width: 2,
        material: Cesium.Color.YELLOW,
        clampToGround: true,
      },
      polygon: {
        hierarchy: fullPositions,
        material: Cesium.Color.YELLOW.withAlpha(0.5),
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      },
    })
  });
};

// 绘制攻击箭头
const drawAttackArrow = (options) => {
	const { toolbarLayer, drawTool, measureTool } = options;
  if(!drawTool) {
    console.error('绘制工具未初始化');
    return;
  }
  drawTool.abort();
  measureTool.abort();
  drawTool.drawAttackArrow().then((positions) => {
    if (positions.length < 3) return

    const fullPositions = createAttackArrowPositions(positions)
    toolbarLayer?.entities.add({
      polyline: {
        positions: [...fullPositions, fullPositions[0]],
        width: 2,
        material: Cesium.Color.YELLOW,
        clampToGround: true,
      },
      polygon: {
        hierarchy: fullPositions,
        material: Cesium.Color.YELLOW.withAlpha(0.5),
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      },
    })
  });
};

// 绘制双箭头
const drawDoubleArrow = (options) => {
	const { toolbarLayer, drawTool, measureTool } = options;
  if(!drawTool) {
    console.error('绘制工具未初始化');
    return;
  }
  drawTool.abort();
  measureTool.abort();
  drawTool.drawDoubleArrow().then((positions) => {
    if (positions.length < 3) return

    const fullPositions = createDoubleArrowPositions(positions)
    toolbarLayer?.entities.add({
      polyline: {
        positions: [...fullPositions, fullPositions[0]],
        width: 2,
        material: Cesium.Color.YELLOW,
        clampToGround: true,
      },
      polygon: {
        hierarchy: fullPositions,
        material: Cesium.Color.YELLOW.withAlpha(0.5),
        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      },
    })
  });
};

// 平面距离测量的绘制
const drawMeasureDistance = (options) => {
	const { drawTool, measureTool } = options;
  if(!measureTool) {
    console.error('绘制工具未初始化');
    return;
  }

  drawTool.abort();
  measureTool.abort();
  measureTool.distance();
};

// 平面面积测量的绘制
const drawMeasureArea = (options) => {
	const { drawTool, measureTool } = options;
  if(!measureTool) {
    console.error('绘制工具未初始化');
    return;
  }

  drawTool.abort();
  measureTool.abort();
  measureTool.area();
};

export {
  drawLocation,
	drawPolyline,
	drawPolygon,
	drawCurvePolygon,
	drawStraightArrow,
	drawWideArrow,
	drawAttackArrow,
	drawDoubleArrow,
	drawMeasureDistance,
	drawMeasureArea
}
