/**
 * 水上客运地图功能组件，通过调用封装好的基础地图组件实现特定业务功能
 */
import {
    fullExtent, clearMap, calculation, changeMap, setCamera,
    layerControl, layerQuery, addGraphic, draw, getDrawGeometry,
    addBoats, addBoatHistory, addLayer
} from '@/utils/map/ArcgisUtil'

import {GetPjylLinePaths, GetWaterFlightPoints, GetShipRout} from "@/apis/ssky";
import {setCenter} from "@/utils/map/Common";

// 浦江游览航线
export function pjylLine(isShow){
    if(isShow){
        GetPjylLinePaths().then(res=>{
            for (let i=res.data.length-1;i>=0;i--){
                let lineData = res.data[i]
                let linePoints = lineData.linePoints
                let points = []
                let first = []
                for(let j=0;j<linePoints.length;j++){
                    let point = [Number(linePoints[j].x),Number(linePoints[j].y)]
                    if(j==0){
                        first = [Number(linePoints[j].x),Number(linePoints[j].y)]
                    }
                    points.push(point)
                }
                points.push(first)

                let symbol
                if(lineData.lineName == '精华航线'){
                    symbol = {
                        type: "simple-fill",
                        color: [50, 205, 50, 0.5],
                        //style: "cross",
                        outline: {
                            color: [255, 0, 0],
                            width: 2,
                            style: "dash"
                        }
                    }
                }else{
                    symbol = {
                        type: "simple-fill",
                        color: [50, 205, 50, 0.5],
                        //style: "cross",
                        outline: {
                            color: [255, 0, 0],
                            width: 2,
                            style: "dash"
                        }
                    }
                }

                let params = {
                    title: lineData.lineName,
                    type: 'polygon',
                    data: points,
                    edit: false,
                    temp: true,
                    clear: false,
                    goto: true,
                    attributes: null,
                    symbol: symbol
                }
                // 绘制到地图上
                addGraphic(params)
            }
        })
    }else{
        clearMap({is_draw: false, is_search:false, layers: ['tempLayer']})
        setCenter();
    }

}

// 浦江游览码头定位
export function onePjylPortLocation(data,isShow){
    if(isShow){
        // 码头定位
        let param = {
            title: data.portName,
            type: 'point',
            data:[data.mapx,data.mapy,20],
            edit: false,
            temp: false,
            clear: false,
            goto: true,
            attributes: null,
            symbol:{
                "type": "picture-marker",
                "url": "images/ssky/bgImgPort.png",
                "width":"300px",
                "height":"10px"
            }
        }
        addGraphic(param)
        // 绘制陆域围栏
        layerQuery({
            title: "waterPassenger",
            layerId: "4",
            where: "码头名称='" + data.portName +"'",
            symbol: null,
            is_draw: true,
            is_clear: false,
            callback: "pjylPortHandle"
        })
    }else{
        clearMap({is_draw: false, is_search:true, layers: []})
        setCenter();
    }

}
// 浦江游览码头查询回调
export function pjylPortHandle(res) {
    let data = res.data[0].geometry.rings[0]
    let params = {
        type: 'line-3d',
        data: data,
        edit: false,
        clear: false,
        goto: true,
        attributes: null,
        title: null
    }
    addGraphic(params)
}

// 对江轮渡码头定位
export function oneDjldPortLocation(data,isShow){
    if(isShow) {
        // 码头定位
        let param = {
            title: data.portName,
            type: 'point',
            data: [data.mapx, data.mapy, 20],
            edit: false,
            clear: false,
            goto: true,
            attributes: null,
            symbol: {
                "type": "picture-marker",
                "url": "images/ssky/bgImgPort.png",
                "width": "300px",
                "height": "10px"
            }
        }
        addGraphic(param)
       /* // 绘制陆域围栏
        layerQuery({
            title: "waterPassenger",
            layerId: "1",
            where: "码头名称='" + data.portName + "'",
            symbol: null,
            is_draw: true,
            is_clear: false,
            callback: "djldPortHandle"
        })*/
    }else{
        clearMap({is_draw: false, is_search:true, layers: []})
        setCenter();
    }
}

// 浦江游览码头查询回调
export function djldPortHandle(res) {
    let data = res.data[0].geometry.rings[0]
    let params = {
        type: 'line-3d',
        data: data,
        edit: false,
        clear: false,
        goto: true,
        attributes: null,
        title: null
    }
    addGraphic(params)
}

// 对江轮渡航线批量展示
export function djldLineAll(isShow) {
    addLayer({
        id: 'djldLayer',
        title: 'djldLayer',
        type: 'feature',
        url: 'http://10.83.68.109:6080/arcgis/rest/services/water_passenger_3857/MapServer/5',
        visible: isShow,
        opacity: 1,
        token: '',
        is_goto: true
    })
   /* if(isShow){
        // GetShipRout({keywords:'',type:''}).then(res =>{
        //     let list = res.data.Rows.list;
        //     for(let i = 0; i < list.length;i++){
        //         let lineData = list[i].pointList;
        //         let lineName = list[i].name;
        //         let points = []
        //         for(let j=0;j<lineData.length;j++){
        //             let point = [Number(lineData[j].mapx),Number(lineData[j].mapy)]
        //             points.push(point)
        //         }
        //         let params = {
        //             title: lineName,
        //             type: 'polyline',
        //             data: points,
        //             edit: false,
        //             clear: false,
        //             temp: false,
        //             goto: false,
        //             attributes: null,
        //             symbol: {
        //                 type: "simple-line",
        //                 color: [255, 255, 0],
        //                 width: 2
        //             }
        //         }
        //         // 绘制到地图上
        //         addGraphic(params)
        //     }
        // })
    }else{
        clearMap({is_draw: false, is_search: true, layers: []})
    }*/
}

// 对江轮渡航线
export function djldLine(lineName,isShow){
    if(isShow){
        GetWaterFlightPoints({lineName:lineName}).then(res=>{
            let rows = res.data.Rows;
            for (let i=0;i<rows.length;i++){
                let lineData = rows[i]
                let linePoints = lineData.linePoint[0]
                let points = []
                for(let j=0;j<linePoints.length;j++){
                    let point = [Number(linePoints[j].x),Number(linePoints[j].y)]
                    points.push(point)
                }
                let params = {
                    title: lineName,
                    type: 'polyline',
                    data: points,
                    edit: false,
                    clear: false,
                    goto: true,
                    attributes: null,
                    symbol: {
                        type: "simple-line",
                        color: [255, 255, 0],
                        width: 2
                    }
                }
                // 绘制到地图上
                addGraphic(params)
            }
        })
    }else{
        clearMap({is_draw: false, is_search:true, layers: []})
        setCenter();
    }

}