/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.server.wm;

import static android.server.wm.StateLogger.logE;
import static android.view.KeyEvent.KEYCODE_APP_SWITCH;
import static android.view.KeyEvent.KEYCODE_MENU;
import static android.view.KeyEvent.KEYCODE_SLEEP;
import static android.view.KeyEvent.KEYCODE_WAKEUP;
import static android.view.KeyEvent.KEYCODE_WINDOW;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

import android.app.DreamManager;
import android.app.KeyguardManager;
import android.content.Context;
import android.graphics.Point;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;

import androidx.test.uiautomator.UiDevice;

import com.android.compatibility.common.util.SystemUtil;

import java.util.function.BooleanSupplier;

/**
 * Helper class to interact with {@link UiDevice}.
 *
 * All references to {@link UiDevice} and {@link KeyEvent} should be here for easy debugging.
 */
public class UiDeviceUtils {

    private static final String TAG = "UiDeviceUtils";
    private static final boolean DEBUG = false;

    static void waitForDeviceIdle(long timeout) {
        if (DEBUG) Log.d(TAG, "waitForDeviceIdle: timeout=" + timeout);
        getDevice().waitForIdle(timeout);
    }

    public static void wakeUpAndUnlock(Context context) {
        final KeyguardManager keyguardManager = context.getSystemService(KeyguardManager.class);
        final PowerManager powerManager = context.getSystemService(PowerManager.class);
        final DreamManager dreamManager = context.getSystemService(DreamManager.class);
        if (keyguardManager == null || powerManager == null) {
            return;
        }

        if (keyguardManager.isKeyguardLocked() || !powerManager.isInteractive()
                || (dreamManager != null
                && SystemUtil.runWithShellPermissionIdentity(dreamManager::isDreaming))) {
            pressWakeupButton();
            pressUnlockButton();
        }
    }

    public static void wakeUpDevice() throws RemoteException {
        if (DEBUG) Log.d(TAG, "wakeUpDevice");
        getDevice().wakeUp();
    }

    public static void dragPointer(Point from, Point to, int steps) {
        if (DEBUG) Log.d(TAG, "dragPointer: from=" + from + " to=" + to + " steps=" + steps);
        getDevice().drag(from.x, from.y, to.x, to.y, steps);
    }

    public static void pressEnterButton() {
        if (DEBUG) Log.d(TAG, "pressEnterButton");
        getDevice().pressEnter();
    }

    /**
     * Simulates a pressed event of {@link KeyEvent#KEYCODE_HOME}. Note this will stop app switches
     * for 5s (see android.permission.STOP_APP_SWITCHES).
     */
    public static void pressHomeButton() {
        if (DEBUG) Log.d(TAG, "pressHomeButton");
        getDevice().pressHome();
    }

    public static void pressBackButton() {
        if (DEBUG) Log.d(TAG, "pressBackButton");
        getDevice().pressBack();
    }

    public static void pressMenuButton() {
        if (DEBUG) Log.d(TAG, "pressMenuButton");
        getDevice().pressMenu();
    }

    public static void pressSleepButton() {
        if (DEBUG) Log.d(TAG, "pressSleepButton");
        final PowerManager pm = getInstrumentation()
                .getContext().getSystemService(PowerManager.class);
        retryPressKeyCode(KEYCODE_SLEEP, () -> pm != null && !pm.isInteractive(),
                "***Waiting for device sleep...");
    }

    public static void pressWakeupButton() {
        if (DEBUG) Log.d(TAG, "pressWakeupButton");
        final PowerManager pm = getInstrumentation()
                .getContext().getSystemService(PowerManager.class);
        retryPressKeyCode(KEYCODE_WAKEUP, () -> pm != null && pm.isInteractive(),
                "***Waiting for device wakeup...");
    }

    public static void pressUnlockButton() {
        if (DEBUG) Log.d(TAG, "pressUnlockButton");
        final KeyguardManager kgm = getInstrumentation()
                .getContext().getSystemService(KeyguardManager.class);
        retryPressKeyCode(KEYCODE_MENU, () -> kgm != null && !kgm.isKeyguardLocked(),
                "***Waiting for device unlock...");
    }

    public static void pressWindowButton() {
        if (DEBUG) Log.d(TAG, "pressWindowButton");
        pressKeyCode(KEYCODE_WINDOW);
    }

    public static void pressAppSwitchButton() {
        if (DEBUG) Log.d(TAG, "pressAppSwitchButton");
        pressKeyCode(KEYCODE_APP_SWITCH);
    }

    private static void retryPressKeyCode(int keyCode, BooleanSupplier waitFor, String msg) {
        int retry = 1;
        do {
            pressKeyCode(keyCode);
            if (waitFor.getAsBoolean()) {
                return;
            }
            Log.d(TAG, msg + " retry=" + retry);
            SystemClock.sleep(50);
        } while (retry++ < 5);
        if (!waitFor.getAsBoolean()) {
            logE(msg + " FAILED");
        }
    }

    private static void pressKeyCode(int keyCode) {
        getDevice().pressKeyCode(keyCode);
    }

    private static UiDevice getDevice() {
        return UiDevice.getInstance(getInstrumentation());
    }
}
