package ru.otus.mobile.page;

import com.codeborne.selenide.Condition;
import com.codeborne.selenide.CollectionCondition;
import com.codeborne.selenide.SelenideElement;
import com.google.inject.Inject;
import ru.otus.mobile.component.BottomNavigationComponent;
import ru.otus.mobile.config.MobileConfig;

import java.time.Duration;

public final class UsersPage extends AbsBasePage {
    @Inject
    public UsersPage(MobileConfig config, BottomNavigationComponent bottomNavigation) {
        super(config, bottomNavigation);
    }

    public void open() {
        bottomNavigation.openUsers();
        byId("users_content").shouldBe(Condition.visible);
    }

    public void filterByUsername(String username) {
        openFilter();
        SelenideElement input = filterInput().shouldBe(Condition.visible, Duration.ofSeconds(15));
        input.clear();
        input.setValue(username);
        applyFilter();
        byTextContains(username).shouldBe(Condition.visible, Duration.ofSeconds(15));
    }

    public void openUser(String username) {
        String appPackage = config().appPackage();
        String xpath = "//android.widget.TextView[@resource-id='" + appPackage + ":id/username'"
                + " and @text=" + xpathLiteral(username) + "]";
        SelenideElement user = byXpath(xpath);
        if (user.exists()) {
            user.shouldBe(Condition.visible, Duration.ofSeconds(15)).click();
        } else {
            scrollToText(username).shouldBe(Condition.visible, Duration.ofSeconds(15)).click();
        }
        byTextContains(username).shouldBe(Condition.visible, Duration.ofSeconds(15));
    }

    public void openFirstWishlist() {
        allById("wishlist_item")
                .shouldHave(CollectionCondition.sizeGreaterThan(0), Duration.ofSeconds(15))
                .first()
                .shouldBe(Condition.visible, Duration.ofSeconds(15))
                .click();
    }

    public void openWishlist(String name) {
        scrollToText(name).shouldBe(Condition.visible, Duration.ofSeconds(15)).click();
    }

    public void openFirstGift() {
        allById("gift_item")
                .shouldHave(CollectionCondition.sizeGreaterThan(0), Duration.ofSeconds(15))
                .first()
                .shouldBe(Condition.visible, Duration.ofSeconds(15))
                .click();
    }

    public void openGift(String name) {
        scrollToText(name).shouldBe(Condition.visible, Duration.ofSeconds(15)).click();
    }

    public boolean isReserved() {
        return Boolean.parseBoolean(byId("reserved").shouldBe(Condition.visible).getAttribute("checked"));
    }

    public void toggleReservation() {
        tap("reserved");
    }

    private void openFilter() {
        if (byId("users_filter").exists()) {
            tap("users_filter");
            byId("users_filter_bottom_sheet").shouldBe(Condition.visible, Duration.ofSeconds(15));
            return;
        }
        if (byId("filter_button").exists()) {
            tap("filter_button");
            byId("users_filter_bottom_sheet").shouldBe(Condition.visible, Duration.ofSeconds(15));
            return;
        }
        if (byId("action_filter").exists()) {
            tap("action_filter");
            byId("users_filter_bottom_sheet").shouldBe(Condition.visible, Duration.ofSeconds(15));
            return;
        }
        SelenideElement button = byUiAutomator("new UiSelector().descriptionContains(\"Фильтр\")");
        if (button.exists()) {
            button.click();
            byId("users_filter_bottom_sheet").shouldBe(Condition.visible, Duration.ofSeconds(15));
            return;
        }
        button = byUiAutomator("new UiSelector().descriptionContains(\"Filter\")");
        if (button.exists()) {
            button.click();
            byId("users_filter_bottom_sheet").shouldBe(Condition.visible, Duration.ofSeconds(15));
            return;
        }
        throw new IllegalStateException("Filter button was not found on Users screen.");
    }

    private SelenideElement filterInput() {
        if (byId("filter_input").exists()) {
            return byId("filter_input");
        }
        if (byId("username_filter_input").exists()) {
            return byId("username_filter_input");
        }
        if (byId("users_filter_input").exists()) {
            return byId("users_filter_input");
        }
        if (byId("username_input").exists()) {
            return byId("username_input");
        }
        if (byId("search_src_text").exists()) {
            return byId("search_src_text");
        }
        if (byId("query_text").exists()) {
            return byId("query_text");
        }
        String appPackage = config().appPackage();
        SelenideElement editText = byXpath(
                "//android.view.ViewGroup[@resource-id='" + appPackage + ":id/users_filter_bottom_sheet']"
                        + "//android.widget.EditText"
        );
        if (editText.exists()) {
            return editText;
        }
        throw new IllegalStateException("Filter input was not found on Users screen.");
    }

    private void applyFilter() {
        if (byId("apply_button").exists()) {
            tap("apply_button");
            return;
        }
        if (byId("apply").exists()) {
            tap("apply");
            return;
        }
        if (byText("ПРИМЕНИТЬ").exists()) {
            byText("ПРИМЕНИТЬ").click();
            return;
        }
        if (byText("APPLY").exists()) {
            byText("APPLY").click();
            return;
        }
        throw new IllegalStateException("Apply filter button was not found on Users filter screen.");
    }
}
