package com.unis.sxzcl.widget.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.unis.sxzcl.R;
import com.xyxsbj.mylibrary.tool.ETTool;
import com.xyxsbj.mylibrary.tool.StringTool;

/**
 * 包名: com.unis.sxzcl.widget.dialog
 * 类名: InputDialog
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/11 14:12
 * 更新人:
 * 更新时间: 2020/12/11 14:12
 * 更新说明:
 * 版本号: 1.0
 */
public class InputDialog {

    Context mContext;
    private SystemDialog mDialog;
    private EditText mEtContent;
    private TextView mBtnSubmit;

    private CallBack mCallBack;

    public InputDialog(@NonNull Context context) {
        mContext = context;
        initDialog();
    }

    /**
     * 初始化对话框
     */
    private void initDialog() {
        mDialog = new SystemDialog(mContext)
                .setTitle("请输入内容")
                .setContentView(getView())
                .setOnShowListener(dialog -> {
                    // 弹出软键盘
                    ETTool.showSoftKeyboard(mEtContent);
                })
                .setDismissListener(dialog -> {
                    // 隐藏软键盘
                    ETTool.closeSoftKeybord(mEtContent);
                });

    }

    /**
     * 获取自定义布局
     */
    private View getView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_input, null, false);
        mEtContent = view.findViewById(R.id.et_content);

        mBtnSubmit = view.findViewById(R.id.btn_submit);
        mBtnSubmit.setOnClickListener(v -> {

            String phone = ETTool.getData(mEtContent);
            if (StringTool.isEmpty(phone)) {
                mEtContent.setError("请输入内容");
                return;
            }
            if (mCallBack != null)
                mCallBack.onSubmitListener(mDialog, phone);
        });
        return view;
    }


    public InputDialog setContent(String content) {
        mEtContent.setText(content);
        return this;
    }

    /**
     * 设置提交监听事件
     */
    public InputDialog setCallBack(CallBack callBack) {
        mCallBack = callBack;
        return this;
    }

    /**
     * 显示对话框
     */
    public InputDialog show() {
        mDialog.show();
        return this;
    }

    /**
     * 隐藏对话框
     */
    public InputDialog dismiss() {
        mDialog.dismiss();
        return this;
    }

    /**
     * 获取对话框
     *
     * @return
     */
    public SystemDialog getDialog() {
        return mDialog;
    }

    public interface CallBack {
        public void onSubmitListener(SystemDialog dialog, String data);
    }
}
