package ru.otus.mobile.component;

import com.codeborne.selenide.CollectionCondition;
import com.codeborne.selenide.Condition;
import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.SelenideElement;

import java.time.Duration;

public final class WishlistsContentComponent extends BaseMobileComponent {
    private final ElementsCollection items = allByIdInRoot("wishlist_item");

    public WishlistsContentComponent(SelenideElement root) {
        super(root);
    }

    public WishlistItemComponent get(int index) {
        return new WishlistItemComponent(items.get(index).shouldBe(Condition.visible, Duration.ofSeconds(15)));
    }

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

    public WishlistItemComponent byTitle(String title) {
        items.shouldHave(CollectionCondition.sizeGreaterThan(0), Duration.ofSeconds(15));
        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.");
    }
}
