package ru.otus.mobile.component;

import com.codeborne.selenide.SelenideElement;

public final class UsersFilterComponent extends BaseMobileComponent {
    public UsersFilterComponent(SelenideElement root) {
        super(root);
    }

    public void applyByUsername(String username) {
        SelenideElement filterInput = filterInput();
        SelenideElement applyButton = applyButton();
        filterInput.click();
        filterInput.clear();
        filterInput.sendKeys(username);
        applyButton.click();
    }

    private SelenideElement filterInput() {
        if (byIdInRoot("username_input").exists()) {
            return byIdInRoot("username_input");
        }
        if (byIdInRoot("filter_input").exists()) {
            return byIdInRoot("filter_input");
        }
        if (byIdInRoot("users_filter_input").exists()) {
            return byIdInRoot("users_filter_input");
        }
        return byIdInRoot("username_filter_input");
    }

    private SelenideElement applyButton() {
        if (byIdInRoot("apply_button").exists()) {
            return byIdInRoot("apply_button");
        }
        return byIdInRoot("apply");
    }
}
