package com.zhaohaoting.framework.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.abs.dialog.BaseDialog;
import com.zhaohaoting.framework.databinding.DialogTipsCustomBinding;

/**
 * author：Created by zhaohaoting on 2018/8/15
 * email：526309416@qq.com
 * desc：提示dialog
 */
public class TipsCustomDialog extends BaseDialog implements View.OnClickListener {

    private String title; // 对话框标题
    private CharSequence message; // 对话框内容
    private String confirm_btnText; // 按钮名称“确定”
    private String cancel_btnText; // 按钮名称“取消”
    private boolean cancelable = true; //是否可以取消
    private DialogInterface.OnClickListener confirm_btnClickListener;
    private DialogInterface.OnClickListener cancel_btnClickListener;

    public TipsCustomDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void initContentView(ViewDataBinding viewDataBinding) {
        DialogTipsCustomBinding binding = (DialogTipsCustomBinding) viewDataBinding;
        binding.setOnClickEvent(this);
        binding.title.setVisibility(TextUtils.isEmpty(title) ? View.GONE : View.VISIBLE);
        if (!TextUtils.isEmpty(title)) binding.title.setText(title);
        if (!TextUtils.isEmpty(confirm_btnText)) binding.confirmBtn.setText(confirm_btnText);
        if (!TextUtils.isEmpty(cancel_btnText)) binding.cancelBtn.setText(cancel_btnText);
        binding.message.setText(message);
        setCancelable(cancelable);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.dialog_tips_custom;
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.cancel_btn) {
            dismiss();
            if (cancel_btnClickListener != null)
                cancel_btnClickListener.onClick(this, DialogInterface.BUTTON_NEGATIVE);

        } else if (i == R.id.confirm_btn) {
            if (confirm_btnClickListener != null)
                confirm_btnClickListener.onClick(this, DialogInterface.BUTTON_POSITIVE);

        }
    }

    public static class Builder {

        private final TipsCustomDialog dialog;

        public Builder(Context context) {
            dialog = new TipsCustomDialog(context);
        }


        /* 设置对话框信息 */
        public Builder setMessage(CharSequence message) {
            dialog.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @param message
         * @return
         */
        public Builder setMessage(int message) {
            dialog.message = dialog.context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            dialog.title = (String) dialog.context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            dialog.title = title;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param confirm_btnText
         * @return
         */
        public Builder setPositiveButton(int confirm_btnText,
                                         DialogInterface.OnClickListener listener) {
            dialog.confirm_btnText = (String) dialog.context.getText(confirm_btnText);
            dialog.confirm_btnClickListener = listener;
            return this;
        }

        /**
         * Set the positive button and it's listener
         *
         * @param confirm_btnText
         * @return
         */
        public Builder setPositiveButton(String confirm_btnText,
                                         DialogInterface.OnClickListener listener) {
            dialog.confirm_btnText = confirm_btnText;
            dialog.confirm_btnClickListener = listener;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            dialog.cancelable = cancelable;
            return this;
        }

        /**
         * Set the negative button resource and it's listener
         *
         * @param cancel_btnText
         * @return
         */
        public Builder setNegativeButton(int cancel_btnText,
                                         DialogInterface.OnClickListener listener) {
            dialog.cancel_btnText = (String) dialog.context.getText(cancel_btnText);
            dialog.cancel_btnClickListener = listener;
            return this;
        }

        /**
         * Set the negative button and it's listener
         *
         * @param cancel_btnText
         * @return
         */
        public Builder setNegativeButton(String cancel_btnText,
                                         DialogInterface.OnClickListener listener) {
            dialog.cancel_btnText = cancel_btnText;
            dialog.cancel_btnClickListener = listener;
            return this;
        }

        public TipsCustomDialog create() {
            return dialog;
        }
    }
}
