package com.android.launcher3.auto_get_permission.vivo;

import android.accessibilityservice.AccessibilityService;
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.PermissionUtil;
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.getChildNodeByIdEqualText;
import static com.android.launcher3.auto_get_permission.AccessibilityNodeOperateUtil.getChildNodeEqual;

public class VivoAutoPermissionGetter extends BaseAutoGetPermission {

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

    @Override
    public void requestkDeviceAdmin(AccessibilityEvent event) {

    }

    @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())) {
            if (SystemUtils.isVIVO() && event.getText() != null && event.getText().toString().equals("[开发]")) {
                getService().performGlobalAction(GLOBAL_ACTION_BACK);
                sleepShort();
                return;
            }
        }

        if ("com.vivo.settings.VivoSubSettings".equals(event.getClassName()) && event.getText() != null && event.getText().toString().equals("[更多设置]")) {

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

            if (listViewNodes != null && listViewNodes.size() > 0) {
                AccessibilityNodeInfo listViewNode = listViewNodes.get(0);

                AccessibilityNodeInfo developerNode = getChildNodeEqual(listViewNode, "开发者选项");
                long time = System.currentTimeMillis();
                while (developerNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                    if (developerNode.isScrollable()) {
                        listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        listViewNode.refresh();
                    } else {
                        break;
                    }
                    developerNode = getChildNodeEqual(listViewNode, "开发者选项");
                }

                if (performAction(developerNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                    sleepShort();
                } else {
                    showToastLong("请点击【开发者选项】");
                    return;
                }
            }
            return;

        }


        if ("com.vivo.settings.DevelpmentSettingsActivity2".equals(event.getClassName())) {
            List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

            if (listViewNodes != null && listViewNodes.size() > 0) {
                AccessibilityNodeInfo listViewNode = listViewNodes.get(0);

                AccessibilityNodeInfo developerNode = getChildNodeEqual(listViewNode, "USB 调试");
                long time = System.currentTimeMillis();
                while (developerNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                    if (developerNode.isScrollable()) {
                        listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        listViewNode.refresh();
                    } else {
                        break;
                    }
                    developerNode = getChildNodeEqual(listViewNode, "USB 调试");
                }


                if (performAction(developerNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                    sleepLong();

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

        if ("android.app.AlertDialog".equals(event.getClassName())) {
            AccessibilityNodeInfo cancelNode = getChildNodeEqual(sourceNode, "取消");

            if (performAction(cancelNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleepLong();
                resetStep();
                return;
            } else {
                showToastLong("请点击【取消】");
                return;
            }
        }

        if ("com.android.settings".equals(event.getPackageName())) {

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


//            }

        gotoDeveloperChoice();
    }

    @Override
    public void requestNotificationPermission(AccessibilityEvent event) {

    }

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

        if ("com.android.settings.Settings".equals(event.getClassName())) {
            AccessibilityNodeInfo safeNode = getChildNodeEqual(nodeInfo, "安全");

            if (performAction(safeNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleepShort();
                return;
            } else {
                showToastLong("请点击【安全】");
                return;
            }
        }

        if ("com.vivo.settings.VivoSubSettings".equals(event.getClassName())) {

            if (event.getText() != null) {
                if (event.getText().toString().equals("[安全]")) {
                    AccessibilityNodeInfo changeLauncherNode = getChildNodeEqual(nodeInfo, "更换系统桌面");

                    if (performAction(changeLauncherNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
                    } else {
                        showToastLong("请点击【更换系统桌面】");
                    }
                    return;

                } else if (event.getText().toString().equals("[更换系统桌面]")) {

                    AccessibilityNodeInfo lockLauncher = getChildNodeEqual(nodeInfo, "主屏键锁定系统桌面");
//
                    if (lockLauncher == null) {
                        showToastLong("请点击【允许更换桌面】");
                        return;
                    }


                    PermissionUtil.startVIVOManager(getApplicationContext());
                    sleep();
                    return;
                }
            }
        }


        if ("com.iqoo.secure.MainActivity".equals(event.getClassName())) {
            AccessibilityNodeInfo softwareMGNode = getChildNodeEqual(nodeInfo, "软件管理");

            if (performAction(softwareMGNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleepShort();
                return;
            } else {
                showToastLong("请点击【软件管理】");
                return;
            }
        }


        if ("com.iqoo.secure.appmanager.AppManagerActivity".equals(event.getClassName())) {
            AccessibilityNodeInfo permissionMGNode = getChildNodeEqual(nodeInfo, "权限管理");


            if (performAction(permissionMGNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleepShort();
                return;
            } else {
                showToastLong("请点击【权限管理】");
                return;
            }
        }

        if ("com.iqoo.secure.safeguard.PurviewTabActivity".equals(event.getClassName())) {
            AccessibilityNodeInfo defaultAppMGNode = getChildNodeEqual(nodeInfo, "默认软件设置");


            if (performAction(defaultAppMGNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleepShort();
                return;
            } else {
                showToastLong("请点击【默认软件设置】");
                return;
            }
        }


        if ("com.iqoo.secure.ui.phoneoptimize.DefaultAppSettingActivity".equals(event.getClassName())) {
            AccessibilityNodeInfo lanucherNode = getChildNodeEqual(nodeInfo, "桌面");


            if (performAction(lanucherNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleepShort();
                return;
            } else {
                showToastLong("请点击【桌面】");
                return;
            }
        }


        if ("com.iqoo.secure.ui.phoneoptimize.DefaultAppResolveActivity".equals(event.getClassName())) {

            AccessibilityNodeInfo targetAppName = getChildNodeEqual(nodeInfo, appName);
            if (performAction(targetAppName, AccessibilityNodeInfo.ACTION_CLICK)) {
                return;
            } else {
                showToastLong("请选择默认桌面为" + appName);
                return;
            }

        }


        if ("com.iqoo.secure.ui.phoneoptimize.DefaultAppConfirmActivity".equals(event.getClassName())) {
            AccessibilityNodeInfo deterNode = getChildNodeEqual(nodeInfo, "更换");

            if (performAction(deterNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleep();
                resetStep();
                return;
            } else {
                showToastLong("请点击【更换】");
                return;
            }
        }


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

            if (!"com.android.settings.Settings".equals(event.getClassName())) {
                getService().performGlobalAction(GLOBAL_ACTION_BACK);
                sleepShort();
                return;
            }
        }

        gotoDefaultLauncherSetting();


    }
}
