package com.android.launcher3.accessibility;

import android.Manifest;
import android.accessibilityservice.AccessibilityService;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityEvent;

import com.android.launcher3.manager.ColumnDef;
import com.android.launcher3.manager.Constants;
import com.android.launcher3.manager.LauncherManager;
import com.android.launcher3.manager.ManagementInfo;
import com.android.launcher3.model.ConfigManager;
import com.android.launcher3.util.Logger;
import com.android.launcher3.util.PermissionUtil;

import java.util.Arrays;
import java.util.List;

/**
 * 拦截app的启动
 */
@Deprecated
public class AppManagerAccessiblityService extends AccessibilityService {

    private static final String TAG = "AppManagerAccessiblityService";

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Logger.e(TAG, "onServiceConnected\t");
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (PermissionUtil.checkPermissionOpen(this, Manifest.permission.PACKAGE_USAGE_STATS))
            return;
        int eventType = event.getEventType();
        if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            Logger.e(TAG, event.toString());
            if (event.getPackageName() != null)
                return;
            String packageName = event.getPackageName().toString();


            if (TextUtils.isEmpty(packageName)) {
                packageName = "";
            }
            if (LauncherManager.getInstance().isInterceptSystemSetting() && packageName.contains("com.android.settings")) {
                backHome(-1);
//                LauncherManager.getInstance().jumpSetting(this);
            }

            ManagementInfo appinfo = LauncherManager.getInstance().getManageAppInfo(packageName);
            if (appinfo != null) {
                if (appinfo.getLock() == ColumnDef.LockApp.LOCKED) {
                    backHome(Constants.DIALOG_TYPE_LOCKED);
                }
                if (appinfo.getLock() == ColumnDef.LockApp.DEADLINE) {
//                    if (!LauncherManager.getInstance().canUse(packageName)) {
//                        backHome(Constants.DIALOG_TYPE_APP_USE_DEADLINE);
//                    }

                }

                if (appinfo.getUseType() == ColumnDef.AppUseType.UNCLASS_TYPE) {
                    backHome(Constants.DIALOG_TYPE_APP_UNCLASS);
                }
            }else {
                backHome(Constants.DIALOG_TYPE_APP_UNCLASS);
            }
            if (LauncherManager.getInstance().getLockScreenMode() != ColumnDef.LockScreen.MODE_UNLOCK) {

                if (LauncherManager.getInstance().isForceLock() && !LauncherManager.getInstance().isSafePackageName(packageName)&&!LauncherManager.getInstance().isSystemPackageName(packageName)) {

                    if (!ConfigManager.getInstance().isLockScreen()) {
//                        LauncherManager.getInstance().relockScreen();
//                        LauncherManager.getInstance().lockScreen(getApplicationContext(), LauncherManager.getInstance().getModeDesc1(), LauncherManager.getInstance().getModeDesc2(), LauncherManager.getInstance().getLockScreenMode());
                    }


                }
            }


            Logger.e(TAG, event.toString());
        } else if (eventType == AccessibilityEvent.TYPE_WINDOWS_CHANGED) {
            Logger.e(TAG, event.toString());
        }

    }

    private void backHome(int type) {
        LauncherManager.getInstance().backLauncherHome(getApplicationContext(), type);
    }


    @Override
    public void onInterrupt() {
        Logger.e(TAG, "onInterrupt");
    }


}
