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

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.esri.arcgisruntime.geometry.Point;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.lcp.arecyclerview.widget.ARecycleView;
import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.ui.activity.UiAdjustDeviceRepair;
import com.tofly.ghrq_route.common.ui.activity.UiCommonDeviceRepair;
import com.tofly.ghrq_route.module.plan.PlanTool;
import com.tofly.ghrq_route.module.plan.domain.OfflinePathBean;
import com.tofly.ghrq_route.module.plan.domain.PlanBean;
import com.tofly.ghrq_route.module.planPointRepair.domain.PlanPointRepair;
import com.tofly.ghrq_route.module.planPointRepair.ui.UiRouteDeviceCheck;
import com.tofly.ghrq_route.common.ui.adapter.PlanPointListAdapter;
import com.tofly.ghrq_route.common.util.Location.LocationUtil;
import com.tofly.ghrq_route.common.util.Location.LocationUtil_xu;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/12/27.
 */

public class PlanPointDetailBottomDialog extends BottomSheetDialog {
    @BindView(R.id.bottom_sheet_detail_header)
    RelativeLayout bottomSheetDetailHeader;
    @BindView(R.id.recycler_view)
    ARecycleView recyclerView;
    @BindView(R.id.progress_loading)
    ProgressBar progressLoading;

    private Context mContext;

    private PlanBean mPlan;

    private List<OfflinePathBean> mPlanPointList;

    private String mSelectedPointId;

    private PlanPointListAdapter planPointListAdapter;

    private Point mLocation;

    public PlanPointDetailBottomDialog(@NonNull Context context, List<OfflinePathBean> planPoints, String selectPointId, @NonNull PlanBean plan, @NonNull Point location) {
        super(context);
        mContext = context;
        mPlan = plan;
        mSelectedPointId = selectPointId;
        mPlanPointList = planPoints;
        mLocation = location;
    }

    private OnRouteClickListener onRouteClickListener;
    public void setOnRouteClickListener(OnRouteClickListener listener){
        onRouteClickListener = listener;
    }

    public interface OnRouteClickListener {
        void onRoute(View view, OfflinePathBean planPoint);
    }

    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener listener){
        onItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onClick(View view, OfflinePathBean planPoint);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_plan_point_bottom);
        ButterKnife.bind(this);

        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
            lp.width = (d.widthPixels); // 高度设置为屏幕的0.6
            dialogWindow.setAttributes(lp);
        }

        initView();
        initData();
        initListener();
    }

    private void initView() {

    }

    private void initData() {
        loadPlanPointList();
    }

    private void initListener(){
        planPointListAdapter.setOnItemClickListener(new ARecyclerBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (onItemClickListener != null){
                    OfflinePathBean planPoint = planPointListAdapter.getItem(position);
                    if (planPoint != null)
                        onItemClickListener.onClick(view, planPoint);
                }
                hide();
            }
        });

        planPointListAdapter.setNavigateClickListener(new PlanPointListAdapter.OnNavigateClickListener() {
            @Override
            public void onNavigateClick(View view, int position) {
                if (onRouteClickListener != null){
                    OfflinePathBean planPoint = planPointListAdapter.getItem(position);
                    if (planPoint != null)
                        onRouteClickListener.onRoute(view, planPoint);
                }
                hide();
            }
        });

        planPointListAdapter.setRecordClickListener((view, position) -> {
            OfflinePathBean planPoint = planPointListAdapter.getItem(position);
            if (planPoint != null){
                if (mPlan.isDeviceType()){
                    PlanTool planTool = PlanTool.getInstance();
                    boolean isValid = planTool.checkPlanValid(mContext, planPoint, mLocation, mPlan);
                    if (isValid) {
                        gotoUiPointRepair(planPoint);
                    }
                }/*else if (mPlan.isDangerDeviceType()){
                    gotoUiClear(planPoint);
                }*/
            }
            hide();
        });
    }

    private void loadPlanPointList(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        planPointListAdapter = new PlanPointListAdapter(mContext, mPlanPointList, mPlan);
        recyclerView.setAdapter(planPointListAdapter);
        if (mSelectedPointId != null){
            int selectPos = planPointListAdapter.selectItem(mSelectedPointId);
            recyclerView.scrollToPosition(selectPos);
        }
    }

    private void gotoUiPointRepair(OfflinePathBean planPoint) {
        if (planPoint != null && mPlan != null){
            Bundle bundle = new Bundle();

            bundle.putSerializable(OfflinePathBean.PLAN_POINT_BEAN_KEY, planPoint);
            bundle.putSerializable(PlanBean.PLAN_BEAN_KEY, mPlan);
            bundle.putBoolean(PlanPointRepair.POINT_REPAIR_IS_NEW_KEY, true);
            bundle.putBoolean(PlanPointRepair.POINT_REPAIR_IS_LOCAL_KEY, false);
            LocationUtil_xu.CoordinateClass coordinate = new LocationUtil_xu.CoordinateClass(mLocation.getX(), mLocation.getY());
            bundle.putSerializable(LocationUtil.CURRENT_LOCATION_KEY, coordinate);
            boolean isCommonDevice = mPlan.isCommonDeviceType();
            Intent intent = new Intent();
            if (isCommonDevice) {
                /*阀门设备*/
                intent.setClass(mContext, UiCommonDeviceRepair.class);
            }else {
                intent.setClass(mContext, UiAdjustDeviceRepair.class);
            }
            intent.putExtras(bundle);
            mContext.startActivity(intent);
        }
    }
}
