package com.example.applock.allmanager;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.applock.roombase.AppDataBase;
import com.example.applock.roombase.AppEntity;
import com.example.applock.roombase.SoftWareLockRoom;

import java.util.Objects;

public class MySoftWareLockService extends IntentService {
    private String lastLockPackName = "";
    private LockAction lockAction;
    private Boolean isLock = true;

    public MySoftWareLockService() {
        super("name");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        lockAction = LockAction.getActionInstance(this);
        while (isLock) {
            String packageName = AppManager.getUsage(this);
            if (Objects.equals(packageName, "")) {
                continue;
            }
            AppEntity entityApp = AppDataBase.getDatabaseInstance().getAppEntityDao().queryByPagName(packageName);
            if (entityApp == null) {
                lockAction.HideLockView();
                lastLockPackName = packageName;
                continue;
            }
            if (Objects.equals(packageName, lastLockPackName)) {
                continue;
            }
            if (entityApp.isApplocked()) {
                if (!Objects.equals(packageName, lastLockPackName)) {
                    SoftWareLockRoom.onRunMain(() -> {
                        lockAction.showLockView();
                    });
                } else {
                }
            } else {
                SoftWareLockRoom.onRunMain(() -> {
                    lockAction.HideLockView();
                });
            }
            lastLockPackName = packageName;
        }
    }
}
