package com.dlc.exam.ui;

import android.Manifest;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.arcsoft.face.ActiveFileInfo;
import com.arcsoft.face.ErrorInfo;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.enums.RuntimeABI;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.dlc.exam.common.Constants;
import com.dlc.exam.databinding.ActivityStartBinding;
import com.dlc.exam.ui.settings.PasswordDialog;
import com.dlc.exam.ui.settings.SettingsActivity;
import com.rc.core.ui.activity.RcBaseActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.disposables.Disposable;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class StartActivity extends RcBaseActivity<ActivityStartBinding> {

    private ActivityStartBinding binding;

    @NonNull
    @Override
    protected ActivityStartBinding createViewBinding() {
        return binding = ActivityStartBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initViews(@Nullable Bundle savedInstanceState) {
        super.initViews(savedInstanceState);
        binding.versionName.setText(AppUtils.getAppVersionName());

        binding.firm.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new PasswordDialog(StartActivity.this, new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        ActivityUtils.startActivity(SettingsActivity.class);
                        finish();
                        return null;
                    }
                }).show();
                return true;
            }
        });

        requestPermission();
    }

    private CountDownTimer activeTimer;

    private void requestPermission() {
        Disposable disposable = new RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.RECORD_AUDIO).subscribe(aBoolean -> {
            if (aBoolean) {
                activeTimer = new CountDownTimer(1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {

                    }

                    @Override
                    public void onFinish() {
                        if (activeOnlineFaceEngine()) {
                            activeTimer.cancel();
                            activeTimer = null;
                            binding.main.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    finish();
                                    ActivityUtils.startActivity(MainActivity.class);
                                }
                            });
                        } else {
                            activeTimer.start();
                        }
                    }
                };
                activeTimer.start();
            } else {
                new AlertDialog.Builder(StartActivity.this)
                        .setTitle("提示")
                        .setMessage("您必须同意所有权限才可以继续使用")
                        .setNegativeButton("确定", (dialog, which)
                                -> requestPermission())
                        .show();
            }
        });
        addDisposable(disposable);
    }

    private boolean activeOnlineFaceEngine() {
        RuntimeABI runtimeABI = FaceEngine.getRuntimeABI();
        LogUtils.d("虹软", runtimeABI);
        ActiveFileInfo activeFileInfo = new ActiveFileInfo();
        int res = FaceEngine.getActiveFileInfo(this, activeFileInfo);
        if (res == ErrorInfo.MOK) {
            LogUtils.json(activeFileInfo);
            return true;
        } else {
            int code = FaceEngine.activeOnline(this, Constants.ArcFace.APP_ID, Constants.ArcFace.SDK_KEY);
            LogUtils.d("虹软激活", code);
            if (ErrorInfo.MOK == code || code == ErrorInfo.MERR_ASF_ALREADY_ACTIVATED) {
                showToast("人脸引擎已激活");
                return true;
            } else {
                showToast("人脸引擎激活失败!" + code);
            }
        }
        return false;
    }
}