export const HONGYUAN_BOUNDS = {
  west: 101.852492,
  south: 31.851389,
  east: 103.347518,
  north: 33.319796,
}

export const WMS_IMAGE_SIZE = {
  width: 2400,
  height: 2400,
}

export const SATELLITE_TEXTURE_ZOOM = 9

export const NATIONAL_PARK_ZONING_COLORS = {
  core: {
    fillColor: "#24F6C4",
    strokeColor: "#B7FFF2",
  },
  general: {
    fillColor: "#FFE06B",
    strokeColor: "#FFF2A0",
  },
}

function escapeCqlLiteral(value) {
  return String(value).replace(/'/g, "''")
}

function createEqualityCql(field, value) {
  return `${field} = '${escapeCqlLiteral(value)}'`
}

const grasslandTypeLegends = [
  { name: "低地草甸类", fillColor: "#FFFFFF", strokeColor: "#FFFFFF" },
  { name: "莎草、杂类草型", fillColor: "#F67A7A", strokeColor: "#F67A7A" },
  { name: "山地草甸类", fillColor: "#FFFFFF", strokeColor: "#FFFFFF" },
  { name: "羊茅、杂类草型", fillColor: "#E69800", strokeColor: "#E69800" },
  { name: "早熟禾、杂类草型", fillColor: "#FFD380", strokeColor: "#FFD380" },
  { name: "垂穗披碱草型", fillColor: "#FFAA01", strokeColor: "#FFAA01" },
  { name: "苔草、杂类草型", fillColor: "#FFEBB0", strokeColor: "#FFEBB0" },
  { name: "委陵菜、杂类草型", fillColor: "#F5CA7B", strokeColor: "#F5CA7B" },
  { name: "披碱草型", fillColor: "#A87001", strokeColor: "#A87001" },
  { name: "高寒草甸类", fillColor: "#FFFFFF", strokeColor: "#FFFFFF" },
  { name: "矮生嵩草、杂类草型", fillColor: "#6CB3FF", strokeColor: "#6CB3FF" },
  { name: "矮生嵩草、圆穗蓼型", fillColor: "#00C7FF", strokeColor: "#00C7FF" },
  { name: "西藏嵩草、杂类草型", fillColor: "#8FE5FD", strokeColor: "#8FE5FD" },
  { name: "西藏嵩草、甘肃嵩草型", fillColor: "#7B8FED", strokeColor: "#7B8FED" },
  { name: "高山嵩草、矮生嵩草型", fillColor: "#71DDFE", strokeColor: "#71DDFE" },
  { name: "高山嵩草、苔草型", fillColor: "#7BB5F3", strokeColor: "#7BB5F3" },
  { name: "高山嵩草、杂类草型", fillColor: "#444E87", strokeColor: "#444E87" },
  { name: "具灌木的矮生嵩草型", fillColor: "#6873D1", strokeColor: "#6873D1" },
].map((item, index) => ({
  ...item,
  key: `grassland-type-${index + 1}`,
  cqlFilter: createEqualityCql("grass_min_type", item.name),
  rankingPropertyName: ["area_code", "acreage"],
  rankingTownCodeKeys: ["area_code"],
  rankingAreaKeys: ["acreage"],
}))

const productivityLegends = [
  { name: ">4000(kg/hm2)", fillColor: "#39A700", strokeColor: "#39A700" },
  { name: "3000-4000(kg/hm2)", fillColor: "#8CCF00", strokeColor: "#8CCF00" },
  { name: "2000-3000(kg/hm2)", fillColor: "#FEFE02", strokeColor: "#FEFE02" },
  { name: "1500-2000(kg/hm2)", fillColor: "#FE7E01", strokeColor: "#FE7E01" },
  { name: "<1500(kg/hm2)", fillColor: "#FB0100", strokeColor: "#FB0100" },
].map((item) => ({ ...item, selectable: false }))

const grasslandLevelText = {
  1: "一级",
  2: "二级",
  3: "三级",
  4: "四级",
  5: "五级",
  6: "六级",
  7: "七级",
  8: "八级",
}

const grasslandLevelLegends = [
  { level: "1", name: "1级草原", fillColor: "#81D10B", strokeColor: "#81D10B" },
  { level: "2", name: "2级草原", fillColor: "#8AE611", strokeColor: "#8AE611" },
  { level: "3", name: "3级草原", fillColor: "#79C801", strokeColor: "#79C801" },
  { level: "4", name: "4级草原", fillColor: "#D0EC03", strokeColor: "#D0EC03" },
  { level: "5", name: "5级草原", fillColor: "#FFCC00", strokeColor: "#FFCC00" },
  { level: "6", name: "6级草原", fillColor: "#FF6501", strokeColor: "#FF6501" },
  { level: "7", name: "7级草原", fillColor: "#FB0002", strokeColor: "#FB0002" },
  { level: "8", name: "8级草原", fillColor: "#D71345", strokeColor: "#D71345" },
].map((item) => ({
  ...item,
  key: `grassland-level-${item.level}`,
  cqlFilter: `grassland_level IN ('${item.level}','${item.name}','${grasslandLevelText[item.level]}草原','${grasslandLevelText[item.level]}草地')`,
  rankingPropertyName: ["area_code", "acreage"],
  rankingTownCodeKeys: ["area_code"],
  rankingAreaKeys: ["acreage"],
  level: undefined,
}))

const wetlandLevelLegends = grasslandLevelLegends.map((item) => {
  const level = item.key.replace("grassland-level-", "")
  const name = item.name.replace("草原", "湿地")
  return {
    ...item,
    key: `wetland-level-${level}`,
    name,
    cqlFilter: `grassland_level IN ('${level}','${name}','${item.name}','${grasslandLevelText[level]}湿地','${grasslandLevelText[level]}草原','${grasslandLevelText[level]}草地')`,
  }
})

const basicGrasslandLegends = [
  {
    key: "basic-grassland-basic",
    name: "基本草原",
    fillColor: "#2EEA78",
    strokeColor: "#18F8FF",
    cqlFilter: "(grassland_category_code = 'BASIC' OR grassland_category_name = '基本草原')",
    rankingValueKey: "basicAreaWanMu",
  },
  {
    key: "basic-grassland-general",
    name: "一般草原",
    fillColor: "#FFD15A",
    strokeColor: "#18F8FF",
    cqlFilter: "(grassland_category_code = 'GENERAL' OR grassland_category_name = '一般草原')",
    rankingValueKey: "generalAreaWanMu",
  },
]

export const forestGrassWetTopics = [
  {
    key: "grassland",
    name: "草地资源",
    color: "#8AE611",
    defaultLayerKey: "grassland-distribution",
    coreGroups: ["grassLand", "grassland"],
  },
  {
    key: "wetland",
    name: "湿地资源",
    color: "#33A3DC",
    defaultLayerKey: "wetland-distribution",
    coreGroups: ["wetland"],
  },
  {
    key: "woodland",
    name: "林地资源",
    color: "#2BD17E",
    defaultLayerKey: "woodland-distribution",
    coreGroups: ["woodland"],
  },
]

export const ecologicalProtectionTopics = [
  {
    key: "national-park",
    name: "若尔盖国家公园",
    color: "#8AE611",
    defaultLayerKey: "national-park-zoning",
  },
  {
    key: "restoration",
    name: "生态修复工程",
    color: "#33A3DC",
    defaultLayerKey: "restoration-projects",
  },
  {
    key: "engineering-analysis",
    name: "工程分析",
    color: "#24F6C4",
    defaultLayerKey: "engineering-analysis-projects",
  },
]

export const yakIndustryChainTopics = [
  {
    key: "yak-industry-chain",
    name: "牦牛产业链",
    color: "#24F6C4",
    defaultLayerKey: "yak-industry-points",
  },
]

export const forestGrassWetLayers = {
  woodland: [
    {
      key: "woodland-distribution",
      name: "林地范围",
      layerName: "ne:t_woodland_2023",
      styles: "woodland_distribution",
      opacity: 0.82,
      legends: [{ key: "woodland-distribution-all", name: "林地范围", fillColor: "#2BD17E", strokeColor: "#2BD17E", selectable: false }],
    },
    {
      key: "woodland-protection",
      name: "公益林",
      layerName: "ne:t_woodland_2023",
      styles: "woodland_protection",
      opacity: 0.86,
      legends: [
        { key: "woodland-protection-level-1", name: "国家一级公益林", fillColor: "#08C4C5", strokeColor: "#08C4C5", cqlFilter: createEqualityCql("gjgyl_bhdj", "1") },
        { key: "woodland-protection-level-2", name: "国家二级公益林", fillColor: "#2BD17E", strokeColor: "#2BD17E", cqlFilter: createEqualityCql("gjgyl_bhdj", "2") },
        { key: "woodland-protection-unset", name: "未划定", fillColor: "#6B8191", strokeColor: "#6B8191", cqlFilter: "(gjgyl_bhdj IS NULL OR gjgyl_bhdj = '' OR gjgyl_bhdj NOT IN ('1','2'))" },
      ],
    },
    {
      key: "woodland-type",
      name: "林地类型",
      layerName: "ne:t_woodland_2023",
      styles: "woodland_type",
      opacity: 0.84,
      legends: [
        { key: "woodland-type-shrub", name: "灌木林地", fillColor: "#2BD17E", strokeColor: "#2BD17E", cqlFilter: createEqualityCql("dlmc", "灌木林地") },
        { key: "woodland-type-arbor", name: "乔木林地", fillColor: "#0DA368", strokeColor: "#0DA368", cqlFilter: createEqualityCql("dlmc", "乔木林地") },
        { key: "woodland-type-swamp", name: "灌丛沼泽", fillColor: "#23C7B7", strokeColor: "#23C7B7", cqlFilter: createEqualityCql("dlmc", "灌丛沼泽") },
        { key: "woodland-type-other", name: "其他林地", fillColor: "#B7D96B", strokeColor: "#B7D96B", cqlFilter: "(dlmc IS NULL OR dlmc = '' OR dlmc NOT IN ('灌木林地','乔木林地','灌丛沼泽'))" },
      ],
    },
    {
      key: "woodland-quality",
      name: "林地质量",
      layerName: "ne:t_woodland_2023",
      styles: "woodland_quality",
      opacity: 0.84,
      legends: [
        { key: "woodland-quality-2", name: "二级质量", fillColor: "#08C4C5", strokeColor: "#08C4C5", cqlFilter: createEqualityCql("zl_dj", "2") },
        { key: "woodland-quality-3", name: "三级质量", fillColor: "#2BD17E", strokeColor: "#2BD17E", cqlFilter: createEqualityCql("zl_dj", "3") },
        { key: "woodland-quality-4", name: "四级质量", fillColor: "#23C7B7", strokeColor: "#23C7B7", cqlFilter: createEqualityCql("zl_dj", "4") },
        { key: "woodland-quality-5", name: "五级质量", fillColor: "#B7D96B", strokeColor: "#B7D96B", cqlFilter: createEqualityCql("zl_dj", "5") },
        { key: "woodland-quality-unset", name: "未划分", fillColor: "#6B8191", strokeColor: "#6B8191", cqlFilter: "(zl_dj IS NULL OR zl_dj = '' OR zl_dj NOT IN ('2','3','4','5'))" },
      ],
    },
    {
      key: "woodland-ownership",
      name: "林地权属",
      layerName: "ne:t_woodland_2023",
      styles: "woodland_ownership",
      opacity: 0.84,
      legends: [
        { key: "woodland-ownership-collective", name: "集体林地", fillColor: "#2BD17E", strokeColor: "#2BD17E", cqlFilter: createEqualityCql("ld_qs", "10") },
        { key: "woodland-ownership-other", name: "国有或其他", fillColor: "#08C4C5", strokeColor: "#08C4C5", cqlFilter: "(ld_qs IS NULL OR ld_qs = '' OR ld_qs <> '10')" },
      ],
    },
  ],
  grassland: [
    {
      key: "grassland-distribution",
      name: "草地分布",
      layerName: "ne:t_grassland_result_caodi",
      opacity: 0.82,
      legends: [{ name: "草地", fillColor: "#1D953F", strokeColor: "#1D953F" }],
    },
    {
      key: "basic-grassland",
      name: "基本/一般草原",
      layerName: "ne:t_basic_grassland_2023",
      styles: "basic_grassland_type",
      opacity: 0.84,
      legends: basicGrasslandLegends,
    },
    {
      key: "grassland-level",
      name: "草原级",
      layerName: "ne:t_grassland_result_level_caodi",
      opacity: 0.82,
      legends: grasslandLevelLegends,
    },
    {
      key: "grassland-type",
      name: "草原类型",
      layerName: "ne:t_grassland_result_grass_min_type_caodi",
      opacity: 0.82,
      legends: grasslandTypeLegends,
    },
    {
      key: "grassland-yield",
      name: "草地产草量/生产力",
      layerName: "ne:t_grassland_result_yield_caodi",
      opacity: 0.82,
      legends: productivityLegends,
    },
    {
      key: "grassland-health",
      name: "草地健康/退化",
      layerName: "ne:2023_08_ndvi",
      opacity: 0.78,
      legends: [
        { name: "非常差", fillColor: "#FF000533", strokeColor: "#FF0005", selectable: false },
        { name: "差", fillColor: "#D8EE8C33", strokeColor: "#D8EE8C", selectable: false },
        { name: "正常", fillColor: "#78C96233", strokeColor: "#78C962", selectable: false },
        { name: "较好", fillColor: "#25814933", strokeColor: "#258149", selectable: false },
      ],
    },
  ],
  wetland: [
    {
      key: "wetland-distribution",
      name: "湿地分布",
      layerName: "ne:t_grassland_result_shidi",
      opacity: 0.84,
      legends: [{ name: "湿地", fillColor: "#33A3DC", strokeColor: "#33A3DC" }],
    },
    {
      key: "wetland-level",
      name: "湿地级",
      layerName: "ne:t_grassland_result_level_shidi",
      opacity: 0.82,
      legends: wetlandLevelLegends,
    },
    {
      key: "wetland-type",
      name: "湿地类型",
      layerName: "ne:t_grassland_result_grass_min_type_shidi",
      opacity: 0.82,
      legends: grasslandTypeLegends,
    },
    {
      key: "wetland-yield",
      name: "湿地产草量/生产力",
      layerName: "ne:t_grassland_result_yield_shidi",
      opacity: 0.82,
      legends: productivityLegends,
    },
  ],
}

export const ecologicalProtectionLayers = {
  "national-park": [
    {
      key: "national-park-zoning",
      name: "国家公园分区",
      sourceType: "wkt-api",
      apiUrl: "/openApi/gjgy/gjgy_area",
      fallbackPath: "datas/ecological-protection/national_park_wkt.json",
      clipToHongyuanBoundary: true,
      opacity: 0.9,
      clipBounds: {
        west: 101.852492,
        south: 32.757724488,
        east: 103.227028864,
        north: 33.319796,
      },
      mapView: {
        targetLngLat: [102.74, 33.12],
        distanceScale: 0.68,
        targetLocalZ: 0.78,
        duration: 0.95,
      },
      legends: [
        {
          key: "national-park-core",
          name: "核心保护区",
          fillColor: NATIONAL_PARK_ZONING_COLORS.core.fillColor,
          strokeColor: NATIONAL_PARK_ZONING_COLORS.core.strokeColor,
          match: { field: "type", value: "核心保护区" },
          selectable: false,
        },
        {
          key: "national-park-general",
          name: "一般控制区",
          fillColor: NATIONAL_PARK_ZONING_COLORS.general.fillColor,
          strokeColor: NATIONAL_PARK_ZONING_COLORS.general.strokeColor,
          match: { field: "type", value: "一般控制区" },
          selectable: false,
        },
      ],
    },
  ],
  restoration: [
    {
      key: "restoration-projects",
      name: "生态修复工程",
      sourceType: "geojson",
      dataPath: "datas/ecological-protection/ecological_restoration_projects.geojson",
      opacity: 0.72,
      legends: [
        {
          key: "restoration-grassland",
          name: "草地修复",
          fillColor: "#75AD49",
          strokeColor: "#D2EA78",
          cqlFilter: createEqualityCql("project_category", "草地修复"),
          match: { field: "project_category", value: "草地修复" },
        },
        {
          key: "restoration-wetland",
          name: "湿地修复",
          fillColor: "#4FB4C3",
          strokeColor: "#A9EEF2",
          cqlFilter: createEqualityCql("project_category", "湿地修复"),
          match: { field: "project_category", value: "湿地修复" },
        },
        {
          key: "restoration-woodland",
          name: "林地修复",
          fillColor: "#43A97A",
          strokeColor: "#95E2B7",
          cqlFilter: createEqualityCql("project_category", "林地修复"),
          match: { field: "project_category", value: "林地修复" },
        },
        {
          key: "restoration-linear",
          name: "线性设施",
          fillColor: "#D2B24D",
          strokeColor: "#FFE28A",
          cqlFilter: createEqualityCql("feature_role", "线性设施"),
          match: { field: "feature_role", value: "线性设施" },
        },
      ],
    },
  ],
  "engineering-analysis": [
    {
      key: "engineering-analysis-projects",
      name: "工程分析项目库",
      sourceType: "geojson",
      dataPath: "datas/ecological-protection/ecological_restoration_projects.geojson",
      opacity: 0.76,
      mapView: {
        targetLngLat: [102.58, 32.72],
        distanceScale: 0.78,
        targetLocalZ: 0.72,
        duration: 0.85,
      },
      legends: [
        {
          key: "analysis-project-grassland",
          name: "草地修复",
          fillColor: "#75AD49",
          strokeColor: "#D2EA78",
          match: { field: "project_category", value: "草地修复" },
          selectable: false,
        },
        {
          key: "analysis-project-wetland",
          name: "湿地修复",
          fillColor: "#4FB4C3",
          strokeColor: "#A9EEF2",
          match: { field: "project_category", value: "湿地修复" },
          selectable: false,
        },
        {
          key: "analysis-project-woodland",
          name: "林地修复",
          fillColor: "#43A97A",
          strokeColor: "#95E2B7",
          match: { field: "project_category", value: "林地修复" },
          selectable: false,
        },
        {
          key: "analysis-project-linear",
          name: "线性设施",
          fillColor: "#D2B24D",
          strokeColor: "#FFE28A",
          match: { field: "feature_role", value: "线性设施" },
          selectable: false,
        },
      ],
    },
  ],
}

export const yakIndustryChainLayers = {
  "yak-industry-chain": [
    {
      key: "yak-recognition-layer",
      name: "牦牛识别",
      layerName: "ne:daping_yak_marks",
      styles: "yak_marks",
      cqlFilter: "gov_show = true",
      opacity: 0.96,
      queryBuffer: 10,
      mapView: {
        targetLngLat: [102.58, 32.72],
        distanceScale: 0.82,
        targetLocalZ: 0.76,
        duration: 0.85,
      },
      legends: [
        {
          key: "yak-recognition-all",
          name: "牦牛识别",
          fillColor: "#FFFF00",
          strokeColor: "#FFFF00",
          selectable: false,
        },
      ],
    },
    {
      key: "yak-industry-points",
      name: "牦牛产业点位",
      sourceType: "geojson",
      dataPath: "datas/yak-industry-chain/yak_industry_points.geojson",
      opacity: 0.96,
      pointLabelLimit: 12,
      showPointLabels: false,
      mapView: {
        targetLngLat: [102.48, 32.69],
        distanceScale: 0.82,
        targetLocalZ: 0.76,
        duration: 0.85,
      },
      legends: [
        {
          key: "yak-industry-family-pasture",
          name: "家庭生态牧场",
          fillColor: "#24F6C4",
          strokeColor: "#B7FFF2",
          match: { field: "stageKey", value: "family-pasture" },
        },
        {
          key: "yak-industry-artificial-grassland",
          name: "人工草地",
          fillColor: "#7EE269",
          strokeColor: "#D6FF8D",
          match: { field: "stageKey", value: "artificial-grassland" },
        },
        {
          key: "yak-industry-scale-breeding",
          name: "规模化养殖基地",
          fillColor: "#32DCFF",
          strokeColor: "#B8F7FF",
          match: { field: "stageKey", value: "scale-breeding" },
        },
        {
          key: "yak-industry-forage-storage",
          name: "防灾减灾",
          fillColor: "#FFE06B",
          strokeColor: "#FFF2A0",
          match: { field: "stageKey", value: "forage-storage" },
        },
        {
          key: "yak-industry-slaughter-processing",
          name: "屠宰与加工",
          fillColor: "#FF9A62",
          strokeColor: "#FFD0A8",
          match: { field: "stageKey", value: "slaughter-processing" },
        },
        {
          key: "yak-industry-farm-equipment",
          name: "农机装备",
          fillColor: "#30DCFF",
          strokeColor: "#B8F7FF",
          match: { field: "stageKey", value: "farm-equipment" },
        },
        {
          key: "yak-industry-social-service",
          name: "社会化服务",
          fillColor: "#9EA7FF",
          strokeColor: "#DFE3FF",
          match: { field: "stageKey", value: "social-service" },
        },
      ],
    },
  ],
}

const layerRegistry = {
  ...forestGrassWetLayers,
  ...ecologicalProtectionLayers,
  ...yakIndustryChainLayers,
}

const topicRegistry = [...forestGrassWetTopics, ...ecologicalProtectionTopics, ...yakIndustryChainTopics]

export function getTopicConfig(topicKey) {
  return topicRegistry.find((topic) => topic.key === topicKey) || forestGrassWetTopics[0]
}

export function getLayersByTopic(topicKey) {
  return layerRegistry[topicKey] || []
}

export function getLayerByKey(topicKey, layerKey) {
  const layers = getLayersByTopic(topicKey)
  return layers.find((layer) => layer.key === layerKey) || layers[0]
}
