package ru.otus.mobile.page;

import com.codeborne.selenide.SelenideElement;
import com.codeborne.selenide.WebDriverRunner;
import com.codeborne.selenide.appium.SelenideAppiumCollection;
import io.appium.java_client.AppiumBy;

import static com.codeborne.selenide.appium.SelenideAppium.$$;
import static com.codeborne.selenide.appium.SelenideAppium.$;

public abstract class AbsPageObject {
    private static final String APP_PACKAGE_PROPERTY = "app.package";
    private static final String DEFAULT_APP_PACKAGE = "ru.otus.wishlist";

    protected SelenideElement byId(String id) {
        return $(AppiumBy.id(fullIdValue(id)));
    }

    protected SelenideAppiumCollection allById(String id) {
        return $$(AppiumBy.id(fullIdValue(id)));
    }

    protected void back() {
        WebDriverRunner.getWebDriver().navigate().back();
    }

    protected static String fullIdValue(String id) {
        if (id.contains(":")) {
            return id;
        }
        String appPackage = System.getProperty(APP_PACKAGE_PROPERTY, DEFAULT_APP_PACKAGE);
        return appPackage + ":id/" + id;
    }
}
