package com.example.applock.roombase;

import android.os.Handler;
import android.os.Looper;

import com.example.applock.listener.QuerySoftListener;

import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class SoftWareLockRoom {
    private static ExecutorService myUIServicePool;
    private static Handler myHandler;
    private static ExecutorService getMyUIServicePool(){
        if (myUIServicePool == null){
                myUIServicePool = Executors.newSingleThreadExecutor();
        }
        return myUIServicePool;
    }
    private static Handler getMyHandler() {
        if (myHandler == null) {
            myHandler = new Handler(Looper.getMainLooper());
        }
        return myHandler;
    }
    public static void QuerySoftRoom(boolean islock, QuerySoftListener listener){
        getMyUIServicePool().execute(new Runnable() {
            @Override
            public void run() {
                List<AppEntity> applist = AppDataBase.getDatabaseInstance().getAppEntityDao().queryLockApp(islock);
                getMyHandler().post(()->{
                    listener.QueryResult(applist);
                });
            }
        });
    }
    public static void RUNIO(Runnable runnable){
        getMyUIServicePool().execute(runnable);
    }
    public static void insertRoom(Set<AppEntity> list) {
        getMyUIServicePool().execute(new Runnable() {
            @Override
            public void run() {
                for (AppEntity entityApp : list) {
                    AppDataBase.getDatabaseInstance().getAppEntityDao().insertData(entityApp);
                }

            }
        });
    }

    public static void onRunMain(Runnable runnable){
        getMyHandler().post(runnable);

    }

    public static void updateRoom(AppEntity entityApp) {
        getMyUIServicePool().execute(new Runnable() {
            @Override
            public void run() {
                AppDataBase.getDatabaseInstance().getAppEntityDao().updateData(entityApp);
            }
        });
    }

}
