package com.rehome.meetingbook.ui.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import com.rehome.meetingbook.MainActivity;
import com.rehome.meetingbook.R;
import com.rehome.meetingbook.ZKUSBManager.ZKUSBManager;
import com.rehome.meetingbook.ZKUSBManager.ZKUSBManagerListener;
import com.rehome.meetingbook.base.BaseActivity2;
import com.rehome.meetingbook.databinding.ActivityLoginBinding;
import com.rehome.meetingbook.utils.ControllerActivity;
import com.rehome.meetingbook.weiget.CustomLoadingDialog;
import com.zkteco.android.biometric.core.device.ParameterHelper;
import com.zkteco.android.biometric.core.device.TransportType;
import com.zkteco.android.biometric.core.utils.LogHelper;
import com.zkteco.android.biometric.module.idcard.IDCardReader;
import com.zkteco.android.biometric.module.idcard.IDCardReaderFactory;
import com.zkteco.android.biometric.module.idcard.IDCardType;
import com.zkteco.android.biometric.module.idcard.exception.IDCardReaderException;
import com.zkteco.android.biometric.module.idcard.meta.IDCardInfo;
import com.zkteco.android.biometric.module.idcard.meta.IDPRPCardInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.widget.Toolbar;

/**
 * Create By HuangWenFei
 * 创建日期：2022-11-29 10:44
 * 描述：登录界面
 */
public class LoginActivity extends BaseActivity2<ActivityLoginBinding> {

    private long exitTime = 0;

    private String lastBtnCkick = "appointment"; //  appointment      search




    private CustomLoadingDialog customDialog;
    private String xm;


    private final ActivityResultLauncher<Intent> mAppointmentSearchStartActivityForResult = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    // 处理 result
//                    Intent data = result.getData();
//                    if (data != null) {
//                        String idCardNo = data.getStringExtra("sfzh");
//                        if (idCardNo != null) {
//                            Intent intent = new Intent();
//                            intent.setClass(LoginActivity.this, AppointmentListActivity.class);
//                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//                            intent.putExtra("idCardNo", idCardNo);
//                            startActivity(intent);
//                        }
//                    }
                }
            }
    );


    //隐藏
    @Override
    protected void initView() {

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        binding.tvVersion.setText("版本号:" + getVersionName());


        binding.llCloseKey.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideKeyBoard(LoginActivity.this);
            }
        });



    }

    @Override
    protected void initData() {

    }

    @Override
    protected ActivityLoginBinding getBinding() {
        return ActivityLoginBinding.inflate(LayoutInflater.from(this));
    }

    @Override
    protected Toolbar getToolbar() {
        return binding.toolbarView.toolbar;
    }


    public void setBack() {
        mToolbar.setNavigationIcon(R.mipmap.back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public static void hideKeyBoard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window token from it.
        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


    //身份证号码格式校验
    public boolean checkIdCardNoFormat(String idCardNo) {
        String reg = "^\\d{15}$|^\\d{17}[0-9Xx]$";
        return idCardNo.matches(reg);
    }


    // 按两次返回键退出程序
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                showToast("再按一次退出程序");
                exitTime = System.currentTimeMillis();
            } else {
                ControllerActivity.getAppManager().finishAllActivity();
                finish();
                System.exit(0);
                //NohttpUtils.getInstance().cancelAll();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 获取版本名称:清单文件中
     *
     * @return 应用版本名称    返回null代表异常
     */
    private String getVersionName() {
        //1,包管理者对象packageManager
        PackageManager pm = getPackageManager();
        //2,从包的管理者对象中,获取指定包名的基本信息(版本名称,版本号),传0代表获取基本信息
        try {
            PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
            //3,获取版本名称
            return packageInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}