package com.hh.arome.common.scan.idcard.lifecycle;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import com.bumptech.glide.util.Util;
import com.hh.arome.common.scan.idcard.base.IDCardScanEngine;
import com.hh.arome.common.scan.idcard.callback.IDCardInitResultCallback;
import com.hh.arome.common.scan.idcard.callback.IDCardScanResultCallback;
import com.hh.arome.utlis.log.AppLogUtils;

/**
 * Created by L470 on 2020/7/20.
 */
public class IDCardScanManager implements LifeCycleListener {


    Context mContext;

    IDCardScanEngine mIDCardScanEngine;

    private final String FRAGMENT_ACTIVITY_NAME = "FRAGMENT_ACTIVITY_NAME";

    public void with(Context context,int idCardReaderBrand) {
        with(context,idCardReaderBrand,false);

    }


    public void with(Context context,int idCardReaderBrand,boolean isOverTime) {
        if (context == null) {
            throw new IllegalArgumentException("You cannot start a load on a null Context");
        }
        this.mContext = context;

        createIDCardScanEngine(context,idCardReaderBrand,isOverTime);
        bind(context);

    }

    private void createIDCardScanEngine(Context context,int idCardReaderBrand,boolean isOverTime) {
        mIDCardScanEngine = IDCardScanEngine.getInstance(context,idCardReaderBrand,isOverTime);
    }

    /**
     * @param context 传入的上下文
     */
    public void bind(Context context) {
        if (Util.isOnMainThread() && !(context instanceof Application)) {
            if (context instanceof FragmentActivity) {
                bindFragmentActivity((FragmentActivity) context);
            }
        }
    }

    private void bindFragmentActivity(FragmentActivity activity) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        supportFragmentGet(fragmentManager, isActivityVisible(activity));
    }

    /**
     * 创建Fragment 并依托于当前Activity(当然Glide做了缓存去重操作)
     *
     * @param fragmentManager
     * @param activityVisible
     */
    private void supportFragmentGet(FragmentManager fragmentManager, boolean activityVisible) {

        Fragment fragment = fragmentManager.findFragmentByTag(FRAGMENT_ACTIVITY_NAME);
        if (fragment == null) {
            SupportManagerFragment current = new SupportManagerFragment(this);

            fragmentManager.beginTransaction()
                    .add(current, FRAGMENT_ACTIVITY_NAME)
                    .commitAllowingStateLoss();
//        current.getLifeCycle().addListener(this);
            /*if (activityVisible) {
                current.getLifeCycle().onStart();
            }*/
        }

    }

    private boolean isActivityVisible(Activity activity) {
        return !activity.isFinishing();
    }


    public void initIDCard(IDCardInitResultCallback idCardInitResultCallback) {
        mIDCardScanEngine.initIDCard(idCardInitResultCallback);
    }


    public void startScanIDCard(IDCardScanResultCallback idCardScanResultCallback) {
        mIDCardScanEngine.startScanIDCard(idCardScanResultCallback);
    }

    public void setOverTime(boolean isOverTime) {
        mIDCardScanEngine.setOverTime(isOverTime);
    }

    @Override
    public void onResume() {
        AppLogUtils.i("IDCardScanManager--onResume");
        mIDCardScanEngine.resumeScanIDCard();
    }

    @Override
    public void onPause() {
        AppLogUtils.i("IDCardScanManager--onPause");
        mIDCardScanEngine.pauseScanIDCard();
    }

    @Override
    public void onStop() {
        AppLogUtils.i("IDCardScanManager--onStop");
        mIDCardScanEngine.stopScanIDCard();
    }

    @Override
    public void onDestroy() {
        AppLogUtils.i("IDCardScanManager--onDestroy");
        mIDCardScanEngine.destoryIDCardScan();
    }


}
