package com.fxy.common;

import android.content.Context;
import android.content.DialogInterface;

import com.fxy.R;
import com.fxy.detection.bean.BtnInfoBean;

import java.util.ArrayList;
import java.util.List;

public  class ConfirmDialog {
    private final Context mContext;
    private List<BtnInfoBean> listBtn = new ArrayList<>();
    private OnCallbackListener listener;
    private DialogInterface dialog;
    private boolean cancelable= false;
    public ConfirmDialog(Context context, List<BtnInfoBean> list){
        mContext = context;
        listBtn = list;
    }

    public void  setCancelable(boolean flag){
        cancelable = flag;
    }


    public void showDialog(String center){
        showDialog(center,"");
    }

    public void showDialog(String center,String title){
        // 使用AlertDialog.Builder来创建对话框的构建器
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mContext);
        builder.setCancelable(cancelable).setMessage(center);

        if (!title.isEmpty()){
            builder.setTitle(title);
        }

        int len = listBtn!=null ? listBtn.size() : 0 ;
        if (len>0){

            builder.setPositiveButton(listBtn.get(0).getTxt(), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if (listener == null){
                        dialogInterface.dismiss();
                    }else{
                        dialog = dialogInterface;
                        listener.OnCallback(listBtn.get(0).getValue(),0);
                    }
                }
            });

            if (len>1){
                builder.setNeutralButton(listBtn.get(1).getTxt(), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (listener == null){
                            dialogInterface.dismiss();
                        }else{
                            dialog = dialogInterface;
                            listener.OnCallback(listBtn.get(1).getValue(),1);
                        }
                    }
                });
            }

            if (len>2){
                builder.setNegativeButton(listBtn.get(2).getTxt(), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (listener == null){
                            dialogInterface.dismiss();
                        }else{
                            dialog = dialogInterface;
                            listener.OnCallback(listBtn.get(2).getValue(),2);
                        }

                    }
                });
            }

        }else{
            builder.setPositiveButton(R.string.str_submit, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
        }

        // 创建并显示对话框
        android.app.AlertDialog dialog = builder.create();
        dialog.show();
    }

    public void dismiss(){
        if (dialog!=null){
            dialog.dismiss();
            dialog = null;
        }
    }



    public interface OnCallbackListener{
        void OnCallback(String value,int position);
    }

    // 提供注册事件监听的方法
    public void setOnCallbackListener(OnCallbackListener onCallbackListener) {
        this.listener = onCallbackListener;
    }
}
