import { LayerStyleState, StyleConfig, DefaultLayerStyleId } from "./styleEditorTypes";

export const SINGLE_COLOR_PALETTES = [
  { color: "rgba(51, 153, 204, 1)" },
  { color: "rgba(255, 138, 92, 1)" },
  { color: "rgba(204, 51, 51, 1)" },
  { color: "rgba(255, 235, 59, 1)" },
  { color: "rgba(44, 160, 44, 1)" },
  { color: "rgba(227, 119, 194, 1)" },
  { color: "rgba(148, 103, 189, 1)" },
];

export const GRADIENT_PALETTES = [
  {
    name: "蓝-红",
    start: "rgba(51, 153, 204, 1)",
    end: "rgba(204, 51, 51, 1)",
  },
  {
    name: "黄-绿",
    start: "rgba(255, 235, 59, 1)",
    end: "rgba(44, 160, 44, 1)",
  },
  {
    name: "粉-紫",
    start: "rgba(227, 119, 194, 1)",
    end: "rgba(148, 103, 189, 1)",
  },
];

export const RAINBOW_PALETTES = [
  {
    name: "正向彩虹",
    colors: [
      "rgba(255, 0, 0, 1)",
      "rgba(255, 127, 0, 1)",
      "rgba(255, 215, 0, 1)",
      "rgba(199, 224, 0, 1)",
      "rgba(76, 175, 80, 1)",
      "rgba(0, 158, 115, 1)",
      "rgba(0, 188, 212, 1)",
      "rgba(33, 150, 243, 1)",
      "rgba(63, 81, 181, 1)",
      "rgba(142, 68, 173, 1)",
    ],
  },
  {
    name: "反向彩虹",
    colors: [
      "rgba(142, 68, 173, 1)",
      "rgba(63, 81, 181, 1)",
      "rgba(33, 150, 243, 1)",
      "rgba(0, 188, 212, 1)",
      "rgba(0, 158, 115, 1)",
      "rgba(76, 175, 80, 1)",
      "rgba(199, 224, 0, 1)",
      "rgba(255, 215, 0, 1)",
      "rgba(255, 127, 0, 1)",
      "rgba(255, 0, 0, 1)",
    ],
  },
];

export const CLASSIFICATION_METHODS = [
  { name: "优雅分段", value: "pretty_breaks" },
  { name: "自定义", value: "custom_breaks" },
];

export const COLOR_TYPE_OPTIONS = [
  { label: "单一色", value: "single" },
  { label: "渐进色", value: "gradient" },
  { label: "离散彩虹", value: "rainbow" },
  { label: "自定义", value: "custom" },
];

const DEFAULT_LAYER_STYLE_PRESETS: Record<
  DefaultLayerStyleId,
  Omit<LayerStyleState, "isActive">
> = {
  junctions: {
    layerId: "junctions",
    layerName: "节点",
    styleConfig: {
      property: "pressure",
      classificationMethod: "custom_breaks",
      customBreaks: [16, 18, 20, 22, 24, 26, 28],
      customColors: [
        "rgba(255, 0, 0, 1)",
        "rgba(255, 127, 0, 1)",
        "rgba(255, 215, 0, 1)",
        "rgba(199, 224, 0, 1)",
        "rgba(76, 175, 80, 1)",
        "rgba(0, 158, 115, 1)",
      ],
      segments: 6,
      minSize: 4,
      maxSize: 12,
      minStrokeWidth: 2,
      maxStrokeWidth: 8,
      fixedStrokeWidth: 3,
      colorType: "rainbow",
      singlePaletteIndex: 0,
      gradientPaletteIndex: 0,
      rainbowPaletteIndex: 0,
      showLabels: true,
      showId: false,
      opacity: 0.9,
      adjustWidthByProperty: true,
    },
    legendConfig: {
      layerId: "junctions",
      layerName: "节点",
      property: "压力",
      colors: [],
      type: "point",
      dimensions: [],
      breaks: [],
    },
  },
  pipes: {
    layerId: "pipes",
    layerName: "管道",
    styleConfig: {
      property: "velocity",
      classificationMethod: "custom_breaks",
      segments: 6,
      minSize: 4,
      maxSize: 12,
      minStrokeWidth: 2,
      maxStrokeWidth: 8,
      fixedStrokeWidth: 3,
      colorType: "gradient",
      singlePaletteIndex: 0,
      gradientPaletteIndex: 0,
      rainbowPaletteIndex: 0,
      showLabels: true,
      showId: false,
      opacity: 0.9,
      adjustWidthByProperty: true,
      customBreaks: [0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4],
      customColors: [],
    },
    legendConfig: {
      layerId: "pipes",
      layerName: "管道",
      property: "流速",
      colors: [],
      type: "linestring",
      dimensions: [],
      breaks: [],
    },
  },
};

export const createEmptyStyleConfig = (): StyleConfig => ({
  property: "",
  classificationMethod: "pretty_breaks",
  segments: 5,
  minSize: 4,
  maxSize: 12,
  minStrokeWidth: 2,
  maxStrokeWidth: 6,
  fixedStrokeWidth: 3,
  colorType: "single",
  singlePaletteIndex: 0,
  gradientPaletteIndex: 0,
  rainbowPaletteIndex: 0,
  showLabels: false,
  showId: false,
  opacity: 0.9,
  adjustWidthByProperty: true,
  customBreaks: [],
  customColors: [],
});

export const createDefaultLayerStyleState = (
  layerId: DefaultLayerStyleId
): LayerStyleState => {
  const preset = DEFAULT_LAYER_STYLE_PRESETS[layerId];
  return {
    ...preset,
    styleConfig: {
      ...preset.styleConfig,
      customBreaks: [...(preset.styleConfig.customBreaks || [])],
      customColors: [...(preset.styleConfig.customColors || [])],
    },
    legendConfig: {
      ...preset.legendConfig,
      colors: [...preset.legendConfig.colors],
      dimensions: [...preset.legendConfig.dimensions],
      breaks: [...preset.legendConfig.breaks],
    },
    isActive: false,
  };
};

export const createDefaultLayerStyleStates = (): LayerStyleState[] => [
  createDefaultLayerStyleState("junctions"),
  createDefaultLayerStyleState("pipes"),
];
