package ru.otus.mobile.component;

import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.SelenideElement;
import io.appium.java_client.AppiumBy;

public final class WishlistsContentComponent extends BaseMobileComponent {
    private final ElementsCollection items;

    public WishlistsContentComponent(SelenideElement root) {
        super(root);
        this.items = root.$$(AppiumBy.id(fullIdValue("wishlist_item")));
    }

    public WishlistItemComponent get(int index) {
        return new WishlistItemComponent(items.get(index));
    }

    public WishlistItemComponent first() {
        return get(0);
    }

    public WishlistItemComponent byTitle(String title) {
        for (int i = 0; i < items.size(); i++) {
            WishlistItemComponent item = get(i);
            if (title.equals(item.titleText())) {
                return item;
            }
        }
        throw new IllegalStateException("Wishlist with title '" + title + "' was not found.");
    }
}
