package ru.otus.mobile.page;

import com.codeborne.selenide.Condition;
import com.google.inject.Inject;
import ru.otus.mobile.component.BottomNavigationComponent;
import ru.otus.mobile.config.MobileConfig;

public final class UsersPage extends AbsBasePage {
    @Inject
    public UsersPage(MobileConfig config, BottomNavigationComponent bottomNavigation) {
        super(config, bottomNavigation);
    }

    public void open() {
        bottomNavigation.openUsers();
        byId("users_content").shouldBe(Condition.visible);
    }

    public void openUser(String username) {
        String appPackage = config().appPackage();
        String selector = "new UiScrollable(new UiSelector().resourceId(\"" + appPackage + ":id/users\"))"
                + ".setMaxSearchSwipes(12)"
                + ".scrollIntoView(new UiSelector().resourceId(\"" + appPackage + ":id/username\")"
                + ".textContains(\"" + username.replace("\"", "\\\"") + "\"))";
        byUiAutomator(selector).shouldBe(Condition.visible).click();
    }

    public void openFirstWishlist() {
        allById("wishlist_item").first().shouldBe(Condition.visible).click();
    }

    public void openFirstGift() {
        allById("gift_item").first().shouldBe(Condition.visible).click();
    }

    public boolean isReserved() {
        return Boolean.parseBoolean(byId("reserved").shouldBe(Condition.visible).getAttribute("checked"));
    }

    public void toggleReservation() {
        tap("reserved");
    }
}
