package ru.otus.mobile.component;

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

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

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

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

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

    public UserItemComponent byUsername(String username) {
        for (int i = 0; i < items.size(); i++) {
            UserItemComponent item = get(i);
            if (username.equals(item.usernameText())) {
                return item;
            }
        }
        throw new IllegalStateException("User with username '" + username + "' was not found.");
    }
}
