package com.casic.dcms.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.casic.dcms.R;
import com.casic.dcms.base.AndroidxBaseActivity;
import com.casic.dcms.bean.CaseCheckBean;
import com.casic.dcms.bean.CaseHandleBean;
import com.casic.dcms.bean.CaseVerifyBean;
import com.casic.dcms.bean.PointDataBean;
import com.casic.dcms.databinding.ActivityMapCaseBinding;
import com.casic.dcms.mvp.presenter.impl.CaseCheckPresenterImpl;
import com.casic.dcms.mvp.presenter.impl.CaseListHandlePresenterImpl;
import com.casic.dcms.mvp.presenter.impl.CaseVerifyPresenterImpl;
import com.casic.dcms.mvp.view.ICaseCheckView;
import com.casic.dcms.mvp.view.ICaseListHandleView;
import com.casic.dcms.mvp.view.ICaseVerifyView;
import com.casic.dcms.utils.ArcGisMapCreator;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.ToastHelper;
import com.casic.dcms.widgets.AlertControlDialog;
import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.IdentifyGraphicsOverlayResult;
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol;
import com.esri.arcgisruntime.util.ListenableList;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class CaseOnMapActivity extends AndroidxBaseActivity<ActivityMapCaseBinding> implements RadioGroup.OnCheckedChangeListener,
        ICaseListHandleView, ICaseVerifyView, ICaseCheckView {

    private static final String TAG = "CaseOnMapActivity";
    private CaseListHandlePresenterImpl caseHandleListPresenter;
    private CaseVerifyPresenterImpl caseVerifyPresenter;
    private CaseCheckPresenterImpl caseCheckPresenter;
    private ListenableList<Graphic> graphics;
    private ListenableList<GraphicsOverlay> graphicsOverlays;
    private List<PointDataBean> casePointList = new ArrayList<>();
    private WeakReferenceHandler weakReferenceHandler;

    @Override
    protected void setupTopBarLayout() {
        viewBinding.titleBarLayout.titleView.setText("案卷查看");
        viewBinding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void initData() {
        viewBinding.mapView.setAttributionTextVisible(false);//去掉左下角属性标识
        viewBinding.mapView.setViewpointScaleAsync(64000);

        ArcGISMap arcGISMap = new ArcGISMap(BasemapStyle.ARCGIS_STREETS);
        //创建底图、并设置底图图层
        Basemap basemap = new Basemap(ArcGisMapCreator.createNoGridLayer());
        arcGISMap.setBasemap(basemap);
        viewBinding.mapView.setMap(arcGISMap);

        ServiceFeatureTable communityFeatureTable = ArcGisMapCreator.createGridFeatureTable();
        communityFeatureTable.setFeatureRequestMode(ServiceFeatureTable.FeatureRequestMode.ON_INTERACTION_NO_CACHE);
        FeatureLayer communityLayer = new FeatureLayer(communityFeatureTable);

        ServiceFeatureTable pointFeatureTable = ArcGisMapCreator.createOtherFeatureTable("部件点");
        pointFeatureTable.setFeatureRequestMode(ServiceFeatureTable.FeatureRequestMode.ON_INTERACTION_NO_CACHE);
        FeatureLayer pointLayer = new FeatureLayer(pointFeatureTable);

        arcGISMap.getOperationalLayers().addAll(Arrays.asList(communityLayer, pointLayer));
        viewBinding.mapView.setMap(arcGISMap);

        caseHandleListPresenter = new CaseListHandlePresenterImpl(this);
        caseVerifyPresenter = new CaseVerifyPresenterImpl(this);
        caseCheckPresenter = new CaseCheckPresenterImpl(this);

        //默认加载处置中数据
        caseHandleListPresenter.onReadyRetrofitRequest(null, null, 1000, 1);

        //Marker相关Layer
        graphicsOverlays = viewBinding.mapView.getGraphicsOverlays();

        //handler弱饮用，防止内存泄漏
        weakReferenceHandler = new WeakReferenceHandler(this);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void initEvent() {
        viewBinding.radioGroup.setOnCheckedChangeListener(this);
        viewBinding.mapView.setOnTouchListener(new DefaultMapViewOnTouchListener(this, viewBinding.mapView) {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                android.graphics.Point screenPoint = new android.graphics.Point(Math.round(e.getX()), Math.round(e.getY()));

                final ListenableFuture<List<IdentifyGraphicsOverlayResult>> overlaysAsync = mMapView.identifyGraphicsOverlaysAsync(screenPoint, 10.0, false, 1);
                overlaysAsync.addDoneListener(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            List<IdentifyGraphicsOverlayResult> overlayResultList = overlaysAsync.get();
                            if (!overlayResultList.isEmpty()) {
                                IdentifyGraphicsOverlayResult identifyResult = overlayResultList.get(0);
                                Point point = identifyResult.getGraphicsOverlay().getExtent().getCenter();
                                for (PointDataBean dataBean : casePointList) {
                                    double parseLng = Double.parseDouble(dataBean.getLng());
                                    double parseLat = Double.parseDouble(dataBean.getLat());
                                    if (Math.abs(parseLng - point.getX()) <= Constant.DELTA_LNG_10 &&
                                            Math.abs(parseLat - point.getY()) <= Constant.DELTA_LAT_10) {
                                        Message message = weakReferenceHandler.obtainMessage();
                                        message.what = 2021051401;
                                        message.obj = dataBean.getId();
                                        weakReferenceHandler.sendMessage(message);
                                    }
                                }
                            } else {
                                weakReferenceHandler.sendEmptyMessage(2021051402);
                            }
                        } catch (InterruptedException | ExecutionException e) {
                            e.printStackTrace();
                        }
                    }
                });
                return super.onSingleTapConfirmed(e);
            }
        });
    }

    private static class WeakReferenceHandler extends Handler {
        private final WeakReference<CaseOnMapActivity> reference;

        private WeakReferenceHandler(CaseOnMapActivity activity) {
            reference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            CaseOnMapActivity onMapActivity = reference.get();
            switch (msg.what) {
                case 2021051401:
                    String caseId = (String) msg.obj;
                    new AlertControlDialog.Builder()
                            .setContext(onMapActivity)
                            .setTitle("查看案卷详情")
                            .setMessage("ID：" + caseId)
                            .setNegativeButton("取消")
                            .setPositiveButton("确定")
                            .setOnDialogButtonClickListener(new AlertControlDialog.OnDialogButtonClickListener() {
                                @Override
                                public void onConfirmClick() {
                                    Intent intent = new Intent(onMapActivity, CaseDetailActivity.class);
                                    intent.putExtra("id", caseId);
                                    onMapActivity.startActivity(intent);
                                }

                                @Override
                                public void onCancelClick() {

                                }
                            }).build().show();
                    break;
                case 2021051402:
                    ToastHelper.showToast("此附近无案卷，请重新选点", ToastHelper.WARING);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.handleRadioButton) {
            resetRadioButton(0);
        } else if (checkedId == R.id.verifyRadioButton) {
            resetRadioButton(1);
        } else if (checkedId == R.id.checkRadioButton) {
            resetRadioButton(2);
        }
    }

    private void resetRadioButton(int index) {
        switch (index) {
            case 0:
                viewBinding.handleRadioButton.setTextColor(ContextCompat.getColor(this, R.color.white));
                viewBinding.verifyRadioButton.setTextColor(ContextCompat.getColor(this, R.color.textColor));
                viewBinding.checkRadioButton.setTextColor(ContextCompat.getColor(this, R.color.textColor));

                //请求处置中数据
                caseHandleListPresenter.onReadyRetrofitRequest(null, null, 1000, 1);
                break;
            case 1:
                viewBinding.verifyRadioButton.setTextColor(ContextCompat.getColor(this, R.color.white));
                viewBinding.handleRadioButton.setTextColor(ContextCompat.getColor(this, R.color.textColor));
                viewBinding.checkRadioButton.setTextColor(ContextCompat.getColor(this, R.color.textColor));

                //请求待核实数据
                caseVerifyPresenter.onReadyRetrofitRequest(1000, 1);
                break;
            case 2:
                viewBinding.checkRadioButton.setTextColor(ContextCompat.getColor(this, R.color.white));
                viewBinding.verifyRadioButton.setTextColor(ContextCompat.getColor(this, R.color.textColor));
                viewBinding.handleRadioButton.setTextColor(ContextCompat.getColor(this, R.color.textColor));

                //请求待核查数据
                caseCheckPresenter.onReadyRetrofitRequest(1000, 1);
                break;
            default:
                break;
        }
    }

    /**
     * 处理中
     */
    @Override
    public void obtainCaseHandleList(CaseHandleBean resultBean) {
//        Log.d(TAG, "obtainCaseHandleList: " + new Gson().toJson(resultBean));
        clearPictureMarker();
        if (resultBean.isSuccess()) {
            List<CaseHandleBean.DataBean.RowsBean> rowsBeans = resultBean.getData().getRows();
            if (rowsBeans.size() >= 1) {
                List<PointDataBean> list = new ArrayList<>();
                List<Point> pointList = new ArrayList<>();
                for (CaseHandleBean.DataBean.RowsBean bean : rowsBeans) {
                    String lng = bean.getLng();
                    String lat = bean.getLat();
                    if (TextUtils.isEmpty(lng) || TextUtils.isEmpty(lat)) {
                        Log.d(TAG, "案卷经纬度异常");
                        continue;
                    }
                    relatePointToCase(list, bean.getId(), lng, lat);

                    Point point = new Point(Double.parseDouble(lng), Double.parseDouble(lat), SpatialReference.create(4326));
                    pointList.add(point);
                }
                addPictureMarker(pointList, Constant.CASE_HANDLE);
            } else {
                ToastHelper.showToast("当前没有[处理中]的案卷", ToastHelper.ERROR);
            }
        }
    }

    /**
     * 关联Marker和案卷
     *
     * @param list 临时数据集合
     */
    private void relatePointToCase(List<PointDataBean> list, String id, String lng, String lat) {
        PointDataBean casePoint = new PointDataBean();
        casePoint.setId(id);
        casePoint.setLng(lng);
        casePoint.setLat(lat);
        list.add(casePoint);
        casePointList = list;
    }

    /**
     * 待核实
     */
    @Override
    public void obtainVerifyListResult(CaseVerifyBean resultBean) {
//        Log.d(TAG, "obtainVerifyListResult: " + new Gson().toJson(resultBean));
        clearPictureMarker();
        if (resultBean.isSuccess()) {
            List<CaseVerifyBean.DataBean.RowsBean> rowsBeans = resultBean.getData().getRows();
            if (rowsBeans.size() >= 1) {
                List<PointDataBean> list = new ArrayList<>();
                List<Point> pointList = new ArrayList<>();
                for (CaseVerifyBean.DataBean.RowsBean bean : rowsBeans) {
                    String lng = bean.getLng();
                    String lat = bean.getLat();
                    if (TextUtils.isEmpty(lng) || TextUtils.isEmpty(lat)) {
                        Log.d(TAG, "案卷经纬度异常");
                        continue;
                    }
                    relatePointToCase(list, bean.getId(), lng, lat);

                    Point point = new Point(Double.parseDouble(lng), Double.parseDouble(lat), SpatialReference.create(4326));
                    pointList.add(point);
                }
                addPictureMarker(pointList, Constant.CASE_VERIFY);
            } else {
                ToastHelper.showToast("当前没有[待核实]的案卷", ToastHelper.ERROR);
            }
        }
    }

    /**
     * 待核查
     */
    @Override
    public void obtainCheckListResult(CaseCheckBean resultBean) {
//        Log.d(TAG, "obtainCheckListResult: " + new Gson().toJson(resultBean));
        clearPictureMarker();
        if (resultBean.isSuccess()) {
            List<CaseCheckBean.DataBean.RowsBean> rowsBeans = resultBean.getData().getRows();
            if (rowsBeans.size() >= 1) {
                List<PointDataBean> list = new ArrayList<>();
                List<Point> pointList = new ArrayList<>();
                for (CaseCheckBean.DataBean.RowsBean bean : rowsBeans) {
                    String lng = bean.getLng();
                    String lat = bean.getLat();
                    if (TextUtils.isEmpty(lng) || TextUtils.isEmpty(lat)) {
                        Log.d(TAG, "案卷经纬度异常");
                        continue;
                    }
                    relatePointToCase(list, bean.getId(), lng, lat);

                    Point point = new Point(Double.parseDouble(lng), Double.parseDouble(lat), SpatialReference.create(4326));
                    pointList.add(point);
                }
                addPictureMarker(pointList, Constant.CASE_CHECK);
            } else {
                ToastHelper.showToast("当前没有[待核查]的案卷", ToastHelper.ERROR);
            }
        }
    }

    private void addPictureMarker(List<Point> points, int caseStatus) {
        Bitmap caseBitmap;
        switch (caseStatus) {
            case Constant.CASE_HANDLE:
                caseBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.location_handle);
                break;
            case Constant.CASE_VERIFY:
                caseBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.location_verify);
                break;
            case Constant.CASE_CHECK:
                caseBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.location_check);
                break;
            default:
                caseBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.location_case);
                break;
        }
        for (Point point : points) {
            BitmapDrawable caseDrawable = new BitmapDrawable(getResources(), caseBitmap);
            PictureMarkerSymbol pictureMarker = new PictureMarkerSymbol(caseDrawable);
            pictureMarker.setHeight(24);
            pictureMarker.setWidth(24);
            pictureMarker.loadAsync();//异步加载Marker，防止阻塞

            GraphicsOverlay graphicsOverlay = new GraphicsOverlay();
            graphics = graphicsOverlay.getGraphics();
            graphics.add(new Graphic(point, pictureMarker));
            graphicsOverlays.add(graphicsOverlay);
        }
    }

    private void clearPictureMarker() {
        if (graphics != null && graphicsOverlays != null) {
            graphics.clear();
            graphicsOverlays.clear();
        }
    }

    protected void onResume() {
        super.onResume();
        viewBinding.mapView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        viewBinding.mapView.pause();
    }

    @Override
    protected void onDestroy() {
        viewBinding.mapView.dispose();
        if (caseHandleListPresenter != null) {
            caseHandleListPresenter.disposeRetrofitRequest();
        }
        if (caseVerifyPresenter != null) {
            caseVerifyPresenter.disposeRetrofitRequest();
        }
        if (caseCheckPresenter != null) {
            caseCheckPresenter.disposeRetrofitRequest();
        }
        super.onDestroy();
    }
}
