package com.casic.dcms.view.map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.amap.api.location.AMapLocation;
import com.casic.dcms.R;
import com.casic.dcms.databinding.ActivityGisBinding;
import com.casic.dcms.utils.ArcGisMapCreator;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.utils.LocationHelper;
import com.casic.dcms.utils.ViewGroupKit;
import com.casic.dcms.utils.callback.ILocationListener;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReferences;
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 com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.utils.LoadingDialog;

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

public class DisplayMapActivity extends AndroidxBaseActivity<ActivityGisBinding> {

    private final Context context = DisplayMapActivity.this;

    @Override
    protected void setupTopBarLayout() {
        ViewGroupKit.initImmersionBar(binding.rootView, this, true, R.color.white);

        binding.titleBarLayout.titleView.setText("位置查看");
        binding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        String caseType = getIntent().getStringExtra("caseType");
        double lng = getIntent().getDoubleExtra("lng", LocaleConstant.DEFAULT_LNG);
        double lat = getIntent().getDoubleExtra("lat", LocaleConstant.DEFAULT_LAT);
        binding.mapView.setAttributionTextVisible(false); //去掉左下角属性标识
        ArcGISMap arcGISMap = new ArcGISMap(BasemapStyle.ARCGIS_STREETS);
        //创建底图、并设置底图图层
        Basemap baseMap = new Basemap(ArcGisMapCreator.createBaseLayer());
        arcGISMap.setBasemap(baseMap);
        binding.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);
        }
        binding.mapView.setMap(arcGISMap);
        /**
         * [116.598359, 28.247870, 0.000000, NaN] SR: 4326
         */
        Point point = new Point(lng, lat, SpatialReferences.getWgs84());
        binding.mapView.setViewpointCenterAsync(point, 2800.0); //标注到指定经纬度并放大地图显示
        //添加图片Marker
        addPictureMarker(point);
    }

    @Override
    protected void initEvent() {
        binding.expandMapView.setOnClickListener(v -> binding.mapView.setViewpointScaleAsync(binding.mapView.getMapScale() * 0.5));
        binding.minusMapView.setOnClickListener(v -> binding.mapView.setViewpointScaleAsync(binding.mapView.getMapScale() * 2));
        binding.removeToLocalView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoadingDialog.show(DisplayMapActivity.this, "定位中，请稍后");
                LocationHelper.get().getCurrentLocation(context, new ILocationListener() {

                    @Override
                    public void onAMapLocationGet(AMapLocation aMapLocation) {
                        double[] gcjToWgs = LocationHelper.get().gcjToWgs(aMapLocation.getLongitude(), aMapLocation.getLatitude());
                        Point point = new Point(gcjToWgs[0], gcjToWgs[1], SpatialReferences.getWgs84());
                        addPictureMarker(point);
                        binding.mapView.setViewpointCenterAsync(point, 2800.0);
                        LoadingDialog.dismiss();
                    }
                }, 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 = binding.mapView.getGraphicsOverlays();
        overlayGraphics.add(graphic);
        graphicsOverlays.add(mGraphicsOverlay);
    }

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

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

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