package xn.hxp.ui.login;

import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.LogUtils;

import java.util.concurrent.atomic.AtomicBoolean;

import xn.hxp.R;
import xn.hxp.base.BaseActivity;
import xn.hxp.databinding.ActivityLoginBinding;
import xn.hxp.ui.login.callback.OnFaceScanResultCallback;
import xn.hxp.ui.login.fragment.LoginCardFragment;
import xn.hxp.ui.login.fragment.LoginFaceFragment;
import xn.hxp.ui.login.fragment.LoginQrFragment;
import xn.hxp.util.AudioPlayerTool;
import xn.hxp.util.CardReaderHelper;

public class LoginActivity extends BaseActivity {
    private ActivityLoginBinding binding;
    private FragmentManager fragmentManager;
    protected LoginFaceFragment loginFaceFragment;
    protected OnFaceScanResultCallback onFaceScanResultCallback;
    private LoginActivityHelper loginActivityHelper;
    private LoginCardFragment loginCardFragment;
    private LoginQrFragment loginQrFragment;
    protected CardReaderHelper cardReaderHelper;
    protected CountDownTimer scanCodeCDT;

    // 是否已经登录成功
    protected final AtomicBoolean isLogged = new AtomicBoolean(false);
    // 是否正在刷卡
    protected final AtomicBoolean isCarding = new AtomicBoolean(false);
    // 是否正在扫码
    protected final AtomicBoolean isScanning = new AtomicBoolean(false);

    @Override
    protected ViewBinding setViewBinding() {
        return binding = ActivityLoginBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onInit() {
        binding.includeTitle.loginLL.setVisibility(View.GONE);
        loginActivityHelper = new LoginActivityHelper(this, binding, cardReaderHelper);

        fragmentManager = getSupportFragmentManager();
        loginFaceFragment = new LoginFaceFragment(onFaceScanResultCallback);
        loginCardFragment = new LoginCardFragment();
        loginQrFragment = new LoginQrFragment();
        FragmentUtils.add(fragmentManager, loginFaceFragment, binding.coordinatorFL.getId());

        binding.card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isScanning.set(false);
                binding.face.setVisibility(View.VISIBLE);
                binding.card.setVisibility(View.GONE);
                binding.qr.setVisibility(View.VISIBLE);
                FragmentUtils.replace(fragmentManager, loginCardFragment, binding.coordinatorFL.getId());
                binding.hint.setText("请在刷卡区域进行刷卡验证");
            }
        });

        binding.face.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isScanning.set(false);
                binding.face.setVisibility(View.GONE);
                binding.card.setVisibility(View.VISIBLE);
                binding.qr.setVisibility(View.VISIBLE);
                binding.hint.setText("请正对屏幕并使脸位于取景框内\n请保持光线充足，避免光照过强或过弱");
                FragmentUtils.replace(fragmentManager, loginFaceFragment, binding.coordinatorFL.getId());
                AudioPlayerTool.getInstance().play(R.raw.login_face_verify_hint);
            }
        });

        binding.qr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isScanning.set(true);
                binding.face.setVisibility(View.VISIBLE);
                binding.card.setVisibility(View.VISIBLE);
                binding.qr.setVisibility(View.GONE);
                binding.hint.setText("请打开微信扫描屏幕二维码进行扫码验证");
                FragmentUtils.replace(fragmentManager, loginQrFragment, binding.coordinatorFL.getId());
            }
        });

        // 人脸识别提醒
        AudioPlayerTool.getInstance().play(R.raw.login_face_verify_hint);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (!isCarding.get()) {
            cardReaderHelper.handleKeyEvent(event);
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cardReaderHelper.release();
        if (scanCodeCDT != null) {
            scanCodeCDT.cancel();
        }
    }

    @Override
    protected void cdFinish() {
        finish();
    }

    @Override
    protected void cdTime(int cd) {
        binding.back.setText("返回" + cd + "s");
    }
}