package com.android.launcher3.auto_get_permission;

import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.android.launcher3.accessibility.PermissionOpenAccessiblityService;
import com.android.launcher3.util.SystemUtils;

import java.util.List;

import static com.android.launcher3.auto_get_permission.AccessibilityNodeOperateUtil.*;

/**
 * 通用的权限获取
 */
public class GeneralAutoGetPermission extends BaseAutoGetPermission {


    public GeneralAutoGetPermission(PermissionOpenAccessiblityService service) {
        super(service);
    }

    @Override
    public void requestkDeviceAdmin(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if ("com.android.settings.Settings$DeviceAdminSettingsActivity".equals(event.getClassName().toString())) {
//                        已跳转到设备管理器页面
            AccessibilityNodeInfo accessibilityNodeInfo1 = getChildNodeEqual(sourceNode, appName);

            if (!performAction(accessibilityNodeInfo1, AccessibilityNodeInfo.ACTION_CLICK)) {
                showToastLong("请找到【" + appName + "】应用并点击");
                return;
            } else {
                sleep();
            }


        } else if ("com.android.settings.DeviceAdminAdd".equals(event.getClassName().toString())) {

            List<AccessibilityNodeInfo> nodeInfoList = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/restricted_action");

            if (nodeInfoList != null && nodeInfoList.size() > 0) {
                nodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                sleep();
                resetStep();
            } else {

                AccessibilityNodeInfo accessibilityNodeInfo1 = getChildNodeEqual(sourceNode, "激活");

                if (accessibilityNodeInfo1 != null && performAction(accessibilityNodeInfo1, AccessibilityNodeInfo.ACTION_CLICK)) {

                    sleep();
                    resetStep();

                } else {
                    showToastLong("请点击【启用/激活此设备管理员】");
                    return;

                }
            }
        }


    }

    @Override
    public void reqeustAutoStartUp(AccessibilityEvent event) {

    }

    @Override
    public void requestBatteryOptimize(AccessibilityEvent event) {

    }

    @Override
    public void requestUsageAppPermission(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();

        if ("com.android.settings.Settings$UsageAccessSettingsActivity".equals(event.getClassName())) {
            getChildNodeEqual(sourceNode, appName);
            sleep();

            List<AccessibilityNodeInfo> appListNodeList = null;

//                getChildNodeEqual(sourceNode,appName);
            appListNodeList = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

            if (appListNodeList == null || appListNodeList.isEmpty()) {
//
                appListNodeList = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/apps_list");
            }


            if (appListNodeList != null && appListNodeList.size() > 0) {
                AccessibilityNodeInfo appListNode = appListNodeList.get(0);

                AccessibilityNodeInfo targetAppItem = null;

                targetAppItem = getChildNodeByIdEqualText(appListNode, "com.android.settings:id/app_name", appName);
                if (targetAppItem == null) {

                    targetAppItem = getChildNodeByIdEqualText(appListNode, "android:id/title", appName);
                }

                long time = System.currentTimeMillis();
                while (targetAppItem == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                    if (appListNode.isScrollable()) {
                        appListNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        appListNode.refresh();
                        targetAppItem = getChildNodeByIdEqualText(appListNode, "com.android.settings:id/app_name", appName);
                        if (targetAppItem == null) {

                            targetAppItem = getChildNodeByIdEqualText(appListNode, "android:id/title", appName);
                        }
                    } else {
                        break;
                    }
                }

                if (!performAction(targetAppItem, AccessibilityNodeInfo.ACTION_CLICK)) {
                    showToastLong("请找到【" + appName + "】并点击");
                    return;
                } else {
                    List<AccessibilityNodeInfo> switcherNodeList = targetAppItem.getParent().findAccessibilityNodeInfosByViewId("com.android.settings:id/app_switch");

                    if (switcherNodeList != null && !switcherNodeList.isEmpty() && performAction(switcherNodeList.get(0), AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
//                                resetStep();
                    }
                }

            }
        } else if ("android.app.AlertDialog".equals(event.getClassName())) {
            AccessibilityNodeInfo allowNode = getChildNodeEqual(sourceNode, "允许");
            if (allowNode == null) {
                allowNode = getChildNodeEqual(sourceNode, "确定");
            }

            if (performAction(allowNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleep();
                resetStep();
            } else {
                showToastLong("请点击【确定】");
                return;
            }
        }


        /**
         * 使用记录访问权限(华为设备不同设备标题不同)
         */
        if ("com.android.settings.SubSettings".equals(event.getClassName()) && event.getText() != null && (event.getText().toString().contains("使用情况访问权限") || event.getText().toString().contains("使用记录访问权限") || event.getText().toString().contains("使用量数据访问"))) {

            if (SystemUtils.isSAMSUNG()) {
                AccessibilityNodeInfo nodeInfo = getChildNodeEqual(sourceNode, "允许追踪使用情况");

                if (performAction(nodeInfo, AccessibilityNodeInfo.ACTION_CLICK)) {

                    sleepShort();
                    resetStep();
                } else {
                    showToastLong("请点击【允许追踪使用情况】并返回上一页");
                    return;
                }
            } else if (SystemUtils.isNOKIA()) {
                sleep();
                List<AccessibilityNodeInfo> switchWidgetNodeList = sourceNode.findAccessibilityNodeInfosByViewId("android:id/switch_widget");

                if (switchWidgetNodeList != null && switchWidgetNodeList.size() > 0) {

                    if (switchWidgetNodeList.get(0).isChecked()) {
                        resetStep();

                    }

                    switchWidgetNodeList.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    resetStep();

                }

            } else {
                AccessibilityNodeInfo textViewNode = getChildNodeByIdEqualText(sourceNode, "android:id/title", "允许访问使用记录");

                if (textViewNode != null && textViewNode.getParent() != null) {
                    if (textViewNode.getParent().isClickable()) {
                        textViewNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        resetStep();
                    }
                } else {
                    textViewNode = getChildNodeByIdEqualText(sourceNode, "android:id/title", "允许查看使用情况");
                    if (performAction(textViewNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        resetStep();
                    } else {
                        showToastLong("请点击【允许查看使用情况】并返回上一页");
                        return;
                    }
                }
            }
        }
    }

    @Override
    public void requestCloseUSB(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode=event.getSource();
        if ("com.android.settings.Settings$DevelopmentSettingsActivity".equals(event.getClassName())) {

//                oppo也是这个页面
            getChildNodeEqual(sourceNode, "USB 调试");
            List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/list");
            if (listViewNodes == null || listViewNodes.isEmpty()) {

                listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");
            }
            if (listViewNodes != null && listViewNodes.size() > 0) {
                AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                AccessibilityNodeInfo usbNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "USB调试");
                if (usbNode == null) {
                    usbNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "USB 调试");
                }
                long time = System.currentTimeMillis();
                while (usbNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                    if (listViewNode.isScrollable()) {
                        listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        listViewNode.refresh();
                    } else {
                        break;
                    }
                    usbNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "USB调试");
                    if (usbNode == null) {
                        usbNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "USB 调试");
                    }
                }
                if (usbNode != null && usbNode.getParent() != null) {
                    if (SystemUtils.isOPPO()) {
                        List<AccessibilityNodeInfo> switchWidgetList = usbNode.getParent().findAccessibilityNodeInfosByViewId("android:id/switch_widget");
                        if (switchWidgetList != null && !switchWidgetList.isEmpty()) {
                            switchWidgetList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            sleepShort();
                            resetStep();
                        }
                    } else {
                        usbNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleepShort();
//                            resetStep();
                    }

                }

            }
        }
    }

    @Override
    public void requestNotificationPermission(AccessibilityEvent event) {

    }

    @Override
    public void requestDefaultLauncher(AccessibilityEvent event) {
        AccessibilityNodeInfo nodeInfo=event.getSource();
        if ("com.android.internal.app.ResolverActivity".equals(event.getClassName())) {
            AccessibilityNodeInfo targetAppNode = getChildNodeEqual(nodeInfo, appName);

            if (targetAppNode != null && targetAppNode.getParent() != null) {
                targetAppNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                sleep();
            }

            List<AccessibilityNodeInfo> alwaysBtnList = nodeInfo.findAccessibilityNodeInfosByViewId("android:id/button_always");
            if (alwaysBtnList != null && alwaysBtnList.size() > 0) {
                alwaysBtnList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }

//                    AccessibilityNodeInfo alwaysBtn = getChildNodeByIdEqualText(nodeInfo, "android:id/button_always");
        }
    }
}
