<!--
  文件描述：地图底部信息条
  创建时间：2024/3/29 8:53
  创建人：Zhaipeixiu
-->
<script setup lang="ts">
import { Angle } from '@/utils/map/angle.ts'
import {ScreenSpaceEventHandler, Math, ScreenSpaceEventType, Cartesian3, Cartographic} from 'cesium'
import {onMounted, ref} from "vue";
import {cartesian2ToCartesian3} from "@/utils/map/coordinate.ts";
import {elevationProfile} from "@/utils/map/SpatialAnalysis.ts";
let nowLatStr: string, nowLonStr: string
let lonlatStr = ref('')
let isDecimal = ref(true)

onMounted(()=>{
  let _viewer = window.viewer
  let canvas =  _viewer.scene.canvas
  let handler = new ScreenSpaceEventHandler(canvas)
  handler.setInputAction((e:any)=> {
    //捕获椭球体，将笛卡尔二维平面坐标转为椭球体的笛卡尔三维坐标，返回球体表面的点
    let position = cartesian2ToCartesian3(_viewer, e.endPosition)
    if (position) {
      //将笛卡尔三维坐标转为地图坐标（弧度）
      let cartographic =  _viewer.scene.globe.ellipsoid.cartesianToCartographic(position);
      try{
        // 将地图坐标（弧度）转为十进制的度数
        nowLatStr = Math.toDegrees(cartographic.latitude).toFixed(7) // 纬度
        nowLonStr = Math.toDegrees(cartographic.longitude).toFixed(7) // 经度
        let camera_alt = (_viewer.camera.positionCartographic.height / 1000) // 视高
        // 相机低于250、俯仰角小于-80度时才计算海拔高度（否则误差大）
        let needElevation: boolean = camera_alt < 250 && (_viewer.camera.pitch < -(Math.PI/180)*80)
        let elevStr = needElevation? Cartographic.fromCartesian(position).height.toFixed(2)+'m'?? '' : '高度过高'

        if(isDecimal.value) {   //如果是十进制度
          lonlatStr.value = `经度: ${nowLonStr}°, 纬度: ${nowLatStr}°, 高度: ${elevStr}`
        }
        else {
          lonlatStr.value = `经度: ${Angle.DecimalDegree2DMS(nowLonStr)}, 纬度: ${Angle.DecimalDegree2DMS(nowLatStr)},
          高度: ${elevStr}`
        }
      } catch (e) {}
    }
  }, ScreenSpaceEventType.MOUSE_MOVE)
})

function lonlatClick() {
  let elevStr = lonlatStr.value.split('高度')[1]
  if(isDecimal.value){
    lonlatStr.value = `经度: ${Angle.DecimalDegree2DMS(nowLonStr)}, 纬度: ${Angle.DecimalDegree2DMS(nowLatStr)}, 高度` + elevStr
  }
  else {
    lonlatStr.value = `经度: ${nowLonStr}°, 纬度: ${nowLatStr}°, 高度` + elevStr
  }
  isDecimal.value = !isDecimal.value
  elevationProfile(window.viewer, Cartesian3.fromDegrees(119.0,30.05),Cartesian3.fromDegrees(119.05,30.0),500)
}
</script>

<template>
  <div id="map-footer">
    <button id="lonlatText" @click="lonlatClick">
      {{ lonlatStr }}
    </button>
  </div>
</template>

<style scoped>
#map-footer {
  position: absolute;
  bottom: 1px;
  left: 0;
  width: 100vw;
  height: 1.7rem;
  background-color: rgba(47, 53, 60, 0.8);
  color: #fff;
  font-size: 0.7rem;
  text-align: center;
  z-index: 1;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#lonlatText{
  background: none;
  border: none;
  color: #ffffff;
  font-size: 0.8rem;
}
#lonlatText:hover{
  cursor: pointer;
}
</style>
