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

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;

import com.esri.arcgisruntime.geometry.Point;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.ui.view.BottomSheetDangerDetail;
import com.tofly.ghrq_route.module.plan.domain.OfflinePathBean;
import com.tofly.ghrq_route.module.plan.domain.PlanBean;
import com.tofly.ghrq_route.module.trouble.TroubleTool;
import com.tofly.ghrq_route.module.trouble.controller.TroubleCallBack;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.common.ui.activity.UiReportedClear;
import com.tofly.ghrq_route.common.util.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/6/19.
 */

public class DangerDetailBottomDialog extends BottomSheetDialog {
    @BindView(R.id.layout_bottom_sheet_detail)
    BottomSheetDangerDetail bottomSheetDetail;
    @BindView(R.id.btn_trouble_route_route)
    Button btnRoute;
    @BindView(R.id.progress_loading)
    ProgressBar progressLoading;

    private Activity mContext;

    private TroubleBean mTrouble;

    private String mTroubleId;

    private PlanBean mPlan;

    private OfflinePathBean mPlanPoint;

    private View.OnClickListener onRouteClickListener;

    private Point mLocation;

    private boolean mIsClearEnabled = false;

/*    public DangerDetailBottomDialog(@NonNull Activity context, @NonNull TroubleBean troubleBean, OfflinePathBean planPoint, PlanBean planBean, @NonNull Point location) {
        super(context);
        mContext = context;
        mTrouble = troubleBean;
        mPlanPoint = planPoint;
        mPlan = planBean;
        mLocation = location;
    }*/

    public DangerDetailBottomDialog(@NonNull Activity context, @NonNull String troubleid, OfflinePathBean planPoint, PlanBean planBean, @NonNull Point location) {
        super(context);
        mContext = context;
        mTroubleId = troubleid;
        mPlanPoint = planPoint;
        mPlan = planBean;
        mLocation = location;
    }

    public void setOnRouteClickListener(View.OnClickListener listener){
        onRouteClickListener = listener;
    }

    @Subscribe
    public void onMessageEvent(UiReportedClear.ReportClearMessageEvent messageEvent){
        if ( messageEvent.message.equals(UiReportedClear.REPORT_ROUTE_UPLOADED)||
                messageEvent.message.equals(UiReportedClear.REPORT_ROUTE_SAVED) ){
            bottomSheetDetail.loadPlanTroubleRecord();
        }else if ( messageEvent.message.equals(UiReportedClear.REPORT_CLEAR_UPLOADED)||
                messageEvent.message.equals(UiReportedClear.REPORT_CLEAR_SAVED) ){
            bottomSheetDetail.loadPlanTroubleClearRecord();
        }
    }

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

        setContentView(R.layout.dialog_plan_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();

        EventBusUtil.register(this);
    }

    private void initView(){
        ImageView imageClose = bottomSheetDetail.findViewById(R.id.bottom_sheet_detail_header_close);
        imageClose.setVisibility(View.GONE);
    }

    private void initData(){
        if (mTrouble == null){
            loadTroubleInfo();
        }
    }

    private void loadTroubleInfo(){
        if (mTroubleId != null && !mTroubleId.isEmpty()){
            progressLoading.setVisibility(View.VISIBLE);
            TroubleTool troubleTool = TroubleTool.getInstance();
            troubleTool.loadTroubleInfo(mContext, mTroubleId, new TroubleCallBack(){
                @Override
                public void onGetTroubleInfo(boolean isSuccess, int errorCode, String errMsg, TroubleBean troubleBean) {
                    super.onGetTroubleInfo(isSuccess, errorCode, errMsg, troubleBean);
                    progressLoading.setVisibility(View.GONE);
                    if (isSuccess){
                        if (troubleBean != null){
                            mTrouble = troubleBean;
                        }else {
                            mTrouble = new TroubleBean();
                            mTrouble.setId(mTroubleId);
                            mTrouble.setSave_state(TroubleBean.TROUBLE_STATE);
                        }
                        bottomSheetDetail.setClearEnable(mIsClearEnabled);
                        bottomSheetDetail.initBottomSheetDetail(mTrouble, mPlanPoint, mPlan, mLocation);
                    }else {
                        mIsClearEnabled = false;
                        hide();
                    }
                }
            });
        }
    }

    public void setDangerClearEnable(boolean enable){
        mIsClearEnabled = enable;
    }

    @OnClick({R.id.btn_trouble_route_route})
    public void onClick(View view){
        if (view.getId() == R.id.btn_trouble_route_route) {
            if (onRouteClickListener != null) {
                onRouteClickListener.onClick(view);
            }
            this.hide();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBusUtil.unregister(this);
    }
}
