/*
 * Copyright (c) 2020. BoostTag E.I.R.L. Romell D.Z.
 * All rights reserved
 * porfile.romellfudi.com
 */

package com.romellfudi.ussdlibrary;

import android.accessibilityservice.AccessibilityService;
import android.os.Build;
import android.os.Parcel;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.elvishew.xlog.XLog;

import java.util.ArrayList;
import java.util.List;

public class USSDServicePay extends AccessibilityService {

    public static AccessibilityEvent event;

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Log.d("hzshkj", String.format("*USSDServicePay onAccessibilityEvent: [type] %s [class] %s [package] %s [time] %s [text] %s",
                event.getEventType(), event.getClassName(), event.getPackageName(),
                event.getEventTime(), event.getText()));
        XLog.i(String.format("*USSDServicePay onAccessibilityEvent: [type] %s [class] %s [package] %s [time] %s [text] %s",
                event.getEventType(), event.getClassName(), event.getPackageName(),
                event.getEventTime(), event.getText()));

        if ("com.android.systemui".contentEquals(event.getPackageName()) && isScreenWidget(event)) {
            try {
                clickOnButton(event, 1);
            } catch (Exception e) {
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            USSDServicePay.event = new AccessibilityEvent(event);
        } else {
            Parcel parcel = Parcel.obtain();
            event.writeToParcel(parcel, 0);
            parcel.setDataPosition(0);
            AccessibilityEvent newEvent = AccessibilityEvent.CREATOR.createFromParcel(parcel);
            parcel.recycle();
            USSDServicePay.event = newEvent;
        }

    }

    private boolean isScreenWidget(AccessibilityEvent event) {
        return (event.getClassName().equals("android.app.AlertDialog")
                || event.getClassName().equals("com.android.systemui.statusbar.phone.AlertDialogWithDelegate"));
    }

    protected static void clickOnButton(AccessibilityEvent event, int index) {
        int count = -1;
        for (AccessibilityNodeInfo leaf : getLeaves(event)) {
            if (leaf.getClassName().toString().toLowerCase().contains("button")) {
                count++;
                if (count == index) {
                    leaf.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        }
    }

    private static List<AccessibilityNodeInfo> getLeaves(AccessibilityEvent event) {
        List<AccessibilityNodeInfo> leaves = new ArrayList<>();
        if (event.getSource() != null) {
            getLeaves(leaves, event.getSource());
        }

        return leaves;
    }

    private static void getLeaves(List<AccessibilityNodeInfo> leaves, AccessibilityNodeInfo node) {
        if (node.getChildCount() == 0) {
            leaves.add(node);
            return;
        }

        for (int i = 0; i < node.getChildCount(); i++) {
            getLeaves(leaves, node.getChild(i));
        }
    }

    @Override
    public void onInterrupt() {

    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.d("hzshkj", "[USSDServicePay] onServiceConnected: "+this);
        PayController.setMyAccessibilityService(this);
    }


}
