package com.android.launcher3.auto_get_permission.oppo;

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

import com.android.launcher3.accessibility.PermissionOpenAccessiblityService;
import com.android.launcher3.auto_get_permission.BaseAutoGetPermission;
import com.android.launcher3.util.Logger;
import com.android.launcher3.util.SystemUtils;

import java.util.List;

import static android.accessibilityservice.AccessibilityService.GLOBAL_ACTION_BACK;
import static com.android.launcher3.auto_get_permission.AccessibilityNodeOperateUtil.*;

public class OppoAutoPermissionGetter extends BaseAutoGetPermission {
    public static final String TAG = "OppoAutoPermissionGetter";

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

    @Override
    public void requestkDeviceAdmin(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        Logger.e(TAG, "isOPPO" + event.getClassName());
        if ("com.android.settings.SubSettings".equals(event.getClassName()) && event.getText() != null && event.getText().toString().equals("[设备管理器]")) {
            AccessibilityNodeInfo accessibilityNodeInfo1 = getChildNodeEqual(sourceNode, appName);

            if (accessibilityNodeInfo1 != null && accessibilityNodeInfo1.getParent() != null) {

                accessibilityNodeInfo1.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                sleep();
            }
        } else if ("com.android.settings.DeviceAdminAdd".equals(event.getClassName().toString()) || "com.coloros.settings.feature.security.ColorDeviceAdminAdd".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 if ("com.android.settings.Settings$DeviceAdminSettingsActivity".equals(event.getClassName().toString())) {
//                        已跳转到设备管理器页面


            AccessibilityNodeInfo accessibilityNodeInfo1 = getChildNodeEqual(sourceNode, appName);

            if (accessibilityNodeInfo1 != null && accessibilityNodeInfo1.getParent() != null) {
                if (SystemUtils.isSAMSUNG()) {


                    accessibilityNodeInfo1.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);

                    Logger.e(TAG, "performAction ACTION_CLICK");
                }

                if (SystemUtils.isHONOR()) {
                    accessibilityNodeInfo1.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);

                    Logger.e(TAG, "performAction ACTION_CLICK");
                } else {
                    accessibilityNodeInfo1.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }

                sleep();


            }


        }


        if (event.getPackageName() != null && event.getPackageName().equals("com.android.settings")) {

            getService().performGlobalAction(GLOBAL_ACTION_BACK);
            sleepShort();
//                            gotoDeviceAdminPermissionSetting();
            return;
        }

        if (event.getPackageName() == null) {
            gotoDeviceAdminPermissionSetting();
            sleep();

        }
//
    }

    @Override
    public void reqeustAutoStartUp(AccessibilityEvent event) {

    }

    @Override
    public void requestBatteryOptimize(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if ("com.android.settings.applications.InstalledAppDetailsTop".equals(event.getClassName())) {
            boolean isTargetApp = getChildNodeEqual(sourceNode, appName) != null;
            if (isTargetApp) {

                List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                if (listViewNodes != null && listViewNodes.size() > 0) {
                    AccessibilityNodeInfo listView = listViewNodes.get(0);
                    AccessibilityNodeInfo allowAutoStart = getChildNodeEqual(listView, "允许自动启动");
                    if (allowAutoStart != null && allowAutoStart.getParent() != null) {
                        List<AccessibilityNodeInfo> listNode = allowAutoStart.getParent().findAccessibilityNodeInfosByViewId("android:id/switch_widget");
                        if (listNode != null && listNode.size() > 0) {
                            listNode.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            sleepShort();
                        }
                    }

                    AccessibilityNodeInfo allowOtherAppAutoStart = getChildNodeEqual(listView, "允许其他应用自动启动");
                    if (allowOtherAppAutoStart != null && allowOtherAppAutoStart.getParent() != null) {
                        List<AccessibilityNodeInfo> listNode = allowOtherAppAutoStart.getParent().findAccessibilityNodeInfosByViewId("android:id/switch_widget");
                        if (listNode != null && listNode.size() > 0) {
                            listNode.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            sleepShort();
                        }
                    }
                }

//                    getChildNodeEqual(sourceNode, "允许其他应用自动启动");

                AccessibilityNodeInfo batterySaveNode = getChildNodeEqual(sourceNode, "耗电保护");
                if (batterySaveNode != null && batterySaveNode.getParent() != null) {
                    batterySaveNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    sleep();
                }
            }
        } else if ("com.coloros.powermanager.fuelgaue.PowerAppsBgSetting".equals(event.getClassName())) {
            boolean isTargetApp = getChildNodeEqual(sourceNode, appName) != null;
            if (isTargetApp) {
                AccessibilityNodeInfo node = getChildNodeEqual(sourceNode, "允许后台运行");
                if (node != null && node.getParent() != null) {
                    node.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    sleepShort();
                    resetStep();
                }
            }
        }

    }

    @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) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if ("com.coloros.securitypermission.permission.PermissionNotificationActivity".equals(event.getClassName())) {

            List<AccessibilityNodeInfo> nodeInfoList = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");
            if (nodeInfoList == null || nodeInfoList.isEmpty()) {

                nodeInfoList = sourceNode.findAccessibilityNodeInfosByViewId("com.coloros.securitypermission:id/list_view");
            }
            if (nodeInfoList != null && nodeInfoList.size() > 0) {
                AccessibilityNodeInfo listViewNode = nodeInfoList.get(0);

                getChildNodeEqual(listViewNode, appName);
                AccessibilityNodeInfo targetAppItem = getChildNodeByIdEqualText(listViewNode, "com.coloros.securitypermission:id/name", appName);
                long time = System.currentTimeMillis();
                while (targetAppItem == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                    if (listViewNode != null) {

                        if (listViewNode.isScrollable()) {
                            listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            sleepShort();
                            listViewNode.refresh();
                        }
                    }

                    targetAppItem = getChildNodeByIdEqualText(listViewNode, "com.coloros.securitypermission:id/name", appName);
                }

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

                    sleepShort();
                } else {
                    showToastLong("请点击【" + appName + "】");
                    return;
                }
            }


        } else if ("com.coloros.securitypermission.common.l.a".equals(event.getClassName())) {
            AccessibilityNodeInfo allowNode = getChildNodeEqual(sourceNode, "允许");

            if (performAction(allowNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleepShort();
                resetStep();
            } else {
                showToastLong("请点击【允许】");
                return;
            }
        }
    }

    @Override
    public void requestDefaultLauncher(AccessibilityEvent event) {
        AccessibilityNodeInfo nodeInfo = event.getSource();

        if ("android.app.Dialog".equals(event.getClassName()) || "com.android.internal.app.ResolverActivity".equals(event.getClassName())) {


            AccessibilityNodeInfo neverRemindNode = getChildNodeEqual(nodeInfo, "不再提醒");
            if (neverRemindNode != null && neverRemindNode.performAction(AccessibilityNodeInfo.ACTION_CLICK)) {
                sleep();
            } else {

                if (neverRemindNode.getParent() != null) {
                    if (neverRemindNode.getParent().isClickable()) {
                        neverRemindNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleep();
                    }
                }
            }

            List<AccessibilityNodeInfo> pagerNodeList = nodeInfo.findAccessibilityNodeInfosByViewId("oppo:id/oppo_pager");

            if (pagerNodeList == null || pagerNodeList.isEmpty()) {

                pagerNodeList = nodeInfo.findAccessibilityNodeInfosByViewId("oppo:id/resolver_scroll_panel");
            }
            if (pagerNodeList != null && !pagerNodeList.isEmpty()) {
                AccessibilityNodeInfo pageNode = pagerNodeList.get(0);

                AccessibilityNodeInfo targetAppNode = getChildNodeEqualByContentDesc(pageNode, appName);

                if (targetAppNode == null) {
                    targetAppNode = getChildNodeEqual(pageNode, appName);
                }
                if (targetAppNode != null && targetAppNode.isClickable()) {

                    targetAppNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    sleepLong();
                    resetStep();
                } else {
                    if (targetAppNode.getParent() != null) {
                        targetAppNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleepLong();
                        resetStep();
                    }
                }

            }


        }
    }
}
