import React, { useState, useEffect, useCallback, useRef } from "react";
import ToolbarButton from "@components/olmap/common/ToolbarButton";

// 导入Material-UI图标
import BackHandOutlinedIcon from "@mui/icons-material/BackHandOutlined";
import BorderColorOutlinedIcon from "@mui/icons-material/BorderColorOutlined";
import MoreHorizOutlinedIcon from "@mui/icons-material/MoreHorizOutlined";
import TimelineIcon from "@mui/icons-material/Timeline";
import CircleOutlinedIcon from "@mui/icons-material/CircleOutlined";
import CheckBoxOutlineBlankIcon from "@mui/icons-material/CheckBoxOutlineBlank";
import GestureIcon from "@mui/icons-material/Gesture";
import UndoIcon from "@mui/icons-material/Undo";
import RedoIcon from "@mui/icons-material/Redo";
import DeleteIcon from "@mui/icons-material/Delete";
import SaveIcon from "@mui/icons-material/Save";

// 导入OpenLayers绘图相关模块
import Draw, {
  DrawEvent,
  createBox,
  GeometryFunction,
} from "ol/interaction/Draw";
import VectorSource from "ol/source/Vector";
import VectorLayer from "ol/layer/Vector";
import { Style, Stroke, Fill, Circle } from "ol/style";
import { Geometry } from "ol/geom";
import Feature from "ol/Feature";
import { Type as GeometryType } from "ol/geom/Geometry";
import { useMap } from "../MapComponent";

const DrawPanel: React.FC = () => {
  const map = useMap();
  const [activeTool, setActiveTool] = useState<string>("pan");
  const drawLayerRef = useRef<VectorLayer<VectorSource> | null>(null);
  const [drawnFeatures, setDrawnFeatures] = useState<Feature<Geometry>[]>([]);
  const [history, setHistory] = useState<{
    stack: Feature<Geometry>[][];
    index: number;
  }>({
    stack: [[]],
    index: 0,
  });

  const drawInteractionRef = useRef<Draw | null>(null);

  // 创建并添加绘图图层
  useEffect(() => {
    if (!map) return;
    const drawSource = new VectorSource();
    const drawVectorLayer = new VectorLayer({
      source: drawSource,
      style: new Style({
        stroke: new Stroke({
          color: `rgba(255, 152, 0, 0.9)`,
          width: 2,
        }),
        fill: new Fill({
          color: `rgba(255, 152, 0, 0.3)`,
        }),
        image: new Circle({
          radius: 7,
          stroke: new Stroke({
            color: `rgba(255, 152, 0, 0.9)`,
            width: 2,
          }),
          fill: new Fill({
            color: `rgba(255, 152, 0, 0.3)`,
          }),
        }),
      }),
      properties: {
        name: "绘制图层", // 设置图层名称
        value: "drawLayer",
        type: "multigeometry",
        properties: [],
      },
    });

    map.addLayer(drawVectorLayer);
    drawLayerRef.current = drawVectorLayer;

    return () => {
      if (drawInteractionRef.current && map) {
        map.removeInteraction(drawInteractionRef.current);
        drawInteractionRef.current = null;
      }
      drawLayerRef.current = null;
      map.removeLayer(drawVectorLayer);
    };
  }, [map, drawInteractionRef]);

  // 保存到历史记录
  const saveToHistory = useCallback(
    (features: Feature<Geometry>[]) => {
      setHistory((prev) => {
        const newStack = prev.stack.slice(0, prev.index + 1);
        newStack.push([...features]);
        return {
          stack: newStack,
          index: newStack.length - 1,
        };
      });
    },
    []
  );

  // 添加绘图交互
  const addDrawInteraction = (
    type: GeometryType,
    geometryFunction?: GeometryFunction
  ) => {
    const drawLayer = drawLayerRef.current;
    if (!drawLayer) return;
    if (!map) return;

    // 清除现有的绘图交互
    if (drawInteractionRef.current && map) {
      map.removeInteraction(drawInteractionRef.current);
    }

    const source = drawLayer.getSource();
    if (!source) return;

    const drawOptions: {
      source: VectorSource;
      type: GeometryType;
      style: Style;
      geometryFunction?: GeometryFunction;
    } = {
      source: source,
      type: type,
      style: new Style({
        stroke: new Stroke({
          color: `rgba(255, 152, 0, 0.9)`,
          width: 2,
        }),
        fill: new Fill({
          color: `rgba(255, 152, 0, 0.3)`,
        }),
        image: new Circle({
          radius: 7,
          stroke: new Stroke({
            color: `rgba(255, 152, 0, 0.9)`,
            width: 2,
          }),
          fill: new Fill({
            color: `rgba(255, 152, 0, 0.3)`,
          }),
        }),
      }),
    };

    // 如果有几何函数，添加它
    if (geometryFunction) {
      drawOptions.geometryFunction = geometryFunction;
    }

    const draw = new Draw(drawOptions);

    // 绘图完成事件
    draw.on("drawend", (event: DrawEvent) => {
      const feature = event.feature;
      const currentFeatures = [...source.getFeatures()];

      // Fallback in case feature has not been synced to source yet.
      if (!currentFeatures.includes(feature)) {
        currentFeatures.push(feature);
      }

      setDrawnFeatures(currentFeatures);
      saveToHistory(currentFeatures);
    });

    map.addInteraction(draw);
    drawInteractionRef.current = draw;
  };

  // 处理工具点击
  const handleToolClick = (tool: string) => {
    // 如果点击的是当前激活的工具，则取消激活
    // console.log("当前激活的工具:", activeTool);
    // console.log("点击的工具:", tool);
    if (activeTool === tool) {
      setActiveTool("");
      if (drawInteractionRef.current && map) {
        map.removeInteraction(drawInteractionRef.current);
        drawInteractionRef.current = null;
      }
      return;
    }
    if (
      tool !== "undo" &&
      tool !== "redo" &&
      tool !== "delete" &&
      tool !== "save"
    ) {
      setActiveTool(tool);
    }
    // 根据工具类型处理不同的交互
    switch (tool) {
      case "pan":
        // 平移地图，移除所有绘图交互
        if (drawInteractionRef.current && map) {
          map.removeInteraction(drawInteractionRef.current);
          drawInteractionRef.current = null;
        }
        break;
      case "select":
        // 选定要素，移除所有绘图交互
        if (drawInteractionRef.current && map) {
          map.removeInteraction(drawInteractionRef.current);
          drawInteractionRef.current = null;
        }
        break;
      case "edit":
        // 编辑要素，移除所有绘图交互
        if (drawInteractionRef.current && map) {
          map.removeInteraction(drawInteractionRef.current);
          drawInteractionRef.current = null;
        }
        break;
      case "point":
        addDrawInteraction("Point");
        break;
      case "line":
        addDrawInteraction("LineString");
        break;
      case "circle":
        addDrawInteraction("Circle");
        break;
      case "box":
        // 使用矩形绘制函数
        addDrawInteraction("Circle", createBox());
        break;
      case "polygon":
        addDrawInteraction("Polygon");
        break;
      case "undo":
        handleUndo();
        break;
      case "redo":
        handleRedo();
        break;
      case "delete":
        handleDelete();
        break;
      case "save":
        handleSave();
        break;
      default:
        if (drawInteractionRef.current && map) {
          map.removeInteraction(drawInteractionRef.current);
          drawInteractionRef.current = null;
        }
    }
  };

  // 撤销功能
  const handleUndo = () => {
    if (history.index > 0) {
      const newIndex = history.index - 1;
      const previousFeatures = history.stack[newIndex];
      updateDrawLayer(previousFeatures);
      setDrawnFeatures(previousFeatures);
      setHistory((prev) => ({
        ...prev,
        index: newIndex,
      }));
    }
  };

  // 重做功能
  const handleRedo = () => {
    if (history.index < history.stack.length - 1) {
      const newIndex = history.index + 1;
      const nextFeatures = history.stack[newIndex];
      updateDrawLayer(nextFeatures);
      setDrawnFeatures(nextFeatures);
      setHistory((prev) => ({
        ...prev,
        index: newIndex,
      }));
    }
  };

  // 删除所有绘制的要素
  const handleDelete = () => {
    const drawLayer = drawLayerRef.current;
    if (!drawLayer) return;

    const source = drawLayer.getSource();
    if (source) {
      source.clear();
      const emptyFeatures: Feature<Geometry>[] = [];
      setDrawnFeatures(emptyFeatures);
      saveToHistory(emptyFeatures);
    }
  };

  // 保存绘制的要素
  const handleSave = () => {};

  // 更新绘图图层
  const updateDrawLayer = (features: Feature<Geometry>[]) => {
    const drawLayer = drawLayerRef.current;
    if (!drawLayer) return;

    const source = drawLayer.getSource();
    if (source) {
      source.clear();
      source.addFeatures(features);
    }
  };

  // 判断按钮是否应该禁用
  const isUndoDisabled = history.index <= 0;
  const isRedoDisabled = history.index >= history.stack.length - 1;
  const isDeleteDisabled = drawnFeatures.length === 0;
  const isSaveDisabled = drawnFeatures.length === 0;

  return (
    <div className="absolute top-20 left-4 bg-white p-1 rounded-xl shadow-lg flex flex-col opacity-85 hover:opacity-100 transition-opacity z-1300">
      <div className="flex">
        <ToolbarButton
          icon={<BackHandOutlinedIcon />}
          name="平移地图"
          isActive={activeTool === "pan"}
          onClick={() => handleToolClick("pan")}
        />
        <ToolbarButton
          icon={<BorderColorOutlinedIcon />}
          name="矢量编辑"
          isActive={activeTool === "edit"}
          onClick={() => handleToolClick("edit")}
        />
        <ToolbarButton
          icon={<MoreHorizOutlinedIcon />}
          name="绘制点"
          isActive={activeTool === "point"}
          onClick={() => handleToolClick("point")}
        />
        <ToolbarButton
          icon={<TimelineIcon />}
          name="绘制线"
          isActive={activeTool === "line"}
          onClick={() => handleToolClick("line")}
        />
        <ToolbarButton
          icon={<CircleOutlinedIcon />}
          name="绘制圆"
          isActive={activeTool === "circle"}
          onClick={() => handleToolClick("circle")}
        />
        <ToolbarButton
          icon={<CheckBoxOutlineBlankIcon />}
          name="绘制框"
          isActive={activeTool === "box"}
          onClick={() => handleToolClick("box")}
        />
        <ToolbarButton
          icon={<GestureIcon />}
          name="绘制多边形"
          isActive={activeTool === "polygon"}
          onClick={() => handleToolClick("polygon")}
        />
      </div>
      <div className="flex mt-1 border-t-1 pt-1">
        <ToolbarButton
          icon={<UndoIcon />}
          name="撤销"
          isActive={false}
          onClick={() => handleToolClick("undo")}
          disabled={isUndoDisabled}
        />
        <ToolbarButton
          icon={<RedoIcon />}
          name="重做"
          isActive={false}
          onClick={() => handleToolClick("redo")}
          disabled={isRedoDisabled}
        />
        <ToolbarButton
          icon={<DeleteIcon />}
          name="删除"
          isActive={false}
          onClick={() => handleToolClick("delete")}
          disabled={isDeleteDisabled}
        />
        {/* <ToolbarButton
          icon={<SaveIcon />}
          name="保存"
          isActive={false}
          onClick={() => handleToolClick("save")}
          disabled={isSaveDisabled}
        /> */}
      </div>
    </div>
  );
};

export default DrawPanel;
