package com.baoshi.swms.part;

import android.app.Dialog;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baoshi.swms.R;

/**
 * 定位在软键盘上方的仿VX输入框
 *
 * @author Zhendong Zhou
 * @date   2021/12/3 18:06
 */
public class InputEditDialog extends Dialog {
    public interface OnTextSendListener {
        void onTextSend(String msg);
    }
    private final EditText messageTextView; //文本框
    private final Context mContext; //上下文
    private final InputMethodManager imm;//键盘
    private int mLastDiff = 0;//最后的标记
    private OnTextSendListener mOnTextSendListener;//文本发送事件监听对象
    protected InputMethodManager inputManager;//输入框类型

    /**
     *
     * @param context 上下文
     * @param theme   theme的样式ID
     * @param hint    输入框提示文本
     */
    public InputEditDialog(Context context, int theme, String hint, int InputType) {
        super(context, theme); //调用父类构造
        mContext = context;    //上下文传递给全局
        setContentView(R.layout.part_edit_keydown);//设置上下文的视图
        inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);//输入框服务
        messageTextView = (EditText) findViewById(R.id.et_input_message);//文本框对象
        messageTextView.setBackgroundColor(getContext().getResources().getColor(R.color.black));
        messageTextView.setInputType(InputType);//文本类型
        messageTextView.setHint(hint);//提示
        messageTextView.getBackground().setColorFilter(context.getResources().getColor(R.color.info), PorterDuff.Mode.CLEAR);//下划线颜色
        ImageView confirmBtn = (ImageView) findViewById(R.id.confrim_btn); //发送按钮对象
        imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); //键盘服务
        confirmBtn.setOnClickListener(new View.OnClickListener() { //监听点击发送事件
            @Override
            public void onClick(View view) {
                String msg = messageTextView.getText().toString().trim(); //获取输入框文本
                if (!TextUtils.isEmpty(msg)) {
                    mOnTextSendListener.onTextSend(msg);
                    imm.showSoftInput(messageTextView, InputMethodManager.SHOW_FORCED);
                    imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
                    messageTextView.setText("");
                    dismiss();
                }
                messageTextView.setText(null);
            }
        });
        messageTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {//键盘输入事件监听
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch (actionId) {
                    case KeyEvent.KEYCODE_ENDCALL:
                    case KeyEvent.KEYCODE_ENTER:
                        if (messageTextView.getText().length() > 0) {
                            imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
                            dismiss();
                        } else Toast.makeText(mContext, "input can not be empty!", Toast.LENGTH_LONG).show();
                        return true;
                    case KeyEvent.KEYCODE_BACK:
                        dismiss();
                        return false;
                    default:
                        return false;
                }
            }
        });
        messageTextView.setOnKeyListener(new View.OnKeyListener() {//按键监听
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                return false;
            }
        });
        RelativeLayout rlDlg = (RelativeLayout) findViewById(R.id.rl_outside_view);//视图容器布局对象
        rlDlg.setOnClickListener(new View.OnClickListener() {//点击容器关闭输入框与键盘
            @Override
            public void onClick(View v) {
                if (v.getId() != R.id.rl_inputdlg_view) dismiss();
            }
        });
        final LinearLayout rldlgview = (LinearLayout) findViewById(R.id.rl_inputdlg_view);//输入框容器对象
        rldlgview.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {//监听容器变更事件
            @Override
            public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
                Rect r = new Rect();
                //获取当前界面可视部分
                getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //获取屏幕的高度
                int screenHeight = getWindow().getDecorView().getRootView().getHeight();
                //获取键盘的高度的， 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
                int heightDifference = screenHeight - r.bottom;
                if (heightDifference <= 0 && mLastDiff > 0)dismiss();
                mLastDiff = heightDifference;
            }
        });
        rldlgview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imm.hideSoftInputFromWindow(messageTextView.getWindowToken(), 0);
                dismiss();
            }
        });
    }

    public void setmOnTextSendListener(OnTextSendListener onTextSendListener) {//文本发送对象监听
        this.mOnTextSendListener = onTextSendListener;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        //dismiss之前重置mLastDiff值避免下次无法打开
        mLastDiff = 0;
    }

    @Override
    public void show() {
        super.show();
        showSoftKeyboard(messageTextView);
    }
    private void showSoftKeyboard(EditText et) {
        if (et == null)return;
        et.requestFocus();
        inputManager.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
    }
}
