package com.tofly.ghrq_route.common.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.geometry.Point;
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.MarkerSymbol;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.ui.BaseMapFileActivity;
import com.tofly.ghrq_route.common.ui.view.BottomSheetUploadPhotoDetail;
import com.tofly.ghrq_route.common.ui.view.MyAudioRecordButton;
import com.tofly.ghrq_route.common.util.ArcgisUtils.ArcgisUtils;
import com.tofly.ghrq_route.common.util.ArcgisUtils.SizeMarkerSymbol;
import com.tofly.ghrq_route.common.util.Location.LocationUtil_xu;
import com.tofly.ghrq_route.module.uploadPhoto.domain.ReportedPhotoBean;
import com.tofly.latte_core.base.IPresenter;

import java.util.concurrent.ExecutionException;

import butterknife.BindView;

/**
 * Created by Amu on 2020/8/10.
 */
public class UiPhotoReportedDetail extends BaseMapFileActivity {
    @BindView(R.id.mapview)
    MapView mapView;
    @BindView(R.id.maptool_location_ll)
    LinearLayout layoutLocation;
    @BindView(R.id.bottom_sheet_detail_header)
    RelativeLayout layoutBottomSheetHeader;
    @BindView(R.id.layout_bottom_sheet_detail)
    BottomSheetUploadPhotoDetail bottomSheetUploadPhotoDetail;

    private ReportedPhotoBean mPhotoReported = null;

    private GraphicsOverlay photoGraphicOverlay;


    @Override
    public void initView() {
        super.initView();
        // Initialize the helper class to use the Toolkit
        setDarkStatusBar();

        if (mapView != null) {
            double DEFAULTSCALE = 20000;
            mapView.setViewpointScaleAsync(DEFAULTSCALE);
            mapView.setBackgroundColor(Color.WHITE);
        }

        initBottomSheet();
    }

    @Override
    public void initNet() {
        super.initNet();

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            mPhotoReported = (ReportedPhotoBean) bundle.getSerializable(ReportedPhotoBean.REPORTED_PHOTO_BEAN_KEY);
            if (mPhotoReported != null) {
                showBottomSheetDetail();
            }
        }

        initListener();
    }

    private void initListener() {

    }

    private void initBottomSheet() {
        BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetUploadPhotoDetail);
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HALF_EXPANDED);
        bottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                    setBottomSheetBehavior(bottomSheet);
                } else if (newState == BottomSheetBehavior.STATE_HALF_EXPANDED) {
                    setBottomSheetBehavior(bottomSheet);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });
    }

    private void setBottomSheetBehavior(View bottomSheet) {
        BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetUploadPhotoDetail);
        if (bottomSheetBehavior != null) {
            Log.d("draged", bottomSheetBehavior.getState()  + "");
            if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {

            } else if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {

            } else if (bottomSheetBehavior.getState() == BottomSheetBehavior.STATE_HALF_EXPANDED) {

            }
        }
    }

    private void showBottomSheetDetail() {
        bottomSheetUploadPhotoDetail.initBottomSheetDetail(mPhotoReported);
        BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetUploadPhotoDetail);
        //bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HALF_EXPANDED);
        bottomSheetBehavior.setHideable(false);
    }

    private Graphic renderPointOnMap() {
        Graphic graphic = null;
        if (mapView != null && mPhotoReported != null) {
            Double lng = mPhotoReported.getLng();
            Double lat = mPhotoReported.getLat();
            if (lng != null && lat != null) {
                BitmapDrawable imageLoc = (BitmapDrawable) getResources().getDrawable(R.mipmap.danger_location);
                MarkerSymbol symbol = SizeMarkerSymbol.sizeSymbol(imageLoc, 18, 18);
                Point point = new Point(lng, lat, mapView.getSpatialReference());
                Graphic deviceGraphic = new Graphic(point, symbol);
                graphic = deviceGraphic;
                photoGraphicOverlay = new GraphicsOverlay();
                photoGraphicOverlay.getGraphics().add(deviceGraphic);
                mapView.getGraphicsOverlays().add(photoGraphicOverlay);
                ListenableFuture<Boolean> future = mapView.setViewpointCenterAsync(point, 5000);
                future.addDoneListener(() -> {
                    try {
                        Boolean isSuccess = future.get();
                        if (isSuccess) {
                            int offset = BaseApp.dip2px(150);
                            Point dstPoint =  ArcgisUtils.pointAddOffset(mapView, point, 0, offset);
                            if (dstPoint != null)
                                mapView.setViewpointCenterAsync(dstPoint);
                        }
                    } catch (ExecutionException | InterruptedException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
        return graphic;
    }

    @Override
    protected IPresenter createPresenter() {
        return null;
    }

    @Override
    public int setLayout() {
        return R.layout.ui_photo_report_detail;
    }

    @Override
    public boolean enableLoadMap() {
        return true;
    }

    @Override
    public boolean enableMoveMapListener() {
        return false;
    }

    @Override
    public boolean enableArcgisLocating() {
        return false;
    }

    @Override
    public boolean enableAmapLocationListener() {
        return true;
    }

    @Override
    public boolean enableMapMoveToLocation() {
        return false;
    }

    @Override
    public TextView getTextAddrView() {
        return null;
    }

    @Override
    public Button getAddPhotoBtn() {
        return null;
    }

    @Override
    public LinearLayout getPhotoGallery() {
        return null;
    }

    @Override
    public MyAudioRecordButton getAudioRecordBtn() {
        return null;
    }

    @Override
    public LocationUtil_xu.CoordinateClass getLocation() {
        return null;
    }

    @Override
    public void onBaseMapLoaded() {
        photoGraphicOverlay = null;
        renderPointOnMap();
    }

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