import hongyuanTownshipsRaw from "/public/datas/geojsons/hongyuan-townships.geojson?raw"
import { HONGYUAN_TOWNSHIP_GEOJSON_PATH } from "@/config/mapResources"

export const hongyuanTownshipGeoJsonPath = HONGYUAN_TOWNSHIP_GEOJSON_PATH

const townshipEnNames = {
  邛溪镇: "qiongxi",
  刷经寺镇: "shuajingsi",
  安曲镇: "anqu",
  龙日镇: "longri",
  江茸乡: "jiangrong",
  查尔玛乡: "chaerma",
  瓦切镇: "waqie",
  阿木乡: "amu",
  麦洼乡: "maiwa",
  色地镇: "sedi",
}

const townshipValues = {
  色地镇: 1199,
  瓦切镇: 991,
  刷经寺镇: 986,
  邛溪镇: 896,
  阿木乡: 892,
  安曲镇: 885,
  查尔玛乡: 729,
  龙日镇: 645,
  麦洼乡: 559,
  江茸乡: 514,
}

const townshipLabelCenterOverrides = {
  龙日镇: [102.5608, 32.5099],
}

function parseGeoJson(raw) {
  try {
    return JSON.parse(raw)
  } catch (error) {
    return { type: "FeatureCollection", features: [] }
  }
}

function readPoint(feature = {}) {
  const properties = feature.properties || {}
  const point = properties.cp || properties.center || properties.centroid || properties.sourceCenter
  if (Array.isArray(point) && Number.isFinite(Number(point[0])) && Number.isFinite(Number(point[1]))) {
    return [Number(point[0]), Number(point[1])]
  }
  return [0, 0]
}

export const hongyuanTownshipsGeoJsonRaw = hongyuanTownshipsRaw
export const hongyuanTownshipsGeoJson = parseGeoJson(hongyuanTownshipsRaw)
export const hongyuanTownshipFeatures = hongyuanTownshipsGeoJson.features || []

const townshipsByCode = hongyuanTownshipFeatures.reduce((result, feature) => {
  const properties = feature.properties || {}
  const name = normalizeHongyuanTownName(properties.name)
  ;[properties.code, properties.fullCode].forEach((code) => {
    const normalizedCode = String(code || "").replace(/\D/g, "")
    if (normalizedCode && name) result[normalizedCode] = name
  })
  return result
}, {})

export const hongyuanTownshipCodeByName = hongyuanTownshipFeatures.reduce((result, feature) => {
  const properties = feature.properties || {}
  const name = normalizeHongyuanTownName(properties.name)
  const code = String(properties.code || "").replace(/\D/g, "")
  if (name && code) result[name] = code
  return result
}, {})

export const hongyuanTownshipLabelPoints = hongyuanTownshipFeatures.map((feature) => {
  const properties = feature.properties || {}
  const name = normalizeHongyuanTownName(properties.name)
  const center = townshipLabelCenterOverrides[name] || readPoint(feature)
  return {
    name,
    code: properties.code,
    fullCode: properties.fullCode,
    center,
    centroid: center,
    acreageMu: Number(properties.acreageMu) || 0,
  }
})

export const hongyuanTownshipRankingPoints = hongyuanTownshipLabelPoints
  .map((item) => ({
    ...item,
    enName: townshipEnNames[item.name] || "",
    value: townshipValues[item.name] || 0,
  }))
  .sort((a, b) => b.value - a.value)

export const hongyuanTownshipDisplayOrder = hongyuanTownshipLabelPoints.map((item) => item.name)

export function normalizeHongyuanTownName(value) {
  const text = String(value || "").trim()
  if (!text || text === "--") return ""
  return text
}

export function getHongyuanTownshipNameByCode(value) {
  const normalizedCode = String(value || "").replace(/\D/g, "")
  if (!normalizedCode) return ""
  return townshipsByCode[normalizedCode] || townshipsByCode[normalizedCode.slice(0, 12)] || townshipsByCode[normalizedCode.slice(0, 9)] || ""
}

export function getHongyuanTownshipCodeByName(value) {
  const name = normalizeHongyuanTownName(value)
  if (!name) return ""
  return hongyuanTownshipCodeByName[name] || ""
}

export function isHongyuanTownshipName(value) {
  const name = normalizeHongyuanTownName(value)
  return Boolean(name && hongyuanTownshipLabelPoints.some((item) => item.name === name))
}
