package com.lc.myapplication.security.locks.tool;

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

import androidx.annotation.Nullable;


import com.lc.myapplication.security.locks.lockroom.SecurityLockDataBase;
import com.lc.myapplication.security.locks.lockroom.SecurityLockEntity;

import java.util.Objects;

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

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

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

    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        lockAction = LockAction.getActionInstance(this);
        while (isLock) {
            String packageName = AppsManager.appsusage(this);
            if (Objects.equals(packageName, "")) {
                continue;
            }
            SecurityLockEntity securityLockEntity = SecurityLockDataBase.getSecurityLockDataBase().getsecurityLockDAO().QueryPackname(packageName);
            if (securityLockEntity == null) {
                lockAction.HideLockView();
                lastLockPackName = packageName;
                //Log.e("--------","---securityLockEntity"+null);
                continue;
            }
            if (Objects.equals(packageName, lastLockPackName)) {
                //Log.e("--------","---22222222222");
                continue;
            }
            if (securityLockEntity.isLocksuccess()) {
               // Log.e("--------","---11111111111");
                if (!Objects.equals(packageName, lastLockPackName)) {
                    MyTools.onRunMain(() -> {
                        lockAction.showLockView();
                    });
                } else {
                }
            } else {
               /// Log.e("--------","---3333333333333");
                MyTools.onRunMain(() -> {
                    lockAction.HideLockView();
                });
            }
            lastLockPackName = packageName;
        }
    }
}
