package ru.otus.mobile.component;

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

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

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

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

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

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