package com.emato.ich.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import com.emato.ich.MainActivity;
import com.emato.ich.R;
import com.emato.ich.api.ICSPClient;
import com.emato.ich.contant.ICSPConstant;
import com.emato.ich.contant.SystemConfigConstant;
import com.emato.ich.databinding.FragmentMyKeyBoardBinding;
import com.emato.ich.databinding.FragmentSendKeyBoardBinding;
import com.emato.ich.entity.vo.ResponseData;
import com.emato.ich.entity.vo.ResponseTokenVo;
import com.emato.ich.local.LocalStorage;
import com.emato.ich.utils.BaseUtils;
import com.emato.ich.utils.ButtonUtils;
import com.emato.ich.utils.JacksonUtils;
import com.emato.ich.utils.Log;
import com.emato.ich.utils.LoggingUtils;
import com.emato.ich.utils.NavUtils;
import com.emato.ich.utils.StringUtils;
import com.emato.ich.utils.TimeOutUtils;
import com.emato.ich.utils.ToastUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 投递员登录页面
 */
public class SendKeyBoardFragment extends Fragment {

    private static final String TAG = SendKeyBoardFragment.class.getName();
    private FragmentSendKeyBoardBinding binding;
    private CountDownTimer timer;
    private long start;

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        start = System.currentTimeMillis();
        binding = FragmentSendKeyBoardBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }





    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        long end = System.currentTimeMillis();
        Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
        super.onViewCreated(view, savedInstanceState);


        BaseUtils.disableEditText(binding.editTextTextPersonName4);
        BaseUtils.disableEditText(binding.editTextTextPersonName5);

        try {
            MainActivity activity = (MainActivity) getActivity();
            timer = TimeOutUtils.timeout(activity, SendKeyBoardFragment.this, binding.timeout, 90);
            timer.start();
        } catch (Exception e) {
            Log.e(TAG, "onViewCreated: 倒计时异常! ", e);
            LoggingUtils.sendErrorLog("业务异常: 投递员登录页面倒计时异常! ", e);
        }

        // 如果有配置账号密码, 账号定死
        String loginPhone = null;
        String loginType = null; // store 门店 courier 快递员投递 collect 快递员揽件
        try {
            MainActivity activity = (MainActivity) getActivity();
            loginPhone = activity.getConfigMap().get(SystemConfigConstant.cabinet_login_phone);
            loginType = activity.getBundleMap().get(CourierFragment.class.getName()).getString("loginType");
            if("store".equals(loginType)){
                // 如果不是门店店员进来 则将注册二维码设置为隐藏
                view.findViewById(R.id.qrCodeImage).setVisibility(View.GONE);
                view.findViewById(R.id.sendingTitleImage).setVisibility(View.GONE);

                view.findViewById(R.id.delivery_code).setTranslationX(-200);
                view.findViewById(R.id.editTextTextPersonName4).setTranslationX(-200);
                view.findViewById(R.id.editTextTextPersonName5).setTranslationX(-200);
                view.findViewById(R.id.button_confirm).setTranslationX(-200);
            }else{

            }
        } catch (Exception e) {
            Log.e(TAG, "onViewCreated: 配置登录手机号出错! ", e);
            LoggingUtils.sendErrorLog("业务异常: 配置登录手机号出错! ", e);
        }
        if (!StringUtils.isNullOrEmpty(loginPhone) && "store".equals(loginType)) {
            String loginPhoneShow = toHidePhone(loginPhone);
            binding.editTextTextPersonName4.setText(loginPhoneShow);
            binding.editTextTextPersonName4.setEnabled(false);
        } else {
            // 4 手机
            binding.editTextTextPersonName4.setOnFocusChangeListener((view1, hasFocus) -> {
                if (hasFocus) {
                    binding.keyBoard.inputButton0.setOnClickListener(v -> {
                        int length = binding.editTextTextPersonName4.getText().length();
                        if (length >= 11) {
                            ToastUtils.make(getContext(), "手机号不能超过11位! ");
                            return;
                        }
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        binding.editTextTextPersonName4.getText().insert(start, "0");
                    });
                    binding.keyBoard.inputButton1.setOnClickListener(v -> {
                        int length = binding.editTextTextPersonName4.getText().length();
                        if (length >= 11) {
                            ToastUtils.make(getContext(), "手机号不能超过11位! ");
                            return;
                        }
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        binding.editTextTextPersonName4.getText().insert(start, "1");
                    });
                    binding.keyBoard.inputButton2.setOnClickListener(v -> {
                        int length = binding.editTextTextPersonName4.getText().length();
                        if (length >= 11) {
                            ToastUtils.make(getContext(), "手机号不能超过11位! ");
                            return;
                        }
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        binding.editTextTextPersonName4.getText().insert(start, "2");
                    });
                    binding.keyBoard.inputButton3.setOnClickListener(v -> {
                        int length = binding.editTextTextPersonName4.getText().length();
                        if (length >= 11) {
                            ToastUtils.make(getContext(), "手机号不能超过11位! ");
                            return;
                        }
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        binding.editTextTextPersonName4.getText().insert(start, "3");
                    });
                    binding.keyBoard.inputButton4.setOnClickListener(v -> {
                        int length = binding.editTextTextPersonName4.getText().length();
                        if (length >= 11) {
                            ToastUtils.make(getContext(), "手机号不能超过11位! ");
                            return;
                        }
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        binding.editTextTextPersonName4.getText().insert(start, "4");
                    });
                    binding.keyBoard.inputButton5.setOnClickListener(v -> {
                        int length = binding.editTextTextPersonName4.getText().length();
                        if (length >= 11) {
                            ToastUtils.make(getContext(), "手机号不能超过11位! ");
                            return;
                        }
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        binding.editTextTextPersonName4.getText().insert(start, "5");
                    });
                    binding.keyBoard.inputButton6.setOnClickListener(v -> {
                        int length = binding.editTextTextPersonName4.getText().length();
                        if (length >= 11) {
                            ToastUtils.make(getContext(), "手机号不能超过11位! ");
                            return;
                        }
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        binding.editTextTextPersonName4.getText().insert(start, "6");
                    });
                    binding.keyBoard.inputButton7.setOnClickListener(v -> {
                        int length = binding.editTextTextPersonName4.getText().length();
                        if (length >= 11) {
                            ToastUtils.make(getContext(), "手机号不能超过11位! ");
                            return;
                        }
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        binding.editTextTextPersonName4.getText().insert(start, "7");
                    });
                    binding.keyBoard.inputButton8.setOnClickListener(v -> {
                        int length = binding.editTextTextPersonName4.getText().length();
                        if (length >= 11) {
                            ToastUtils.make(getContext(), "手机号不能超过11位! ");
                            return;
                        }
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        binding.editTextTextPersonName4.getText().insert(start, "8");
                    });
                    binding.keyBoard.inputButton9.setOnClickListener(v -> {
                        int length = binding.editTextTextPersonName4.getText().length();
                        if (length >= 11) {
                            ToastUtils.make(getContext(), "手机号不能超过11位! ");
                            return;
                        }
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        binding.editTextTextPersonName4.getText().insert(start, "9");
                    });
                    binding.keyBoard.inputButtonCancel.setOnClickListener(v -> {
                        Editable text = binding.editTextTextPersonName4.getText();
                        int start = binding.editTextTextPersonName4.getSelectionStart();
                        if (text.length() > 0) {
                            if (start > 0) {
                                text.delete(start - 1, start);
                            }
                            binding.editTextTextPersonName4.setText(text, TextView.BufferType.EDITABLE);
                            binding.editTextTextPersonName4.setSelection(start - 1 < 0 ? text.length() : start - 1);
                        }
                    });
                    // 长按删除
                    binding.keyBoard.inputButtonCancel.setOnLongClickListener(v -> {
                        binding.editTextTextPersonName4.getText().clear();
                        return false;
                    });
                }
            });
        }

        String finalLoginPhone = loginPhone;
        String finalLoginType = loginType;
        binding.keyBoard.inputButtonConfirm.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            // 登录逻辑  editTextTextPersonName4 手机 editTextTextPersonName5 密码
            String sendPhone = "";
            if ("courier".equals(finalLoginType) || "collect".equals(finalLoginType)){
                sendPhone = binding.editTextTextPersonName4.getText().toString();
            }else {
                sendPhone = !StringUtils.isNullOrEmpty(finalLoginPhone) ? finalLoginPhone : binding.editTextTextPersonName4.getText().toString();
            }
            String password = binding.editTextTextPersonName5.getText().toString();

            Pattern phonePattern = Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");
            Pattern pwdPattern = Pattern.compile("^[0-9]{8,16}$");

            if (phonePattern.matcher(sendPhone).matches() && pwdPattern.matcher(password).matches()) {
                String finalSendPhone = sendPhone;
                ICSPClient.login(sendPhone, password, new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        // TODO 登录失败处理
                        ToastUtils.make(getContext(), "登录失败！网络异常！");
                        Log.e(TAG, "onFailure: 登录失败! call: " + call.timeout().toString(), e);
                        LoggingUtils.sendErrorLog("业务异常: 登录失败！网络异常！ ", e);
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        ((MainActivity) getActivity()).runOnUiThread(() -> {
                            String responseStr = ICSPClient.isSuccessfulAndParseResponse(response);
                            if (!StringUtils.isNullOrEmpty(responseStr)) {
                                try {
                                    ResponseData<ResponseTokenVo> responseData = JacksonUtils.objectmapper.readValue(responseStr, new TypeReference<ResponseData<ResponseTokenVo>>() {
                                    });
                                    ResponseTokenVo data = responseData.getData();

                                    if (responseData.getCode().equals(ICSPConstant.OK)) {
                                        LocalStorage.Session session = new LocalStorage.Session();
                                        session.setPhone(finalSendPhone);
                                        session.setTime(System.currentTimeMillis());
                                        session.setToken(data.getToken());
                                        MainActivity activity = (MainActivity) getActivity();
                                        String pageType = activity.getBundleMap().get(MainFragment.class.getName()).getString("pageType");
                                        String loginType = activity.getBundleMap().get(CourierFragment.class.getName()).getString("loginType");
                                        LocalStorage.getInstance().cleanSession(activity,false);
                                        LocalStorage.getInstance().setSession(session);

                                        Bundle pageTypeBundle = new Bundle();
                                        pageTypeBundle.putString("pageType",pageType);
                                        activity.getBundleMap().put(MainFragment.class.getName(), pageTypeBundle);
                                        Bundle loginTypeBundle = new Bundle();
                                        loginTypeBundle.putString("loginType",loginType);
                                        activity.getBundleMap().put(CourierFragment.class.getName(), loginTypeBundle);

                                        // 登录成功
                                        ToastUtils.make(getContext(), "登录成功！");
                                        if (null != timer) {
                                            timer.cancel();
                                        }
                                        if ("store".equals(finalLoginType) || "courier".equals(finalLoginType)){
                                            Bundle isPayBundle = new Bundle();
                                            isPayBundle.putString("isPay",data.getIsPay());
                                            isPayBundle.putString("payAmount",data.getPayAmount().toString());
                                            activity.getBundleMap().put(SendKeyBoardFragment.class.getName(), isPayBundle);
                                            // "30"为供应链账号，定为管理员账号
                                            if("store".equals(finalLoginType) ){
                                                if("10".equals(data.getDelType()) || "30".equals(data.getDelType())){
                                                    NavUtils.navigate(SendKeyBoardFragment.this, R.id.action_sendFragment_to_sendMainFragment);
                                                } else {
                                                    ToastUtils.make(getContext(), "请用门店账号登录！");
                                                    return;
                                                }
                                            }else {
                                                if("20".equals(data.getDelType()) || "30".equals(data.getDelType())){
                                                    NavUtils.navigate(SendKeyBoardFragment.this, R.id.action_sendFragment_to_sendMainFragment);
                                                } else {
                                                    ToastUtils.make(getContext(), "请用快递员账号登录！");
                                                    return;
                                                }
                                            }

//                                            if ("0".equals(data.getIsPay())){
//                                                NavUtils.navigate(SendKeyBoardFragment.this, R.id.action_sendFragment_to_paymentFragment);
//                                            }else {
//                                                NavUtils.navigate(SendKeyBoardFragment.this, R.id.action_sendFragment_to_sendMainFragment);
//                                            }
                                        }else{
                                            if(!"20".equals(data.getDelType())){
                                                ToastUtils.make(getContext(), "请用快递员账号登录！");
                                                return;
                                            }
                                            NavUtils.navigate(SendKeyBoardFragment.this, R.id.action_sendFragment_to_collectMaterialsFragment);
                                        }

                                    } else {
                                        ToastUtils.make(getContext(), responseData.getMsg());
                                        Log.w(TAG, "onResponse: " + responseData.getMsg());
                                    }

                                } catch (JsonProcessingException e) {
                                    Log.e(TAG, "onViewCreated: 解析登录响应信息错误! response body: " + responseStr, e);
                                    ToastUtils.make(getContext(), "登录失败！");
                                    LoggingUtils.sendErrorLog("业务异常: 登录失败！", e);
                                } catch (ClassCastException e) {
                                    Log.e(TAG, "onResponse: 解析登录响应信息类型转换错误! response body: " + responseStr, e);
                                    ToastUtils.make(getContext(), "登录失败！");
                                    LoggingUtils.sendErrorLog("业务异常: 登录失败！", e);
                                } catch (RuntimeException e) {
                                    Log.e(TAG, "onResponse: 登录失败! ", e);
                                    ToastUtils.make(getContext(), "登录失败！");
                                    LoggingUtils.sendErrorLog("业务异常: 登录失败！未知错误! ", e);
                                }
                            } else {
                                // 登录失败
                                ToastUtils.make(getContext(), "服务异常！请稍后重试！");
                            }
                        });
                    }
                });
            } else if (!phonePattern.matcher(sendPhone).matches()) {
                ToastUtils.make(getContext(), "输入正确的手机号！");
            } else if (!pwdPattern.matcher(password).matches()) {
                ToastUtils.make(getContext(), "密码格式错误！请输入8-16位的数字！");
            }
        });
        binding.buttonConfirm.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            // 登录逻辑  editTextTextPersonName4 手机 editTextTextPersonName5 密码
            String sendPhone = "";
            if ("courier".equals(finalLoginType) || "collect".equals(finalLoginType)){
                sendPhone = binding.editTextTextPersonName4.getText().toString();
            }else {
                sendPhone = !StringUtils.isNullOrEmpty(finalLoginPhone) ? finalLoginPhone : binding.editTextTextPersonName4.getText().toString();
            }
            String password = binding.editTextTextPersonName5.getText().toString();

            Pattern phonePattern = Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");
            Pattern pwdPattern = Pattern.compile("^[0-9]{8,16}$");

            if (phonePattern.matcher(sendPhone).matches() && pwdPattern.matcher(password).matches()) {
                String finalSendPhone = sendPhone;
                ICSPClient.login(sendPhone, password, new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        // TODO 登录失败处理
                        ToastUtils.make(getContext(), "登录失败！网络异常！");
                        Log.e(TAG, "onFailure: 登录失败! call: " + call.timeout().toString(), e);
                        LoggingUtils.sendErrorLog("业务异常: 登录失败！网络异常！ ", e);
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        ((MainActivity) getActivity()).runOnUiThread(() -> {
                            String responseStr = ICSPClient.isSuccessfulAndParseResponse(response);
                            if (!StringUtils.isNullOrEmpty(responseStr)) {
                                try {
                                    ResponseData<ResponseTokenVo> responseData = JacksonUtils.objectmapper.readValue(responseStr, new TypeReference<ResponseData<ResponseTokenVo>>() {
                                    });
                                    ResponseTokenVo data = responseData.getData();

                                    if (responseData.getCode().equals(ICSPConstant.OK)) {
                                        LocalStorage.Session session = new LocalStorage.Session();
                                        session.setPhone(finalSendPhone);
                                        session.setTime(System.currentTimeMillis());
                                        session.setToken(data.getToken());
                                        MainActivity activity = (MainActivity) getActivity();
                                        String pageType = activity.getBundleMap().get(MainFragment.class.getName()).getString("pageType");
                                        String loginType = activity.getBundleMap().get(CourierFragment.class.getName()).getString("loginType");
                                        LocalStorage.getInstance().cleanSession(activity,false);
                                        LocalStorage.getInstance().setSession(session);

                                        Bundle pageTypeBundle = new Bundle();
                                        pageTypeBundle.putString("pageType",pageType);
                                        activity.getBundleMap().put(MainFragment.class.getName(), pageTypeBundle);
                                        Bundle loginTypeBundle = new Bundle();
                                        loginTypeBundle.putString("loginType",loginType);
                                        activity.getBundleMap().put(CourierFragment.class.getName(), loginTypeBundle);

                                        // 登录成功
                                        ToastUtils.make(getContext(), "登录成功！");
                                        if (null != timer) {
                                            timer.cancel();
                                        }
                                        if ("store".equals(finalLoginType) || "courier".equals(finalLoginType)){
                                            Bundle isPayBundle = new Bundle();
                                            isPayBundle.putString("isPay",data.getIsPay());
                                            activity.getBundleMap().put(SendKeyBoardFragment.class.getName(), isPayBundle);
                                            // "30"为供应链账号，定为管理员账号
                                            if("store".equals(finalLoginType) ){
                                                if("10".equals(data.getDelType()) || "30".equals(data.getDelType())){
                                                    NavUtils.navigate(SendKeyBoardFragment.this, R.id.action_sendFragment_to_sendMainFragment);
                                                } else {
                                                    ToastUtils.make(getContext(), "请用门店账号登录！");
                                                    return;
                                                }
                                            }else {
                                                if("20".equals(data.getDelType()) || "30".equals(data.getDelType())){
                                                    NavUtils.navigate(SendKeyBoardFragment.this, R.id.action_sendFragment_to_sendMainFragment);
                                                } else {
                                                    ToastUtils.make(getContext(), "请用快递员账号登录！");
                                                    return;
                                                }
                                            }

//                                            if ("0".equals(data.getIsPay())){
//                                                NavUtils.navigate(SendKeyBoardFragment.this, R.id.action_sendFragment_to_paymentFragment);
//                                            }else {
//                                                NavUtils.navigate(SendKeyBoardFragment.this, R.id.action_sendFragment_to_sendMainFragment);
//                                            }
                                        }else{
                                            if(!"20".equals(data.getDelType())){
                                                ToastUtils.make(getContext(), "请用快递员账号登录！");
                                                return;
                                            }
                                            NavUtils.navigate(SendKeyBoardFragment.this, R.id.action_sendFragment_to_collectMaterialsFragment);
                                        }

                                    } else {
                                        ToastUtils.make(getContext(), responseData.getMsg());
                                        Log.w(TAG, "onResponse: " + responseData.getMsg());
                                    }

                                } catch (JsonProcessingException e) {
                                    Log.e(TAG, "onViewCreated: 解析登录响应信息错误! response body: " + responseStr, e);
                                    ToastUtils.make(getContext(), "登录失败！");
                                    LoggingUtils.sendErrorLog("业务异常: 登录失败！", e);
                                } catch (ClassCastException e) {
                                    Log.e(TAG, "onResponse: 解析登录响应信息类型转换错误! response body: " + responseStr, e);
                                    ToastUtils.make(getContext(), "登录失败！");
                                    LoggingUtils.sendErrorLog("业务异常: 登录失败！", e);
                                } catch (RuntimeException e) {
                                    Log.e(TAG, "onResponse: 登录失败! ", e);
                                    ToastUtils.make(getContext(), "登录失败！");
                                    LoggingUtils.sendErrorLog("业务异常: 登录失败！未知错误! ", e);
                                }
                            } else {
                                // 登录失败
                                ToastUtils.make(getContext(), "服务异常！请稍后重试！");
                            }
                        });
                    }
                });
            } else if (!phonePattern.matcher(sendPhone).matches()) {
                ToastUtils.make(getContext(), "输入正确的手机号！");
            } else if (!pwdPattern.matcher(password).matches()) {
                ToastUtils.make(getContext(), "密码格式错误！请输入8-16位的数字！");
            }
        });
        // 密码
        binding.editTextTextPersonName5.setOnFocusChangeListener((view1, hasFocus) -> {
            AtomicBoolean caseSmallFlag = new AtomicBoolean(true);
            
            binding.keyBoard.inputButton0.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "0");
            });
            binding.keyBoard.inputButton1.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "1");
            });
            binding.keyBoard.inputButton2.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "2");
            });
            binding.keyBoard.inputButton3.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "3");
            });
            binding.keyBoard.inputButton4.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "4");
            });
            binding.keyBoard.inputButton5.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "5");
            });
            binding.keyBoard.inputButton6.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "6");
            });
            binding.keyBoard.inputButton7.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "7");
            });
            binding.keyBoard.inputButton8.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "8");
            });
            binding.keyBoard.inputButton9.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "9");
            });

            // todo：绑定字母按键
            binding.keyBoard.inputButtonQ.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "Q" : "q");
            });
            binding.keyBoard.inputButtonW.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "W" : "w");
            });
            binding.keyBoard.inputButtonE.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "E" : "e");
            });
            binding.keyBoard.inputButtonR.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "R" : "r");
            });
            binding.keyBoard.inputButtonT.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "T" : "t");
            });
            binding.keyBoard.inputButtonY.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "Y" : "y");
            });
            binding.keyBoard.inputButtonU.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "U" : "u");
            });
            binding.keyBoard.inputButtonI.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "I" : "i");
            });
            binding.keyBoard.inputButtonO.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "O" : "o");
            });
            binding.keyBoard.inputButtonP.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "P" : "p");
            });
            binding.keyBoard.inputButtonA.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "A" : "a");
            });
            binding.keyBoard.inputButtonS.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "S" : "s");
            });
            binding.keyBoard.inputButtonD.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "D" : "d");
            });
            binding.keyBoard.inputButtonF.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "F" : "f");
            });
            binding.keyBoard.inputButtonG.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "G" : "g");
            });
            binding.keyBoard.inputButtonH.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "H" : "h");
            });
            binding.keyBoard.inputButtonJ.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "J" : "j");
            });
            binding.keyBoard.inputButtonK.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "K" : "k");
            });
            binding.keyBoard.inputButtonL.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "L" : "l");
            });
            binding.keyBoard.inputButtonZ.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "Z" : "z");
            });
            binding.keyBoard.inputButtonX.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位!");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "X" : "x");
            });
            binding.keyBoard.inputButtonC.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "C" : "c");
            });
            binding.keyBoard.inputButtonV.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "V" : "v");
            });
            binding.keyBoard.inputButtonB.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "B" : "b");
            });
            binding.keyBoard.inputButtonN.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "N" : "n");
            });
            binding.keyBoard.inputButtonM.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, caseSmallFlag.get() ? "M" : "m");
            });
            //  符号
            binding.keyBoard.symbolButton1.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "-");
            });
            binding.keyBoard.symbolButton2.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "/");
            });
            binding.keyBoard.symbolButton3.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, ":");
            });
            binding.keyBoard.symbolButton4.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "+");
            });
            binding.keyBoard.symbolButton5.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "(");
            });
            binding.keyBoard.symbolButton6.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, ")");
            });
            binding.keyBoard.symbolButton7.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "#");
            });
            binding.keyBoard.symbolButton8.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "@");
            });
            binding.keyBoard.symbolButton9.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "密码最多不能超过16位! ");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, ".");
            });
            binding.keyBoard.symbolButton10.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.editTextTextPersonName5.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "?");
                    return;
                }
                int start = binding.editTextTextPersonName5.getSelectionStart();
                binding.editTextTextPersonName5.getText().insert(start, "!");
            });


            // 大小写按键绑定  默认大写
            binding.keyBoard.inputButtonCaseSmall.setOnClickListener(v -> {
                if (caseSmallFlag.get()) {
                    caseSmallFlag.set(false);
                } else {
                    caseSmallFlag.set(true);
                }
                binding.keyBoard.inputButtonCaseSmall.setText(caseSmallFlag.get() ? "大写" : "小写");
                // 设置键盘字母大小写
                binding.keyBoard.inputButtonQ.setText(caseSmallFlag.get() ? "Q" : "q");
                binding.keyBoard.inputButtonW.setText(caseSmallFlag.get() ? "W" : "w");
                binding.keyBoard.inputButtonE.setText(caseSmallFlag.get() ? "E" : "e");
                binding.keyBoard.inputButtonR.setText(caseSmallFlag.get() ? "R" : "r");
                binding.keyBoard.inputButtonT.setText(caseSmallFlag.get() ? "T" : "t");
                binding.keyBoard.inputButtonY.setText(caseSmallFlag.get() ? "Y" : "y");
                binding.keyBoard.inputButtonU.setText(caseSmallFlag.get() ? "U" : "u");
                binding.keyBoard.inputButtonI.setText(caseSmallFlag.get() ? "I" : "i");
                binding.keyBoard.inputButtonO.setText(caseSmallFlag.get() ? "O" : "o");
                binding.keyBoard.inputButtonP.setText(caseSmallFlag.get() ? "P" : "p");
                binding.keyBoard.inputButtonA.setText(caseSmallFlag.get() ? "A" : "a");
                binding.keyBoard.inputButtonS.setText(caseSmallFlag.get() ? "S" : "s");
                binding.keyBoard.inputButtonD.setText(caseSmallFlag.get() ? "D" : "d");
                binding.keyBoard.inputButtonF.setText(caseSmallFlag.get() ? "F" : "f");
                binding.keyBoard.inputButtonG.setText(caseSmallFlag.get() ? "G" : "g");
                binding.keyBoard.inputButtonH.setText(caseSmallFlag.get() ? "H" : "h");
                binding.keyBoard.inputButtonJ.setText(caseSmallFlag.get() ? "J" : "j");
                binding.keyBoard.inputButtonK.setText(caseSmallFlag.get() ? "K" : "k");
                binding.keyBoard.inputButtonL.setText(caseSmallFlag.get() ? "L" : "l");
                binding.keyBoard.inputButtonZ.setText(caseSmallFlag.get() ? "Z" : "z");
                binding.keyBoard.inputButtonX.setText(caseSmallFlag.get() ? "X" : "x");
                binding.keyBoard.inputButtonC.setText(caseSmallFlag.get() ? "C" : "c");
                binding.keyBoard.inputButtonV.setText(caseSmallFlag.get() ? "V" : "v");
                binding.keyBoard.inputButtonB.setText(caseSmallFlag.get() ? "B" : "b");
                binding.keyBoard.inputButtonN.setText(caseSmallFlag.get() ? "N" : "n");
                binding.keyBoard.inputButtonM.setText(caseSmallFlag.get() ? "M" : "m");
            });

            binding.keyBoard.inputButtonCancel.setOnClickListener(v -> {
                Editable text = binding.editTextTextPersonName5.getText();
                int start = binding.editTextTextPersonName5.getSelectionStart();
                if (text.length() > 0) {
                    if (start > 0) {
                        text.delete(start - 1, start);
                    }
                    binding.editTextTextPersonName5.setText(text, TextView.BufferType.EDITABLE);
                    binding.editTextTextPersonName5.setSelection(start - 1 < 0 ? text.length() : start - 1);
                }
            });
            // 长按删除
            binding.keyBoard.inputButtonCancel.setOnLongClickListener(v -> {
                binding.editTextTextPersonName5.getText().clear();
                return false;
            });

        });

        binding.returnBtn.setOnClickListener(view1 -> {
            if (ButtonUtils.isFastClick()) {
                return;
            }
            if (null != timer) {
                timer.cancel();
            }
            NavUtils.navigate(this, R.id.action_sendFragment_to_mainFragment);
        });



        // 默认选中密码输入
        if (!StringUtils.isNullOrEmpty(binding.editTextTextPersonName4.getText().toString())) {
            binding.editTextTextPersonName5.requestFocus();
        } else {
            binding.editTextTextPersonName4.requestFocus();
        }
    }

    /**
     * 隐藏部分信息
     *
     * @param loginPhone 投递员手机号
     * @return 隐藏后的手机号
     */
    private String toHidePhone(String loginPhone) {
        StringBuilder stringBuilder = new StringBuilder();
        char[] chars = loginPhone.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i > 2 && i < 8) {
                stringBuilder.append('*');
            } else {
                stringBuilder.append(chars[i]);
            }
        }
        return stringBuilder.toString();
    }

    @Override
    public void onDestroyView() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onDestroyView();
        binding = null;

        Log.i(TAG, TAG + "被销毁。。。");
    }

}