package com.casic.dcms.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.casic.dcms.R;
import com.casic.dcms.base.AndroidxBaseActivity;
import com.casic.dcms.databinding.ActivityGisBinding;
import com.casic.dcms.utils.ArcGisMapCreator;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.LocationHelper;
import com.casic.dcms.utils.OtherUtils;
import com.casic.dcms.utils.callback.ILocationListener;
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.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol;
import com.esri.arcgisruntime.util.ListenableList;

import java.util.ArrayList;
import java.util.List;

public class DisplayMapActivity extends AndroidxBaseActivity<ActivityGisBinding> {

    private final Context context = DisplayMapActivity.this;

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

    @Override
    protected void initData() {
        String caseType = getIntent().getStringExtra("caseType");
        double lng = getIntent().getDoubleExtra("lng", Constant.DEFAULT_LNG);
        double lat = getIntent().getDoubleExtra("lat", Constant.DEFAULT_LAT);
        viewBinding.mapView.setAttributionTextVisible(false); //去掉左下角属性标识
        ArcGISMap arcGISMap = new ArcGISMap(BasemapStyle.ARCGIS_STREETS);
        //创建底图、并设置底图图层
        Basemap baseMap = new Basemap(ArcGisMapCreator.createBaseLayer());
        arcGISMap.setBasemap(baseMap);
        viewBinding.mapView.setMap(arcGISMap);

        //社区 (ID: 13)
        ServiceFeatureTable communityFeatureTable = ArcGisMapCreator.createGridFeatureTable();
        communityFeatureTable.setFeatureRequestMode(ServiceFeatureTable.FeatureRequestMode.ON_INTERACTION_NO_CACHE);
        FeatureLayer communityLayer = new FeatureLayer(communityFeatureTable);
        if ("部件".equals(caseType)) { //部件点图层 (ID: 2)
            ServiceFeatureTable pointFeatureTable = ArcGisMapCreator.createOtherFeatureTable("部件点");
            pointFeatureTable.setFeatureRequestMode(ServiceFeatureTable.FeatureRequestMode.ON_INTERACTION_NO_CACHE);
            FeatureLayer pointLayer = new FeatureLayer(pointFeatureTable);

            List<FeatureLayer> layerList = new ArrayList<>();
            layerList.add(communityLayer);
            layerList.add(pointLayer);
            arcGISMap.getOperationalLayers().addAll(layerList);
        }
        viewBinding.mapView.setMap(arcGISMap);
        /**
         * [116.598359, 28.247870, 0.000000, NaN] SR: 4326
         */
        Point point = new Point(lng, lat, SpatialReference.create(4326));
        viewBinding.mapView.setViewpointCenterAsync(point, 2800.0); //标注到指定经纬度并放大地图显示
        //添加图片Marker
        addPictureMarker(point);
    }

    @Override
    protected void initEvent() {
        viewBinding.expandMapView.setOnClickListener(v -> viewBinding.mapView.setViewpointScaleAsync(viewBinding.mapView.getMapScale() * 0.5));
        viewBinding.minusMapView.setOnClickListener(v -> viewBinding.mapView.setViewpointScaleAsync(viewBinding.mapView.getMapScale() * 2));
        viewBinding.removeToLocalView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OtherUtils.showLoadingDialog(context, "定位中，请稍后");
                LocationHelper.obtainCurrentLocationByGD(context, new ILocationListener() {
                    @Override
                    public void onLocationGet(Location location) {

                    }

                    @Override
                    public void onAMapLocationGet(AMapLocation aMapLocation) {
                        double[] gcjToWgs = LocationHelper.gcjToWgs(aMapLocation.getLongitude(), aMapLocation.getLatitude());
                        Point point = new Point(gcjToWgs[0], gcjToWgs[1], SpatialReference.create(4326));
                        addPictureMarker(point);
                        viewBinding.mapView.setViewpointCenterAsync(point, 2800.0);
                        OtherUtils.dismissLoadingDialog();
                    }
                }, true);
            }
        });
    }

    private void addPictureMarker(Point point) {
        Bitmap caseBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.location_verify);
        BitmapDrawable caseDrawable = new BitmapDrawable(getResources(), caseBitmap);
        PictureMarkerSymbol pictureMarker = new PictureMarkerSymbol(caseDrawable);
        pictureMarker.setWidth(24f);
        pictureMarker.setHeight(24f);
        pictureMarker.loadAsync();
        Graphic graphic = new Graphic(point, pictureMarker);
        GraphicsOverlay mGraphicsOverlay = new GraphicsOverlay();
        ListenableList<Graphic> overlayGraphics = mGraphicsOverlay.getGraphics();
        ListenableList<GraphicsOverlay> graphicsOverlays = viewBinding.mapView.getGraphicsOverlays();
        overlayGraphics.add(graphic);
        graphicsOverlays.add(mGraphicsOverlay);
    }

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

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

    @Override
    protected void onDestroy() {
        viewBinding.mapView.dispose();
        super.onDestroy();
    }
}