package com.casic.dcms.utils;

import android.util.Log;

import com.casic.dcms.model.MapConfigModel;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;

import java.util.List;

public class ArcGisMapCreator {

    private static final String TAG = "ArcGisBaseLayerCreator";

    /**
     * 创建带网格的ArcGIS底图
     */
    public static ArcGISTiledLayer createBaseLayer() {
        List<MapConfigModel.DataBean.RowsBean> mapConfigList = DataBaseManager.get().getMapConfigList();
        if (mapConfigList.size() == 0) {
            Log.d(TAG, "create: mapConfigList.size() == 0");
            return new ArcGISTiledLayer("/");
        }
        //第一个是GIS地图基础地址，http://111.198.10.15:13002
        String baseGisUrl = mapConfigList.get(0).getUrl();
        //第二个是二维地图URL，/arcgis/rest/services/crdt/MapServer
        /**
         * http://111.198.10.15:13002/arcgis/rest/services/crdt/MapServer
         * */
        Log.d(TAG, "createBaseLayer: " + baseGisUrl + mapConfigList.get(1).getUrl());
        return new ArcGISTiledLayer(baseGisUrl + mapConfigList.get(1).getUrl());
    }

    /**
     * 创建不带网格的ArcGIS底图
     */
    public static ArcGISTiledLayer createNoGridLayer() {
        List<MapConfigModel.DataBean.RowsBean> mapConfigList = DataBaseManager.get().getMapConfigList();
        if (mapConfigList.size() == 0) {
            Log.d(TAG, "create: mapConfigList.size() == 0");
            return new ArcGISTiledLayer("/");
        }
        String baseGisUrl = mapConfigList.get(0).getUrl();
        //第九个是无网格地图URL，/arcgis/rest/services/crdt_1/MapServer
        /**
         * http://111.198.10.15:13002/arcgis/rest/services/crdt_1/MapServer
         * */
        return new ArcGISTiledLayer(baseGisUrl + mapConfigList.get(8).getUrl());
    }

    /**
     * 创建网格图层或者社区图层ServiceFeatureTable
     */
    public static ServiceFeatureTable createGridFeatureTable() {
        List<MapConfigModel.DataBean.RowsBean> mapConfigList = DataBaseManager.get().getMapConfigList();
        if (mapConfigList.size() == 0) {
            Log.d(TAG, "create: mapConfigList.size() == 0");
            return new ServiceFeatureTable("/");
        }
        String baseGisUrl = mapConfigList.get(0).getUrl();
        //第五个是网格图层URL，/arcgis/rest/services/crdt/MapServer/13
        /**
         * http://111.198.10.15:13002/arcgis/rest/services/crdt/MapServer/13
         * */
        return new ServiceFeatureTable(baseGisUrl + mapConfigList.get(4).getUrl());
    }

    /**
     * 创建其他图层ServiceFeatureTable
     */
    public static ServiceFeatureTable createOtherFeatureTable(String layerName) {
        List<MapConfigModel.DataBean.RowsBean> mapConfigList = DataBaseManager.get().getMapConfigList();
        if (mapConfigList.size() == 0) {
            Log.d(TAG, "create: mapConfigList.size() == 0");
            return new ServiceFeatureTable("/");
        }
        String baseGisUrl = mapConfigList.get(0).getUrl();
        ServiceFeatureTable featureTable;
        switch (layerName) {
            case "店铺三包":
                /**
                 * http://111.198.10.15:13002/arcgis/rest/services/crbj/MapServer/1
                 * */
                featureTable = new ServiceFeatureTable(baseGisUrl + mapConfigList.get(5).getUrl());
                break;
            case "部件点":
                /**
                 * http://111.198.10.15:13002/arcgis/rest/services/crbj/MapServer/2
                 * */
                featureTable = new ServiceFeatureTable(baseGisUrl + mapConfigList.get(6).getUrl());
                break;
            case "公共厕所":
                /**
                 * http://111.198.10.15:13002/arcgis/rest/services/crbjfc/FeatureServer/33
                 * */
                String toiletLayerId = StringHelper.getToiletLayerId();
                featureTable = new ServiceFeatureTable(baseGisUrl + LocaleConstant.TOILET_SERVICE_URL + "/" + toiletLayerId);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + layerName);
        }
        return featureTable;
    }
}
