package com.android.launcher3.auto_get_permission;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.annotation.TargetApi;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.view.accessibility.AccessibilityNodeInfo;

import com.android.launcher3.util.Logger;

import java.util.List;

public class AccessibilityNodeOperateUtil {
    private static final String TAG = "AccessibilityNodeOperateUtil";

    public static AccessibilityNodeInfo getChildNodeEqual(AccessibilityNodeInfo root, String text) {

        if (root == null)
            return null;
        if (root.getChildCount() > 0) {
            for (int i = 0; i < root.getChildCount(); i++) {
                AccessibilityNodeInfo child = root.getChild(i);

                if (child == null)
                    continue;
                Logger.e(TAG, child.getViewIdResourceName() + "\t" + child.getChildCount() + "child child\t" + child.toString());
                List<AccessibilityNodeInfo> nodeInfoList = child.findAccessibilityNodeInfosByText(text);
                if (nodeInfoList != null && nodeInfoList.size() > 0) {
                    for (AccessibilityNodeInfo findNode : nodeInfoList) {
                        Logger.e(TAG, findNode.getChildCount() + "fuzzy \t" + findNode.toString());
                        if (findNode.getText() == null)
                            continue;
                        if (findNode.getText().toString().equals(text)) {
                            Logger.e(TAG, "find equal \t" + findNode.toString());
                            return findNode;
                        }
                    }

                }

                child = getChildNodeEqual(child, text);

                if (child == null) {
                    continue;
                }
                if (child.getContentDescription() == null) {
                    continue;
                }

                if (child.getContentDescription().toString().equals(text)) {
//                    Logger.e(TAG, "find equal \t" + child.toString());
                    return child;
                }

            }
        }


        return null;
    }

    public static AccessibilityNodeInfo getChildNodeEqualByContentDesc(AccessibilityNodeInfo root, String text) {

        if (root == null)
            return null;
        if (root.getChildCount() > 0) {
            for (int i = 0; i < root.getChildCount(); i++) {
                AccessibilityNodeInfo child = root.getChild(i);

                if (child == null)
                    continue;
                Logger.e(TAG, child.getViewIdResourceName() + "\t" + child.getChildCount() + "child child\t" + child.toString());


                if (child.getContentDescription() != null && child.getContentDescription().toString().equals(text)) {
                    Logger.e(TAG, "find equal \t" + child.toString());
                    return child;
                }

                child = getChildNodeEqualByContentDesc(child, text);

                if (child == null) {
                    continue;
                }

                if (child.getContentDescription() == null) {
                    continue;
                }

                if (child.getContentDescription().toString().equals(text)) {
                    Logger.e(TAG, "find equal \t" + child.toString());
                    return child;
                }

            }
        }


        return null;
    }


    public static AccessibilityNodeInfo getChildNodeByIdEqualText(AccessibilityNodeInfo root, String viewId, String text) {

        if (root == null)
            return null;
        List<AccessibilityNodeInfo> nodeInfoList = root.findAccessibilityNodeInfosByViewId(viewId);

        if (nodeInfoList != null && nodeInfoList.size() > 0) {
            for (AccessibilityNodeInfo childNode : nodeInfoList) {
                if (childNode.getText() == null)
                    continue;
                if (childNode.getText().toString().equals(text)) {
                    Logger.e(TAG, "find equal \t" + childNode.toString());
                    return childNode;
                }
            }
        }
        return null;
    }


    @TargetApi(Build.VERSION_CODES.O)
    public static boolean click(AccessibilityService service, Rect bounds) {
        Path path = new Path();
        path.moveTo(bounds.left + (bounds.right - bounds.left) / 2, bounds.top + (bounds.bottom - bounds.top) / 2);
        path.lineTo(bounds.left + (bounds.right - bounds.left) / 2, bounds.top + (bounds.bottom - bounds.top) / 2);
        GestureDescription description = new GestureDescription.Builder().addStroke(new GestureDescription.StrokeDescription(path, 0, 50)).build();
        boolean isDispatch = service.dispatchGesture(description, new AccessibilityService.GestureResultCallback() {
            @Override
            public void onCompleted(GestureDescription gestureDescription) {
//                Logger.e(TAG, "onCompleted");
            }

            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                super.onCancelled(gestureDescription);
//                Logger.e(TAG, "onCancelled");
            }
        }, null);

//        Logger.e(TAG, "isDispatch\t" + isDispatch);
        return isDispatch;
    }


}
