package com.yidian.bcommon.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.yidian.bcommon.R;


public class DialogBuilder {
    private String navigateTxt = "取消";
    private String positiveTxt = "确定";
    private String titleTxt = "";
    private String msgTxt = "";
    private boolean showNavigate = true;
    private boolean showPositive = false;
    private boolean showTitle = false;
    private boolean showMsg = false;
    private View.OnClickListener navigateClickListener = null;
    private View.OnClickListener positiveClickListener = null;
    private boolean canceledOnTouchOutside = false;

    public CommonDialog build(Context context) {
        CommonDialog dialog = new CommonDialog(context);
        dialog.navigateTxt = navigateTxt;
        dialog.positiveTxt = positiveTxt;
        dialog.showNavigate = showNavigate;
        dialog.showPositive = showPositive;
        dialog.titleTxt = titleTxt;
        dialog.msgTxt = msgTxt;
        dialog.showTitle = showTitle;
        dialog.showMsg = showMsg;
        dialog.navigateClickListener = navigateClickListener;
        dialog.positiveClickListener = positiveClickListener;
        dialog.canceledOnTouchOutside = canceledOnTouchOutside;
        return dialog;
    }


    public DialogBuilder setNavigateTxt(String navigateTxt) {
        this.navigateTxt = navigateTxt;
        return this;
    }

    public DialogBuilder setPositiveTxt(String positiveTxt) {
        this.positiveTxt = positiveTxt;
        return this;
    }

    public DialogBuilder showNavigate(boolean showNavigate, View.OnClickListener clickListener) {
        this.showNavigate = showNavigate;
        this.navigateClickListener = clickListener;
        return this;
    }

    public DialogBuilder showPositive(boolean showPositive, View.OnClickListener clickListener) {
        this.showPositive = showPositive;
        this.positiveClickListener = clickListener;
        return this;
    }

    public DialogBuilder showNavigate(boolean showNavigate, String txt, View.OnClickListener clickListener) {
        this.showNavigate = showNavigate;
        this.navigateClickListener = clickListener;
        this.navigateTxt = txt;
        return this;
    }

    public DialogBuilder showNavigate(boolean showNavigate) {
        this.showNavigate = showNavigate;
        return this;
    }

    public DialogBuilder showPositive(boolean showPositive, String txt, View.OnClickListener clickListener) {
        this.showPositive = showPositive;
        this.positiveClickListener = clickListener;
        this.positiveTxt = txt;
        return this;
    }

    public DialogBuilder showTitle(boolean showTitle, String txt) {
        this.showTitle = showTitle;
        this.titleTxt = txt;
        return this;
    }

    public DialogBuilder showMsg(boolean showMsg, String txt) {
        this.showMsg = showMsg;
        this.msgTxt = txt;
        return this;
    }


    public void setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
        this.canceledOnTouchOutside = canceledOnTouchOutside;
    }

    public static class CommonDialog extends Dialog {
        private String navigateTxt = "取消";
        private String positiveTxt = "确定";
        private String titleTxt = "确定";
        private String msgTxt = "确定";
        private boolean showNavigate = true;
        private boolean showPositive = false;
        private boolean showTitle = false;
        private boolean showMsg = false;
        private boolean canceledOnTouchOutside = false;
        private TextView tv_title;
        private TextView tv_message;
        private TextView tv_navigate;
        private TextView tv_positive;
        private View.OnClickListener navigateClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        };
        private View.OnClickListener positiveClickListener = null;

        public CommonDialog(@NonNull Context context) {
            this(context, R.style.base_dialog);
        }

        public CommonDialog(@NonNull Context context, int themeResId) {
            super(context, themeResId);
        }


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_common);
            //按空白处不能取消动画
            setCanceledOnTouchOutside(canceledOnTouchOutside);
            //初始化界面控件
            initView();
            //初始化界面控件的事件
            initEvent();
        }

        private void initView() {
            tv_title = findViewById(R.id.tv_title);
            tv_message = findViewById(R.id.tv_message);
            tv_navigate = findViewById(R.id.tv_navigate);
            tv_positive = findViewById(R.id.tv_positive);
            if (showPositive) {
                tv_positive.setVisibility(View.VISIBLE);
                tv_positive.setText(positiveTxt);
                if (positiveClickListener != null) {
                    tv_positive.setOnClickListener(positiveClickListener);
                } else {
                    tv_positive.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                        }
                    });
                }
            } else {
                tv_positive.setVisibility(View.GONE);
            }
            if (showNavigate) {
                tv_navigate.setVisibility(View.VISIBLE);
                tv_navigate.setText(navigateTxt);
                if (navigateClickListener != null) {
                    tv_navigate.setOnClickListener(navigateClickListener);
                } else {
                    tv_navigate.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                        }
                    });
                }
            } else {
                tv_navigate.setVisibility(View.GONE);
            }
            if (showNavigate || showPositive) {
                findViewById(R.id.ll_btn).setVisibility(View.VISIBLE);
            } else {
                findViewById(R.id.ll_btn).setVisibility(View.GONE);
            }
            showTitle = showTitle && titleTxt != null && titleTxt.length() > 0;
            showMsg = showMsg && msgTxt != null && msgTxt.length() > 0;
            if (showTitle || showMsg) {
                findViewById(R.id.ll_content).setVisibility(View.VISIBLE);
            } else {
                findViewById(R.id.ll_content).setVisibility(View.GONE);
            }
            if (showTitle) {
                tv_title.setVisibility(View.VISIBLE);
                tv_title.setText(titleTxt);
            } else {
                tv_title.setVisibility(View.GONE);
            }
            if (showMsg) {
                tv_message.setVisibility(View.VISIBLE);
                tv_message.setText(msgTxt);
            } else {
                tv_message.setVisibility(View.GONE);
            }
        }

        private void initEvent() {

        }


        public void setNavigateTxt(String navigateTxt) {
            this.navigateTxt = navigateTxt;
        }

        public void setPositiveTxt(String positiveTxt) {
            this.positiveTxt = positiveTxt;
        }

        public void setTitleTxt(String titleTxt) {
            this.titleTxt = titleTxt;
        }

        public void setMsgTxt(String msgTxt) {
            this.msgTxt = msgTxt;
        }

        public void setShowNavigate(boolean showNavigate) {
            this.showNavigate = showNavigate;
        }

        public void setShowPositive(boolean showPositive) {
            this.showPositive = showPositive;
        }

        public void setShowTitle(boolean showTitle) {
            this.showTitle = showTitle;
        }

        public void setShowMsg(boolean showMsg) {
            this.showMsg = showMsg;
        }

        public void setNavigateClickListener(View.OnClickListener navigateClickListener) {
            this.navigateClickListener = navigateClickListener;
        }

        public void setPositiveClickListener(View.OnClickListener positiveClickListener) {
            this.positiveClickListener = positiveClickListener;
        }
    }

}
