package com.emato.ich.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.InputType;
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.fragment.app.Fragment;

import com.cherry.sdk.controller.utils.ScanGunKeyEventHelper;
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.ScanGunConstant;
import com.emato.ich.databinding.FragmentCollectMaterialsBinding;
import com.emato.ich.entity.vo.ExpressSendOrderPickUpDto;
import com.emato.ich.entity.vo.PreparedOrderResponseVo;
import com.emato.ich.entity.vo.PreparedOrderVo;
import com.emato.ich.entity.vo.ResponseData;
import com.emato.ich.entity.vo.TakeParcelVo;
import com.emato.ich.local.LocalStorage;
import com.emato.ich.utils.BaseUtils;
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 com.fasterxml.jackson.databind.ObjectMapper;

import org.jetbrains.annotations.NotNull;

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

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

/**
 * 输入投递信息
 */
public class CollectMaterialsFragment extends Fragment {

    private static final String TAG = CollectMaterialsFragment.class.getName();
    private FragmentCollectMaterialsBinding binding;
    private PreparedOrderResponseVo data;
    private final AtomicInteger mailNoLock = new AtomicInteger(1);
    private final AtomicInteger submitLock = new AtomicInteger(1);
    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 = FragmentCollectMaterialsBinding.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。");
        BaseUtils.disableEditText(binding.mailNo);

        try {
            MainActivity activity = (MainActivity) getActivity();
            timer = TimeOutUtils.timeout(activity, CollectMaterialsFragment.this, binding.timeout, 90);
            timer.start();
        } catch (Exception e) {
            Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
            LoggingUtils.sendErrorLog("业务异常: 输入投递信息页面倒计时出现异常! ", e);
        }

        Bundle arguments = null;
        String sectionType = null;
        try {
            MainActivity activity = (MainActivity) getActivity();
            binding.mailNo.setInputType(InputType.TYPE_NULL);   
            // 扫描枪监听 扫描运单号
            activity.scanGunKeyEventHelper = new ScanGunKeyEventHelper(s -> {
                // 扫码成功回调
                Log.d(TAG, "onScanSuccess: " + s);

                if (!StringUtils.isNullOrEmpty(s)) {
                    for (String pattern : ScanGunConstant.getPatterns()) {
                        Matcher matcher = Pattern.compile(pattern).matcher(s);
                        if (!matcher.matches()) {
                            ToastUtils.make(getContext(), "请将条形码置于扫码枪前! ");
                            return;
                        }
                    }
                }

                if (mailNoLock.get() == 1) {
                    // 禁用软件盘
                    Editable text = binding.mailNo.getText();
                    text.clear();
                    binding.mailNo.setText(s);
                    mailNoLock.incrementAndGet();
                }
            });

        } catch (RuntimeException e) {
            Log.e(TAG, "onViewCreated: InputInfoFragment页面获取ChooseCabinetFragment传值错误! ", e);
            LoggingUtils.sendErrorLog("业务异常: InputInfoFragment页面获取ChooseCabinetFragment传值错误! ", e);
        }

        // TODO 离线逻辑, 暂时不做

        if (submitLock.get() == 1) {
            submitLock.incrementAndGet();
            binding.keyBoard.inputButtonConfirm.setOnClickListener(view1 -> {
                Bundle bundle = new Bundle();
                String mailNo = binding.mailNo.getText().toString();
//            String phone = binding.delivererPhone.getText().toString();

               if (StringUtils.isNullOrEmpty(mailNo)) {
                    ToastUtils.make(getContext(), "运单号不能为空！请扫描运单号！");
                } else {
                    // todo:调用揽件接口，使用运单号开门
                   ExpressSendOrderPickUpDto expressSendOrderPickUpDto = new ExpressSendOrderPickUpDto();
                   expressSendOrderPickUpDto.setExprSn(mailNo);
                   expressSendOrderPickUpDto.setCabSn(BaseUtils.getClientId());

                   ICSPClient.takeParcelByExpressNo(LocalStorage.getInstance().getSession().getToken(), expressSendOrderPickUpDto, new Callback() {
                       @Override
                       public void onFailure(@NotNull Call call, @NotNull IOException e) {
                           // TODO 取件接口请求失败逻辑处理
                           ToastUtils.make(getContext(), "取件失败！网络异常！");
                           Log.e(TAG, "onFailure: 取件请求失败!", e);
                           LoggingUtils.sendErrorLog("业务异常: 取件请求失败! 网络异常! ", e);
                       }

                       @Override
                       public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                           // TODO 返回锁板和锁id, 开柜门
                           getActivity().runOnUiThread(() -> {
                               String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
                               if (!StringUtils.isNullOrEmpty(parseResponse)) {
                                   try {
                                       ObjectMapper objectMapper = JacksonUtils.objectmapper;
                                       ResponseData<PreparedOrderResponseVo> responseData = null;
                                       try {
                                           responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
                                           });
                                           ToastUtils.make(getContext(), responseData.getMsg());
                                       } catch (JsonProcessingException e) {
                                           Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
                                           LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! ", e);
                                       } catch (RuntimeException e) {
                                           Log.e(TAG, "onResponse: 未知错误! ", e);
                                           ToastUtils.make(getContext(), "未知错误！");
                                           LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! 未知错误!  ", e);
                                       }
                                       if (null != responseData && responseData.getCode().equals(ICSPConstant.OK)) {
                                           // 开门
                                           Log.i(TAG, "onResponse: 取件成功! ");
                                           ToastUtils.make(getContext(), "取件成功！");
                                           Bundle bundle = new Bundle();
                                           bundle.putString("mailNo", mailNo);
                                           bundle.putString("pickUp", responseData.getData().getPickUp());
                                           bundle.putString("takeCabinetNo", responseData.getData().getLockerName());
                                           ((MainActivity) getActivity()).getBundleMap().put(ExpressSendOrderPickUpDto.class.getName(), bundle);
                                           if (null != timer) {
                                               timer.cancel();
                                           }
                                           NavUtils.navigate(CollectMaterialsFragment.this, R.id.action_collectMaterialsFragment_to_takeSuccessFragment);
                                       }
                                   } catch (RuntimeException e) {
                                       Log.e(TAG, "onResponse: 取件失败! ", e);
                                       ToastUtils.make(getContext(), "取件失败！");
                                       LoggingUtils.sendErrorLog("业务异常: 取件失败! ", e);
                                   }
                               } else {
                                   ToastUtils.make(getContext(), "服务器异常！ 请稍后重试！");
                               }
                           });
                       }
                   });
                }
            });
        }

        // 数字输入 5 手机号   4 面单号
        binding.mailNo.setOnFocusChangeListener((view1, hasFocus) -> {
            AtomicBoolean caseSmallFlag = new AtomicBoolean(true);
            
            binding.keyBoard.inputButton0.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.mailNo.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "面单号最多不能超过16位! ");
                    return;
                }
                int start = binding.mailNo.getSelectionStart();
                binding.mailNo.getText().insert(start, "0");
            });
            binding.keyBoard.inputButton1.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.mailNo.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "面单号最多不能超过16位! ");
                    return;
                }
                int start = binding.mailNo.getSelectionStart();
                binding.mailNo.getText().insert(start, "1");
            });
            binding.keyBoard.inputButton2.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.mailNo.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "面单号最多不能超过16位! ");
                    return;
                }
                int start = binding.mailNo.getSelectionStart();
                binding.mailNo.getText().insert(start, "2");
            });
            binding.keyBoard.inputButton3.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.mailNo.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "面单号最多不能超过16位! ");
                    return;
                }
                int start = binding.mailNo.getSelectionStart();
                binding.mailNo.getText().insert(start, "3");
            });
            binding.keyBoard.inputButton4.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.mailNo.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "面单号最多不能超过16位! ");
                    return;
                }
                int start = binding.mailNo.getSelectionStart();
                binding.mailNo.getText().insert(start, "4");
            });
            binding.keyBoard.inputButton5.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.mailNo.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "面单号最多不能超过16位! ");
                    return;
                }
                int start = binding.mailNo.getSelectionStart();
                binding.mailNo.getText().insert(start, "5");
            });
            binding.keyBoard.inputButton6.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.mailNo.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "面单号最多不能超过16位! ");
                    return;
                }
                int start = binding.mailNo.getSelectionStart();
                binding.mailNo.getText().insert(start, "6");
            });
            binding.keyBoard.inputButton7.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.mailNo.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "面单号最多不能超过16位! ");
                    return;
                }
                int start = binding.mailNo.getSelectionStart();
                binding.mailNo.getText().insert(start, "7");
            });
            binding.keyBoard.inputButton8.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.mailNo.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "面单号最多不能超过16位! ");
                    return;
                }
                int start = binding.mailNo.getSelectionStart();
                binding.mailNo.getText().insert(start, "8");
            });
            binding.keyBoard.inputButton9.setOnClickListener(v -> {
                if(!hasFocus){
                    return;
                }
                int length = binding.mailNo.getText().length();
                if (length >= 16) {
                    ToastUtils.make(getContext(), "面单号最多不能超过16位! ");
                    return;
                }
                int start = binding.mailNo.getSelectionStart();
                binding.mailNo.getText().insert(start, "9");
            });

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

        });

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

        // 默认选中运单号输入框
        binding.mailNo.requestFocus();
    }

    private void extracted(AtomicBoolean caseSmallFlag) {

    }


    public PreparedOrderResponseVo getData() {
        return data;
    }

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
        binding = null;
        Log.i(TAG, TAG + "被销毁。。。");
    }
}
