package com.casic.dcms.ui;

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

import androidx.core.content.ContextCompat;

import com.amap.api.location.AMapLocation;
import com.casic.dcms.R;
import com.casic.dcms.base.BaseActivity;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.LocationHelper;
import com.casic.dcms.utils.callback.ILocationListener;
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
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.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.PictureMarkerSymbol;
import com.esri.arcgisruntime.util.ListenableList;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

public class DisplayMapActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "DisplayMapActivity";
    @BindView(R.id.gisTopLayout)
    QMUITopBarLayout gisTopLayout;
    @BindView(R.id.mapView)
    MapView mapView;

    private double lng, lat;
    private QMUITipDialog loadingDialog;

    @Override
    public int initLayoutView() {
        return R.layout.activity_gis;
    }

    @Override
    protected void setupTopBarLayout() {
        gisTopLayout.setTitle("查看案卷位置").setTextColor(ContextCompat.getColor(this, R.color.white));
        gisTopLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.mainThemeColor));
        gisTopLayout.addLeftImageButton(R.drawable.ic_left_white, 0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initData() {
        String caseType = getIntent().getStringExtra("caseType");
//        Log.d(TAG, "caseType: " + caseType);
        lng = getIntent().getDoubleExtra("lng", 0);
        lat = getIntent().getDoubleExtra("lat", 0);

        mapView.setAttributionTextVisible(false);//去掉左下角属性标识

        ArcGISMap arcGISMap = new ArcGISMap(BasemapStyle.ARCGIS_STREETS);
        //创建底图、并设置底图图层
        ArcGISTiledLayer baseLayer = new ArcGISTiledLayer(Constant.BASE_MAP_SERVICE_URL);
        Basemap basemap = new Basemap(baseLayer);
        arcGISMap.setBasemap(basemap);
        mapView.setMap(arcGISMap);

        //社区 (ID: 13)
        ServiceFeatureTable communityFeatureTable = new ServiceFeatureTable(Constant.BASE_MAP_SERVICE_URL + "/13");
        communityFeatureTable.setFeatureRequestMode(ServiceFeatureTable.FeatureRequestMode.ON_INTERACTION_NO_CACHE);
        FeatureLayer communityLayer = new FeatureLayer(communityFeatureTable);
        if ("部件".equals(caseType)) {//部件点图层 (ID: 0)
            ServiceFeatureTable pointFeatureTable = new ServiceFeatureTable(Constant.COMPONENT_SERVICE_URL + "/0");
            pointFeatureTable.setFeatureRequestMode(ServiceFeatureTable.FeatureRequestMode.ON_INTERACTION_NO_CACHE);
            FeatureLayer pointLayer = new FeatureLayer(pointFeatureTable);
            arcGISMap.getOperationalLayers().addAll(Arrays.asList(communityLayer, pointLayer));
        }
        mapView.setMap(arcGISMap);

        loadingDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("定位中，请稍后")
                .create();
    }

    @Override
    public void initEvent() {
        /**
         * [116.598359, 28.247870, 0.000000, NaN] SR: 4326
         */
        Point point = new Point(lng, lat, mapView.getSpatialReference());
        mapView.setViewpointCenterAsync(point, 2800);//标注到指定经纬度并放大地图显示

        //添加普通小圆点Marker
//        addSimpleMarker(point);

        //添加图片Marker
        addPictureMarker(point);
    }

    private void addPictureMarker(Point point) {
        Bitmap caseBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.location_case);
        BitmapDrawable caseDrawable = new BitmapDrawable(getResources(), caseBitmap);
        PictureMarkerSymbol pictureMarker = new PictureMarkerSymbol(caseDrawable);
        pictureMarker.setHeight(24);
        pictureMarker.setWidth(24);
        //The image used for the symbol has a transparent buffer around it, so the offset is not simply height/2
        pictureMarker.setOffsetY(12);
        pictureMarker.loadAsync();

        Graphic graphic = new Graphic(point, pictureMarker);
        GraphicsOverlay mGraphicsOverlay = new GraphicsOverlay();
        ListenableList<Graphic> overlayGraphics = mGraphicsOverlay.getGraphics();
        ListenableList<GraphicsOverlay> graphicsOverlays = mapView.getGraphicsOverlays();
        overlayGraphics.add(graphic);
        graphicsOverlays.add(mGraphicsOverlay);
    }


    @OnClick({R.id.expandMapView, R.id.minusMapView, R.id.removeToLocalView})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.expandMapView:
                mapView.setViewpointScaleAsync(mapView.getMapScale() * 0.5);
                break;
            case R.id.minusMapView:
                mapView.setViewpointScaleAsync(mapView.getMapScale() * 2);
                break;
            case R.id.removeToLocalView:
                loadingDialog.show();
                LocationHelper.obtainCurrentLocationByGD(this, new ILocationListener() {
                    @Override
                    public void onLocationGet(Location location) {

                    }

                    @Override
                    public void onAMapLocationGet(AMapLocation aMapLocation) {
                        if (aMapLocation != null) {
                            mapView.setViewpointCenterAsync(new Point(aMapLocation.getLongitude(), aMapLocation.getLatitude()), 1000);
                            loadingDialog.dismiss();
                        }
                    }
                }, true);
                break;
            default:
                break;
        }
    }

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

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

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