package com.tofly.ghrq_route.module.map;

import android.content.Context;

import com.esri.arcgisruntime.geometry.Point;
import com.google.gson.JsonObject;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.module.map.controller.MapServiceCallback;
import com.tofly.ghrq_route.module.map.controller.MapServiceController;
import com.tofly.ghrq_route.module.map.domain.LayerSidBean;
import com.tofly.ghrq_route.module.pipeAnalyze.domain.PipeAnalysisResultBean;
import com.tofly.ghrq_route.module.pipeAnalyze.domain.PipeAnalyzeFindTaskBean;
import com.tofly.ghrq_route.module.pipeAnalyze.domain.PipeAnalyzeStatusBean;

import java.util.List;

public class MapServiceTool {

    private static final String TAG = MapServiceTool.class.getName();

    private static MapServiceTool mapServiceTool = null;

    private MapServiceController mapServiceController;

    public static String MAP_DEFAULT_GIS_TOKEN = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJVc2VySUQiOiIiLCJVU0VSTkFNRSI6IiIsIlJFQUxOQU1FIjoiIiwiV0tJRCI6NDU0NCwiUkFOR0UiOiJYUlRZIiwiUkFOR0VGSUVMRCI6bnVsbCwiQ09ERSI6IlhSVFkiLCJDT05ORUNUU1RSIjoiREFUQUJBU0U9b3JjbDtORVROQU1FPTE3Mi4yNi4yLjIwMC9vcmNsO1NFUlZFUj0xNzIuMjYuMi4yMDA7VVNFUklEPVRGX0dIUlFfR0lTO1BBU1NXT1JEPVRGX0dIUlFfR0lTO1BPUlQ9MTUyMUA1MTUxL3RjcDtWRVJTSU9OPVNERS5ERUZBVUxUIiwiQm9yblRpbWUiOjAsIkFwcElEIjpudWxsfQ.A0N4pTcf5NY0YlWAqQaR0JaEpJiXDJLkmyauiktKBq-lw_isijArGeg8Ge2TjY364obrpDWE8-sc2Tk-IXUnuA";

    private MapServiceTool() {

    }

    public static synchronized MapServiceTool getInstance() {
        if (mapServiceTool == null) {
            mapServiceTool = new MapServiceTool();
        }
        return mapServiceTool;
    }


    public void commitPipeAnalyzePoint(Context context, Point point, final MapServiceCallback mapServiceCallback) {
        if (point != null) {
            if (mapServiceController == null)
                mapServiceController = new MapServiceController();
            mapServiceController.commitPipeAnalyzePoint(context, point.getX(), point.getY(), new MapServiceCallback(){
                @Override
                public void onCommitPipeAnalyzePoint(boolean isSuccess, int errorCode, String errMsg) {
                    super.onCommitPipeAnalyzePoint(isSuccess, errorCode, errMsg);
                    String msg = "提交爆管位置" + (isSuccess ? "成功" : "失败") + ". : " + errMsg;
                    ToastUtil.showToast(context, msg);
                    if (mapServiceCallback != null)
                        mapServiceCallback.onCommitPipeAnalyzePoint(isSuccess, errorCode, errMsg);
                }
            });
        }
    }

    public void getLayerInfo(Context context, String layer_id, MapServiceCallback mapServiceCallback) {
        if (mapServiceController == null)
            mapServiceController = new MapServiceController();
        String gisToken = MAP_DEFAULT_GIS_TOKEN;
        mapServiceController.getLayerInfo(context, gisToken, layer_id, new MapServiceCallback(){
            @Override
            public void onGetLayerInfo(boolean isSuccess, int errorCode, String errMsg, String layerdbname) {
                super.onGetLayerInfo(isSuccess, errorCode, errMsg, layerdbname);
                String msg = "查询图层属性" + (isSuccess ? "成功" : "失败") + ". : " + errMsg;
                ToastUtil.showToast(context, msg);
                if (mapServiceCallback != null)
                    mapServiceCallback.onGetLayerInfo(isSuccess, errorCode, errMsg, layerdbname);
            }
        });
    }

    public void submitPipeAnalyzeJob(Context context, String gisToken, String pipe_id, String featureName, MapServiceCallback mapServiceCallback) {
        if (mapServiceController == null)
            mapServiceController = new MapServiceController();
        mapServiceController.submitPipeAnalyzeJob(context, gisToken, pipe_id, featureName, new MapServiceCallback(){
            @Override
            public void onSubmitPipeAnalyzeJobt(boolean isSuccess, int errorCode, String errMsg, String job_id) {
                super.onSubmitPipeAnalyzeJobt(isSuccess, errorCode, errMsg, job_id);
                if (mapServiceCallback != null)
                    mapServiceCallback.onSubmitPipeAnalyzeJobt(isSuccess, errorCode, errMsg, job_id);
            }
        });
    }

    public void getPipeAnalyzeJobStatus(Context context, String tag, String job_id, MapServiceCallback mapServiceCallback) {
        if (mapServiceController == null)
            mapServiceController = new MapServiceController();
        mapServiceController.getPipeAnalyzeJobStatus(context, tag, job_id, new MapServiceCallback(){
            @Override
            public void onGetPipeAnalyzeJobStatus(boolean isSuccess, int errorCode, String errMsg, PipeAnalyzeStatusBean statusBean) {
                super.onGetPipeAnalyzeJobStatus(isSuccess, errorCode, errMsg, statusBean);
                if (mapServiceCallback != null)
                    mapServiceCallback.onGetPipeAnalyzeJobStatus(isSuccess, errorCode, errMsg, statusBean);
            }
        });
    }

    public void getPipeAnalyzeResult(Context context, String job_id, String paramUrl, MapServiceCallback mapServiceCallback) {
        if (mapServiceController == null)
            mapServiceController = new MapServiceController();
        mapServiceController.getPipeAnalyzeResult(context, job_id, paramUrl, new MapServiceCallback(){
            @Override
            public void onGetPipeAnalyzeResult(boolean isSuccess, int errorCode, String errMsg, PipeAnalysisResultBean resultBean) {
                super.onGetPipeAnalyzeResult(isSuccess, errorCode, errMsg, resultBean);
                if (mapServiceCallback != null)
                    mapServiceCallback.onGetPipeAnalyzeResult(isSuccess, errorCode, errMsg, resultBean);
            }
        });
    }

    public void pipeAnalyzeFindTask(Context context, String job_url, String layername, MapServiceCallback mapServiceCallback) {
        if (mapServiceController == null)
            mapServiceController = new MapServiceController();
        mapServiceController.pipeAnalyzeFindTask(context, job_url, layername, new MapServiceCallback(){
            @Override
            public void onPipeAnalyzeFindTask(boolean isSuccess, int errorCode, String errMsg, List<PipeAnalyzeFindTaskBean> taskBeans) {
                super.onPipeAnalyzeFindTask(isSuccess, errorCode, errMsg, taskBeans);
                if (mapServiceCallback != null)
                    mapServiceCallback.onPipeAnalyzeFindTask(isSuccess, errorCode, errMsg, taskBeans);
            }
        });
    }

    public void getLayerIdBySidFromQRCode(Context context, String sid, MapServiceCallback mapServiceCallback) {
        if (mapServiceController == null)
            mapServiceController = new MapServiceController();
        mapServiceController.getLayerIdBySidFromQRCode(context, sid, new MapServiceCallback(){
            @Override
            public void onGetLayerIdBySid(boolean isSuccess, int errorCode, String errMsg, LayerSidBean layerSidBean) {
                super.onGetLayerIdBySid(isSuccess, errorCode, errMsg, layerSidBean);
                if (mapServiceCallback != null)
                    mapServiceCallback.onGetLayerIdBySid(isSuccess, errorCode, errMsg, layerSidBean);
            }
        });
    }

    public void query_device_by_sid(Context context, String sid, MapServiceCallback mapServiceCallback) {
        if (mapServiceController == null)
            mapServiceController = new MapServiceController();
        mapServiceController.query_device_by_sid(context, sid, new MapServiceCallback(){
            @Override
            public void onQueryMapBySid(boolean isSuccess, int errorCode, String errMsg, JsonObject jsonAttributes, JsonObject jsonGeometry, String tableName) {
                super.onQueryMapBySid(isSuccess, errorCode, errMsg, jsonAttributes, jsonGeometry, tableName);
                if (mapServiceCallback != null)
                    mapServiceCallback.onQueryMapBySid(isSuccess, errorCode, errMsg, jsonAttributes, jsonGeometry, tableName);
            }
        });
    }
}
