import * as THREE from 'three';
import { Line2 } from 'three/examples/jsm/lines/Line2.js';
import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial.js';
import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry.js';

export default function useCountry() {
  /**
   * 创建国家边界线
   * @param {Object} data - GeoJSON数据
   * @param {Object} options - 线条样式选项
   * @param {string} lineType - 线条类型 ('Line', 'LineLoop', 'LineSegments', 'Line2')
   * @returns {THREE.Object3D} 线条对象
   */
  const createCountryFlatLine = (data, options = {}, lineType = 'Line') => {
    const group = new THREE.Group();
    
    if (!data || !data.features) {
      return group;
    }

    data.features.forEach(feature => {
      if (feature.geometry && feature.geometry.coordinates) {
        const coordinates = feature.geometry.coordinates;
        
        coordinates.forEach(multiPolygon => {
          multiPolygon.forEach(polygon => {
            const points = [];
            
            // 提取坐标点
            polygon.forEach(coord => {
              if (Array.isArray(coord) && coord.length >= 2) {
                points.push(new THREE.Vector3(coord[0], coord[1], 0));
              }
            });
            
            if (points.length > 0) {
              const line = createLine(points, options, lineType);
              if (line) {
                group.add(line);
              }
            }
          });
        });
      }
    });

    return group;
  };

  /**
   * 创建线条对象
   * @param {Array<THREE.Vector3>} points - 点数组
   * @param {Object} options - 样式选项
   * @param {string} lineType - 线条类型
   * @returns {THREE.Object3D} 线条对象
   */
  const createLine = (points, options = {}, lineType = 'Line') => {
    if (!points || points.length < 2) {
      return null;
    }

    const defaultOptions = {
      color: 0xffffff,
      linewidth: 0.001,
      transparent: true,
      opacity: 1,
      depthTest: true
    };

    const finalOptions = { ...defaultOptions, ...options };

    try {
      switch (lineType) {
        case 'Line2': {
          // 使用Line2创建更高质量的线条
          const geometry = new LineGeometry();
          const positions = [];
          
          points.forEach(point => {
            positions.push(point.x, point.y, point.z);
          });
          
          geometry.setPositions(positions);
          
          const material = new LineMaterial({
            color: finalOptions.color,
            linewidth: finalOptions.linewidth,
            transparent: finalOptions.transparent,
            opacity: finalOptions.opacity,
            depthTest: finalOptions.depthTest
          });
          
          // 设置分辨率
          material.resolution.set(window.innerWidth, window.innerHeight);
          
          return new Line2(geometry, material);
        }
        
        case 'LineLoop': {
          const geometry = new THREE.BufferGeometry().setFromPoints(points);
          const material = new THREE.LineBasicMaterial({
            color: finalOptions.color,
            transparent: finalOptions.transparent,
            opacity: finalOptions.opacity,
            depthTest: finalOptions.depthTest
          });
          return new THREE.LineLoop(geometry, material);
        }
        
        case 'LineSegments': {
          const geometry = new THREE.BufferGeometry().setFromPoints(points);
          const material = new THREE.LineBasicMaterial({
            color: finalOptions.color,
            transparent: finalOptions.transparent,
            opacity: finalOptions.opacity,
            depthTest: finalOptions.depthTest
          });
          return new THREE.LineSegments(geometry, material);
        }
        
        default: // 'Line'
        case 'Line': {
          const geometry = new THREE.BufferGeometry().setFromPoints(points);
          const material = new THREE.LineBasicMaterial({
            color: finalOptions.color,
            transparent: finalOptions.transparent,
            opacity: finalOptions.opacity,
            depthTest: finalOptions.depthTest
          });
          return new THREE.Line(geometry, material);
        }
      }
    } catch (error) {
      console.error('Error creating line:', error);
      return null;
    }
  };

  return {
    createCountryFlatLine,
    createLine
  };
}