package ru.otus.mobile.ui;

import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.WebDriverRunner;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.RemoteWebElement;

import java.util.HashMap;
import java.util.Map;

import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.$$;

public final class MobileUi {
    private MobileUi() {
    }

    public static SelenideElement byId(String id) {
        return $(MobileSelectors.id(id));
    }

    public static ElementsCollection allById(String id) {
        return $$(MobileSelectors.id(id));
    }

    public static SelenideElement byIdOrAccessibility(String id) {
        if (UiWait.exists(MobileSelectors.id(id))) {
            return $(MobileSelectors.id(id));
        }
        if (UiWait.exists(MobileSelectors.accessibilityId(id))) {
            return $(MobileSelectors.accessibilityId(id));
        }
        return $(MobileSelectors.id(id));
    }

    public static SelenideElement byTextContains(String text) {
        return $(MobileSelectors.textContains(text));
    }

    public static boolean existsById(String id) {
        return UiWait.exists(MobileSelectors.id(id));
    }

    public static SelenideElement byIdOrText(String id, String text) {
        ElementsCollection byId = allById(id);
        if (!byId.isEmpty()) {
            return byId.first();
        }
        return $(MobileSelectors.textContains(text));
    }

    public static SelenideElement byIdOrTextOrAccessibility(String id, String text) {
        ElementsCollection byId = allById(id);
        if (!byId.isEmpty()) {
            return byId.first();
        }
        ElementsCollection byA11y = $$(AppiumBy.accessibilityId(id));
        if (!byA11y.isEmpty()) {
            return byA11y.first();
        }
        return $(MobileSelectors.textContains(text));
    }

    public static void back() {
        AndroidDriver driver = (AndroidDriver) WebDriverRunner.getWebDriver();
        driver.pressKey(new KeyEvent(AndroidKey.BACK));
    }

    public static void longPress(SelenideElement element) {
        String elementId = ((RemoteWebElement) element.toWebElement()).getId();
        Map<String, Object> params = new HashMap<>();
        params.put("elementId", elementId);
        params.put("duration", 1000);
        ((JavascriptExecutor) WebDriverRunner.getWebDriver())
                .executeScript("mobile: longClickGesture", params);
    }

    public static void hideKeyboard() {
        AndroidDriver driver = (AndroidDriver) WebDriverRunner.getWebDriver();
        try {
            driver.hideKeyboard();
        } catch (Exception ignored) {
        }
    }

    public static void restartApp(String appPackage) {
        AndroidDriver driver = (AndroidDriver) WebDriverRunner.getWebDriver();
        driver.terminateApp(appPackage);
        driver.activateApp(appPackage);
    }
}
