package com.sxyz.rfid;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import com.orhanobut.hawk.Hawk;
import com.sxyz.rfid.greendao.GreenDaoManager;
import com.sxyz.rfid.utils.L;
import com.sxyz.rfid.widgets.MyActivityLifecycle;
import com.ubx.usdk.RFIDSDKManager;
import com.ubx.usdk.rfid.RfidManager;
import com.ubx.usdk.util.SoundTool;

import java.util.LinkedList;
import java.util.List;

public class App extends MultiDexApplication {
    public static App instance;
    public static Context mContext;
    private List<Activity> mActivityList;
    public RfidManager mRfidManager;
    public boolean RFID_INIT_STATUS = false;
    public int readerType = 0;

    public static App getInstance() {
        return instance;
    }

    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        mContext = getApplicationContext();
//        initRfid();
        initGreenDao();
        mActivityList = new LinkedList<>();
        Hawk.init(getApplicationContext()).build();
        registerActivityLifecycleCallbacks(new MyActivityLifecycle(getContext()));
    }

    public void initRfid() {
        SoundTool.getInstance(getApplicationContext());
        // 在异步回调中拿到RFID实例
        RFIDSDKManager.getInstance().power(true);
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                boolean connect = RFIDSDKManager.getInstance().connect();
                if (connect) {
                    L.i("initRfid()  success.");
                    RFID_INIT_STATUS = true;
                    readerType = RFIDSDKManager.getInstance().getRfidManager().getReaderType();//80为短距，其他为长距
                    mRfidManager = RFIDSDKManager.getInstance().getRfidManager();
                    String firmware = RFIDSDKManager.getInstance().getRfidManager().getFirmwareVersion();
                    L.i("initRfid: GetReaderType() = " + readerType);
                } else {
                    L.i("initRfid  fail.");
                }
            }
        }, 1500);
    }

    public void addActivity(Activity activity) {
        // 判断当前集合中不存在该Activity
        if (!mActivityList.contains(activity)) {
            mActivityList.add(activity);//把当前Activity添加到集合中
        }
    }

    /**
     * @description 销毁单个Activity
     * @author wuchunyuan
     * @time 2020/7/10 10:01
     */
    public void removeActivity(Activity activity) {
        if (mActivityList.contains(activity)) {//判断当前集合中存在该Activity
            mActivityList.remove(activity);//从集合中移除
            activity.finish();//销毁当前Activity
        }
    }

    public void exit() {
        try {
            for (Activity activity : mActivityList) {
                if (activity != null) activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    /**
     * @description 获取activity是否存在
     * @author wuchunyuan
     * @time 2020/7/10 10:01
     */
    public boolean currentActivity(Class<?> cls) {
        for (Activity activity : mActivityList) {
            if (activity.getClass().equals(cls)) {
                return true;
            }
        }
        return false;
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /**
     * 初始化GreenDao,直接在Application中进行初始化操作
     */
    private void initGreenDao() {
        GreenDaoManager.getInstance().init(this);
    }

}
