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

import android.content.Context;
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.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.BottomSheetRepairDetail;
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.PointRepairEvent;
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 RepairDetailBottomDialog extends BottomSheetDialog {
    @BindView(R.id.layout_bottom_sheet_detail)
    BottomSheetRepairDetail bottomSheetDetail;
    @BindView(R.id.btn_repair_route_route)
    Button btnRoute;
    @BindView(R.id.progress_loading)
    ProgressBar progressLoading;

    private Context mContext;

    private OfflinePathBean mPlanPoint;

    private PlanBean mPlan;

    private Point mLocation;

    private View.OnClickListener onRouteClickListener;

    private boolean mIsEditable = true;

    public RepairDetailBottomDialog(@NonNull Context context, @NonNull OfflinePathBean planPoint, @NonNull PlanBean planBean, @NonNull Point location) {
        super(context);
        mContext = context;
        mPlanPoint = planPoint;
        mPlan = planBean;
        mLocation = location;
    }


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

    @Subscribe
    public void onMessageEvent(PointRepairEvent event){
        if (event.message.equals(PointRepairEvent.POINT_REPAIR_REMOVED) ||
                event.message.equals(PointRepairEvent.REPAIR_ROUTE_SAVED) ||
                event.message.equals(PointRepairEvent.REPAIR_ROUTE_UPLOADED)){

            if (event.message.equals(PointRepairEvent.REPAIR_ROUTE_UPLOADED)
            || event.message.equals(PointRepairEvent.REPAIR_ROUTE_SAVED)){
                OfflinePathBean planPoint = (OfflinePathBean) event.arg;
                if (planPoint != null && planPoint.getTemp_id().equals(mPlanPoint.getTemp_id())
                && planPoint.getPlan_id().equals(mPlanPoint.getPlan_id())){
                    bottomSheetDetail.setEditable(false);
                }
            }
            bottomSheetDetail.refreshData();
        }
    }

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

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

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

        initView();
        initData();

        EventBusUtil.register(this);
    }

    private void initView(){

    }

    private void initData(){
        loadRepairPointInfo();
    }

    private void loadRepairPointInfo(){
        if (mPlanPoint != null && mPlan != null){
            bottomSheetDetail.setEditable(mIsEditable);
            bottomSheetDetail.initBottomSheetDetail(mPlanPoint,  mPlan, mLocation);
        }
    }

    public void setPlanEditable(boolean enable){
        mIsEditable = enable;
        if (bottomSheetDetail != null){
            bottomSheetDetail.setEditable(enable);
        }
    }

    @OnClick({R.id.btn_repair_route_route})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_repair_route_route:{
                if (onRouteClickListener != null){
                    onRouteClickListener.onClick(view);
                }
                this.hide();
            }
            break;
        }
    }

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