package com.android.launcher3.auto_get_permission.nokia;

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 com.android.launcher3.auto_get_permission.AccessibilityNodeOperateUtil.*;


public class NokiaAutoPermissionGetter extends BaseAutoGetPermission {
    private static final String TAG="NokiaAutoPermissionGetter";
    public NokiaAutoPermissionGetter(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())) {

            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) {

        AccessibilityNodeInfo sourceNode=event.getSource();

        if ("com.evenwell.powersaving.g3.exception.PowerSaverExceptionActivity".equals(event.getClassName().toString())) {
            Logger.e(TAG, "enter PowerSaverExceptionActivity");
//                sleep();
            sleepLong();
            getChildNodeEqual(sourceNode, appName);
            List<AccessibilityNodeInfo> appListNodeList = sourceNode.findAccessibilityNodeInfosByViewId("com.evenwell.powersaving.g3:id/applist");

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

                AccessibilityNodeInfo targetAppNode = getChildNodeByIdEqualText(appListNode, "com.evenwell.powersaving.g3:id/appName", appName);
                long time = System.currentTimeMillis();
                while (targetAppNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                    if (appListNode.isScrollable()) {
                        appListNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        appListNode.refresh();
                    } else {
                        break;
                    }

                    targetAppNode = getChildNodeByIdEqualText(appListNode, "com.evenwell.powersaving.g3:id/appName", appName);
                }


                if (targetAppNode.getParent() != null) {
                    AccessibilityNodeInfo linearLayoutNode = targetAppNode.getParent();

                    List<AccessibilityNodeInfo> checkBoxNodeInfoList = linearLayoutNode.findAccessibilityNodeInfosByViewId("com.evenwell.powersaving.g3:id/appCheckBoxhWidgetDisAuto");
                    if (checkBoxNodeInfoList != null && checkBoxNodeInfoList.size() > 0) {
                        checkBoxNodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleepShort();
                    }

                    List<AccessibilityNodeInfo> appCheckBoxhWidgetBAMNodeInfoList = linearLayoutNode.findAccessibilityNodeInfosByViewId("com.evenwell.powersaving.g3:id/appCheckBoxhWidgetBAM");
                    if (appCheckBoxhWidgetBAMNodeInfoList != null && appCheckBoxhWidgetBAMNodeInfoList.size() > 0) {
                        appCheckBoxhWidgetBAMNodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleepShort();
                    }
                }
            }
            return;
        }

        if ("android.app.AlertDialog".equals(event.getClassName())) {

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

            if (deterBtnNodeList != null && deterBtnNodeList.size() > 0) {
                deterBtnNodeList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                resetStep();

            }
            return;
        }
    }

    @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".equals(event.getClassName())) {
            List<AccessibilityNodeInfo> recyclerViewListNode = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/dashboard_container");
            if (recyclerViewListNode != null && recyclerViewListNode.size() > 0) {

                AccessibilityNodeInfo recyclerViewNode = recyclerViewListNode.get(0);

                AccessibilityNodeInfo developerChoiceNode = getChildNodeByIdEqualText(recyclerViewNode, "android:id/title", "开发者选项");
                long time = System.currentTimeMillis();
                while (developerChoiceNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                    if (recyclerViewNode.isScrollable()) {
                        recyclerViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        recyclerViewNode.refresh();
                    } else {
                        break;
                    }

                    developerChoiceNode = getChildNodeByIdEqualText(recyclerViewNode, "android:id/title", "开发者选项");
                }
                if (performAction(developerChoiceNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                    sleep();
                } else {
                    showToastLong("请点击【开发者选项】");
                    return;
                }

            }

        }

//                        三星不同版本之前的开发者选项的界面不一样 所以需要两种判断

        if ("com.android.settings.Settings$DevelopmentSettingsDashboardActivity".equals(event.getClassName())) {
            List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/list");
            if (listViewNodes != null && listViewNodes.size() > 0) {
                AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                AccessibilityNodeInfo 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 (performAction(usbNode, AccessibilityNodeInfo.ACTION_CLICK)) {

                    resetStep();
                } else {
                    showToastLong("请点击【USB 调试】");
                    return;
                }

            }
        }

        if ("com.android.settings.Settings$DevelopmentSettingsActivity".equals(event.getClassName())) {
            AccessibilityNodeInfo usbNode = getChildNodeEqual(sourceNode, "USB调试");
            if (!performAction(usbNode, AccessibilityNodeInfo.ACTION_CLICK)) {

                List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/list");
                if (listViewNodes != null && listViewNodes.size() > 0) {
                    AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                    usbNode = getChildNodeEqual(listViewNode, "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 = getChildNodeEqual(listViewNode, "USB调试");
                    }
                    if (performAction(usbNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        resetStep();
                    } else {
                        showToastLong("请点击【USB 调试】");
                        return;
                    }
                }
            }
        }
    }

    @Override
    public void requestNotificationPermission(AccessibilityEvent event) {

        AccessibilityNodeInfo sourceNode=event.getSource();
        if ("com.android.settings.Settings$NotificationAccessSettingsActivity".equals(event.getClassName())) {

            AccessibilityNodeInfo targetApp = getChildNodeEqual(sourceNode, appName);
            if (!performAction(targetApp, AccessibilityNodeInfo.ACTION_CLICK)) {
                List<AccessibilityNodeInfo> nodeInfoList = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/list");
                if (nodeInfoList != null && nodeInfoList.size() > 0) {
                    AccessibilityNodeInfo listViewNode = nodeInfoList.get(0);

                    AccessibilityNodeInfo targetAppItem = getChildNodeByIdEqualText(listViewNode, "android:id/title", 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, "android:id/title", appName);


                    }

                    if (!performAction(targetAppItem, AccessibilityNodeInfo.ACTION_CLICK)) {
                        showToastLong("请点击【" + appName + "】");
                        return;
                    }
                } else {
                    showToastLong("请点击【" + appName + "】");
                }
            }


        }

        if ("android.app.AlertDialog".equals(event.getClassName())) {
//                            允许
            List<AccessibilityNodeInfo> nodeInfoList = sourceNode.findAccessibilityNodeInfosByViewId("android:id/button1");
            if (nodeInfoList != null && nodeInfoList.size() > 0 && performAction(nodeInfoList.get(0), AccessibilityNodeInfo.ACTION_CLICK)) {

                resetStep();
                return;

            } else {
                showToastLong("请点击【允许】");
                return;
            }
        }
    }

    @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");
        }
    }
}
