package com.android.launcher3.accessibility;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import com.android.launcher3.BuildConfig;
import com.android.launcher3.Launcher;
import com.android.launcher3.auto_get_permission.BaseAutoGetPermission;
import com.android.launcher3.auto_get_permission.GeneralAutoGetPermission;
import com.android.launcher3.auto_get_permission.honor.HonorAutoPermissionGetter;
import com.android.launcher3.auto_get_permission.huawei.HuaweiAutoPermissionGetter;
import com.android.launcher3.auto_get_permission.meizu.MeizuAutoPermissionGetter;
import com.android.launcher3.auto_get_permission.nokia.NokiaAutoPermissionGetter;
import com.android.launcher3.auto_get_permission.oppo.OppoAutoPermissionGetter;
import com.android.launcher3.auto_get_permission.samsung.SamsungAutoPermissionGetter;
import com.android.launcher3.auto_get_permission.vivo.VivoAutoPermissionGetter;
import com.android.launcher3.auto_get_permission.xiaomi.XiaoMiAutoPermissionGetter;
import com.android.launcher3.manager.LauncherManager;
import com.android.launcher3.util.KToast;
import com.android.launcher3.util.Logger;
import com.android.launcher3.util.PermissionUtil;
import com.android.launcher3.util.SystemUtils;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**
 * 自动开启权限
 */
public class PermissionOpenAccessiblityService extends AccessibilityService {

    private static final String TAG = "PermissionOpenAccessiblityService";

    private BaseAutoGetPermission permissionGetter;

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

        if (SystemUtils.isHONOR()) {
            permissionGetter = new HonorAutoPermissionGetter(this);
        } else if (SystemUtils.isHW()) {
            permissionGetter = new HuaweiAutoPermissionGetter(this);
        } else if (SystemUtils.isMEIZU()) {
            permissionGetter = new MeizuAutoPermissionGetter(this);
        } else if (SystemUtils.isNOKIA()) {
            permissionGetter = new NokiaAutoPermissionGetter(this);
        } else if (SystemUtils.isOPPO()) {
            permissionGetter = new OppoAutoPermissionGetter(this);
        } else if (SystemUtils.isSAMSUNG()) {
            permissionGetter = new SamsungAutoPermissionGetter(this);
        } else if (SystemUtils.isVIVO()) {
            permissionGetter = new VivoAutoPermissionGetter(this);
        } else if (SystemUtils.isMIUI()) {
            permissionGetter = new XiaoMiAutoPermissionGetter(this);
        } else {
            permissionGetter = new GeneralAutoGetPermission(this);
        }


        LauncherManager.getInstance().changeExitState(true);
//        AccessibilityEvent accessibilityEvent = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
//        onAccessibilityEvent(accessibilityEvent);
        permissionGetter.start();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        permissionGetter.stop();
        return super.onUnbind(intent);
    }


    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Logger.e(TAG, "onAccessibilityEvent " + event.toString());

        if (LauncherManager.getInstance().isInterceptSystemSetting())
            return;

        if (event == null || event.getClassName() == null) {
            return;
        }

        if ((event.getPackageName().toString().equals(getApplicationContext().getPackageName()) && !TextUtils.isEmpty(event.getClassName()) && event.getClassName().toString().equals("android.widget.LinearLayout"))) {
            performGlobalAction(GLOBAL_ACTION_BACK);
            return;
        }


        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {

            permissionGetter.operateAccessibilityEvent(event);

        }


    }

    @Override
    public void onInterrupt() {

    }


    /**
     * 权限完成后的操作 此处无法使用当前的context去startActivity
     */
    public void finish() {

    }

    @Override
    public void onDestroy() {
        permissionGetter.stop();
        super.onDestroy();
    }

}
