package com.app.simplitend.appblocking;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;

import com.app.simplitend.BuildConfig;

import java.util.ArrayList;
import java.util.List;

public class TopAppDetectionService extends AccessibilityService {
    public static final String BLOCKED_APP_NAME = "BLOCKED_APP_NAME";

    public static final String STOP_ACCESSIBILITY_SERVICE = "stop_accessibility_service";

    MySharedPref sharedPref;
    List<String> appsList = new ArrayList<>();

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        sharedPref = new MySharedPref(getApplication());
        appsList.addAll(sharedPref.getArrayList("APP_LIST"));

        Log.i("TAG--->", "onAccessibilityEvent: ==>" + sharedPref.getArrayList("APP_LIST"));

        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            // Get the package name of the currently opened app
            String packageName = event.getPackageName().toString();

            Log.i("TAG--->", "onAccessibilityEvent: 1==>" + packageName);

            if (sharedPref.checkKey("APP_LIST") &&
                    !packageName.equals(BuildConfig.APPLICATION_ID) &&
                    !packageName.equals("com.android.settings") &&
                    !packageName.equals("com.google.android.googlequicksearchbox")) {

                if (sharedPref.getArrayList("APP_LIST").contains(packageName)) {
                    Intent intent = new Intent(this, BlockApp.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    String appName;
                    try {
                        appName = getPackageManager().getApplicationLabel(getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA)).toString();
                    }catch (Exception e){
                        appName = "This app";
                    }

                    intent.putExtra(BLOCKED_APP_NAME, appName);
                    startActivity(intent);
                }
            }

//            // Check if the package name matches the blocked app
//            for (String packageNames : appsList) {
//                if (packageNames.equals(packageName)) {
//                    // Launch the parent app or show a blocking message
//                    Intent intent = new Intent(this, BlockApp.class);
//                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    startActivity(intent);
//                }
//            }
        }
    }

    @Override
    public void onInterrupt() {
        // This method is required but not used in this example
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null && STOP_ACCESSIBILITY_SERVICE.equals(intent.getAction())){
            // stop the service
            disableSelf();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();

        sharedPref = new MySharedPref(getApplication());
        // Set the event types and package names to monitor
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
//        info.packageNames = sharedPref.getArrayList("APP_LIST").toArray(new String[0]);
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;

        // Set the flags based on the Android version
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            info.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS |
                    AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS;
        }

        // Set the service info
        setServiceInfo(info);
    }

 /*   @Override
    public void onTaskRemoved(Intent rootIntent) {
        Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
        restartServiceIntent.setPackage(getPackageName());
        startService(restartServiceIntent);
        super.onTaskRemoved(rootIntent);
    }*/
}