import * as Cesium from 'cesium'

// 定义火情点的属性
export const warning = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "火情点",
    disabled: true,
  },
  {
    key: "name",
    name: "点位名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "longitude",
    name: "经度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "latitude",
    name: "纬度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "deviceModel",
    name: "设备型号",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "deviceType",
    name: "设备类型",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "amountOfDamage",
    name: "受损金额",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "fireArea",
    name: "火灾面积",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "fireLevel",
    name: "火情等级",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "causeOfFire",
    name: "起火原因",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "discoveryTime",
    name: "发现时间",
    type: "text",
    value: "",
    disabled: false,
  },
];

// 定义巡护路线的属性
export const track = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "巡护路线",
    disabled: true,
  },
  {
    key: "name",
    name: "名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "length",
    name: "长度",
    type: "text",
    value: "",
    disabled: true,
  },
]

// 定义护林员的属性
export const ranger = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "护林员",
    disabled: true,
  },
  {
    key: "name",
    name: "人员名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "longitude",
    name: "经度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "latitude",
    name: "纬度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "phone",
    name: "联系电话",
    type: "text",
    value: "",
    disabled: false,
  },
];

// 定义云台的属性
export const ptz = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "云台",
    disabled: true,
  },
  {
    key: "name",
    name: "云台名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "longitude",
    name: "经度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "latitude",
    name: "纬度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "no",
    name: "云台编号",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "equipmentManufacturers",
    name: "设备厂商",
    type: "text",
    value: "",
    disabled: false,
  },
]

// 定义卡口的属性
export const bayonet = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "卡口",
    disabled: true,
  },
  {
    key: "name",
    name: "卡口名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "longitude",
    name: "经度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "latitude",
    name: "纬度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "no",
    name: "卡口编号",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "equipmentManufacturers",
    name: "设备厂商",
    type: "text",
    value: "",
    disabled: false,
  },
]

// 定义无人机机场的属性
export const airport = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "无人机机场",
    disabled: true,
  },
  {
    key: "name",
    name: "机场名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "longitude",
    name: "经度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "latitude",
    name: "纬度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "no",
    name: "机场编号",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "equipmentManufacturers",
    name: "设备厂商",
    type: "text",
    value: "",
    disabled: false,
  },
]

// 定义热点的属性
export const hot = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "热点",
    disabled: true,
  },
  {
    key: "name",
    name: "热点名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "longitude",
    name: "经度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "latitude",
    name: "纬度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "fireLevel",
    name: "火情等级",
    type: "select",
    value: "",
    options: [
      { label: "一级", value: "1" },
      { label: "二级", value: "2" },
      { label: "三级", value: "3" },
    ],
    disabled: false,
  },
]

  // 定义水源地的属性
export const watersource = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "水源地",
    disabled: true,
  },
  {
    key: "name",
    name: "名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "area",
    name: "占地面积",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "longitude",
    name: "经度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "latitude",
    name: "纬度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "volume",
    name: "储水量",
    type: "text",
    value: "0",
    disabled: false,
  },
];

  // 定义仓库的属性
export const warehouse = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "仓库",
    disabled: true,
  },
  {
    key: "name",
    name: "名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "longitude",
    name: "经度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "latitude",
    name: "纬度",
    type: "text",
    value: "0",
    disabled: true,
  },
];

// 定义林区道路的属性
export const road = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "林区道路",
    disabled: true,
  },
  {
    key: "name",
    name: "道路名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "roadType",
    name: "道路类型",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "length",
    name: "道路长度",
    type: "text",
    value: "",
    disabled: true,
  },
  {
    key: "width",
    name: "道路宽度",
    type: "text",
    value: "",
    disabled: false,
  },
]

// 定义重点区域的属性
export const keyarea = [
  {
    key: "type",
    name: "要素类型",
    type: "text",
    value: "重点区域",
    disabled: true,
  },
  {
    key: "name",
    name: "名称",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "area",
    name: "占地面积",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "longitude",
    name: "经度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "latitude",
    name: "纬度",
    type: "text",
    value: "0",
    disabled: true,
  },
  {
    key: "description",
    name: "区域说明",
    type: "text",
    value: "",
    disabled: false,
  },
  {
    key: "startTime",
    name: "开始监管时间",
    type: "datetime",
    value: "",
    disabled: false,
  },
  {
    key: "endTime",
    name: "结束监管时间",
    type: "datetime",
    value: "",
    disabled: false,
  },
];

const config = {
  warning,
  track,
  ranger,
  ptz,
  bayonet,
  airport,
  hot,
  watersource,
  warehouse,
  road,
  keyarea,
}

const getClone = (config) => {
  return config.map(item => ({ ...item }))
}

export const getPropertyData = (entity) => {
  let data = []
  if(!entity || !entity.properties) {
    return data // 如果没有实体或属性，返回空数组
  }

  const properties = entity.properties.getValue()
  if (!properties.__type || !config[properties.__type]) {
    console.warn('__type属性无效，或__type属性的值不存在于配置对象中')
    return data
  }
  data = getClone(config[properties.__type])

  // 遍历数据数组，将properties有的属性赋给对应的属性
  // 注意：地图要素属性主要参考这里的数据结构，就算properties没有值，也会显示出来
  data.forEach(item => {
    const value = properties[item.key]
    if (value !== undefined) {
      item.value = value
    }
  });
  return data
};

export const setPropertyData = (data, entity) => {
  if (!entity || !entity.properties) {
    console.warn("Entity or properties not found")
    return
  }

  const newProperties = entity.properties.getValue()
  data.forEach(item => {
    if (item.key && item.value !== undefined) {
      newProperties[item.key] = item.value
    }
  })
  // 替换 properties
  entity.properties = new Cesium.PropertyBag(newProperties)

  // 这里默认name字段是标签显示的内容，如果entity有label，则更新name字段值
  if(entity.label) {
    entity.label.text = new Cesium.ConstantProperty(newProperties.name);
  }
}
