/**
 * 船舶地图组件功能，通过调用封装好的基础地图组件实现特定业务功能
 */
import { fullExtent, clearMap, calculation, changeMap, setCamera,
    layerControl, layerQuery, addGraphic, draw, getDrawGeometry,
    addBoats, setBoatHistory, playBoatHistory,
    getMapScale
} from '@/utils/map/ArcgisUtil'
import {GetHpjShip} from "@/apis/ship";
import {nextTick} from "vue";
import bus from "@/utils/bus";

let boatInterval;
let showBoat = false;

// 船舶定位
export function locationBoat(mmsi,isShow){
    if(isShow){
        GetHpjShip().then(res=>{
            let data = res.Result.data
            let boats = {
                "scale": 2000,
                "goto": true,
                "data": []
            }
            for(let i=0;i<data.length;i++){
                if(data[i].DEVICEID == mmsi){
                    let boat = {
                        "mmsi": data[i].DEVICEID,
                        "shipNameCn": data[i].BOATNAME,
                        "shipTypeName": data[i].SHIPTYPENAME,
                        "direction": data[i].DIRECTION,
                        "velocity": data[i].VELOCITY,
                        "shipBreadth": data[i].SHIPBREADTH,
                        "shipLength": data[i].SHIPLENGTH,
                        "mapx": data[i].MAPX,
                        "mapy": data[i].MAPY
                    }
                    boats.data.push(boat)
                }
            }
            // console.log('定位：'+JSON.stringify(boats))
            addBoats(boats)
        })
    }else{
        if (!showBoat){
            clearMap({is_draw: false, is_search:false, layers: ['boatLayer']})
        }
    }
}

// 船舶撒点
export function setBoat(isShow){
    if(isShow) {
        if (!showBoat){
            showBoat = true
        }else{
            return
        }

        onLocationBoat()
        if (boatInterval != null) {
            clearInterval(boatInterval)
        }
        boatInterval = setInterval(onLocationBoat, 10000)
    }else{
        if (showBoat){
            showBoat = false
        }
        if (boatInterval != null) {
            clearInterval(boatInterval)
        }
        clearMap({is_draw: false, is_search:true, layers: ['boatLayer','boatPathLayer']})
    }
}

// 加载船舶
function onLocationBoat(){
    GetHpjShip().then(res=>{
        let data = res.Result.data
        let boats = {
            "scale": getMapScale(),
            "goto": false,
            "data": []
        }
        for(let i=0;i<data.length;i++){
            let boat = {
                "mmsi": data[i].DEVICEID,
                "shipNameCn": data[i].BOATNAME,
                "shipTypeName": data[i].SHIPTYPENAME,
                "direction": data[i].DIRECTION,
                "velocity": data[i].VELOCITY,
                "shipBreadth": data[i].SHIPBREADTH,
                "shipLength": data[i].SHIPLENGTH,
                "mapx": data[i].MAPX,
                "mapy": data[i].MAPY
            }
            boats.data.push(boat)
        }
        addBoats(boats)
    })
}



bus.on('playBoatPath', (params) => {
    nextTick(() => {
        setBoatPath(params)
    },{immediate: true});
    nextTick(() => {
        playBoatHistoryPath('start')
    },{immediate: true})
})




// 设置船舶历史轨迹
export function setBoatPath(data){
    GetBoatAISHistoryByMMSI({
        deviceId:data.mmsi,
        startTime:data.startTime,
        endTime:data.endTime
    }).then(res=>{
        let data = res.Result.data
        let historyPoints=[]
        for(let i=0;i<data.length;i++){
            let boat = {
                "gpsTime": data[i].GPSTIME,
                "mapx": data[i].MAPX,
                "mapy": data[i].MAPY
            }
            historyPoints.push(boat)
        }
        setBoatHistory(historyPoints)
    })
}

/**
 * 船舶轨迹播放
 * 开始：start，
 * 暂停：pause，
 * 继续：resume，
 * 停止：stop
 * @param type
 */
export function playBoatHistoryPath(type) {
    playBoatHistory(type)
}

/**
 * 清除船舶轨迹
 */
export function clearBoatHistoryPath(){
    clearMap({is_draw: false, is_search: false, layers: ['boatLayer','boatPathLayer','tempLayer']})
}