import { AppiumDriver } from "nativescript-dev-appium";
import { Screen } from "./screen"
import { assert } from "chai";

const time = 1;
export const roots = ["setFrameRootView", "setLayoutRootView", "setTabRootView", "setNamedFrameRootView"];

export async function modalFrameBackground(driver: AppiumDriver, screen: Screen) {
    await driver.backgroundApp(time);
    await screen.loadedModalFrame();
}

export async function testDialogBackground(driver: AppiumDriver, screen: Screen, isInFrame: boolean = true) {
    await screen.showDialogConfirm();
    await screen.loadedConfirmDialog();

    await driver.backgroundApp(time);
    await screen.loadedConfirmDialog();

    await screen.closeDialog();
    if (isInFrame) {
        await screen.loadedModalFrame();
    }
}

export async function testSecondPageBackground(driver: AppiumDriver, screen: Screen) {
    await screen.navigateToSecondPage();
    await screen.loadedSecondPage();

    await driver.backgroundApp(time);
    await screen.loadedSecondPage();

    await screen.goBackFromSecondPage();
    await screen.loadedModalFrame();
}

export async function testSecondPageClose(driver: AppiumDriver, screen: Screen) {
    await screen.navigateToSecondPage();
    await screen.loadedSecondPage();

    await screen.closeModal();
    await screen.loadedHome();
}

export async function testNestedModalFrameBackground(driver: AppiumDriver, screen: Screen, isInFrame: boolean = true) {
    await screen.showNestedModalFrame();
    await screen.loadedNestedModalFrame();

    await driver.backgroundApp(time);
    await screen.loadedNestedModalFrame();

    await screen.closeModalNested();
    isInFrame ? await screen.loadedModalFrame() : await screen.loadedModalPage();
}

export async function testNestedModalPageBackground(driver: AppiumDriver, screen: Screen, isInFrame: boolean = true) {
    await screen.showNestedModalPage();
    await screen.loadedNestedModalPage();

    await driver.backgroundApp(time);
    await screen.loadedNestedModalPage();

    await screen.closeModalNested();
    isInFrame ? await screen.loadedModalFrame() : await screen.loadedModalPage();
}

export async function modalPageBackground(driver: AppiumDriver, screen: Screen, isInFrame: boolean = true) {
    await driver.backgroundApp(time);
    isInFrame ? await screen.loadedModalFrame() : await screen.loadedModalPage();
}

export async function modalTabViewBackground(driver: AppiumDriver, screen: Screen) {
    await driver.backgroundApp(time);
    await screen.loadedModalTabView();
}

export async function testSecondItemBackground(driver: AppiumDriver, screen: Screen) {
    await screen.navigateToSecondItem();
    await screen.loadedSecondItem();

    await driver.backgroundApp(time);
    await screen.loadedSecondItem();

    await screen.navigateToFirstItem();
    await screen.loadedFirstItem();
}

export async function assertComponent(driver: AppiumDriver, message: string) {
    const lbl = await driver.waitForElement(message, 5000);
    assert.isTrue(await lbl.isDisplayed());
}

export async function navigateToSecondComponent(driver: AppiumDriver) {
    const navigateBtnTap = await driver.findElementByAutomationText("Go To Second (to open shared modal)");
    await navigateBtnTap.click();
}

export async function goBack(driver: AppiumDriver) {
    const backBtnTap = await driver.findElementByAutomationText("go back");
    await backBtnTap.click();
}