import * as TKUnit from "../../TKUnit";
import * as app from "tns-core-modules/application";
import * as button from "tns-core-modules/ui/button";
import * as testModule from "../../ui-test";
import * as layoutHelper from "../layouts/layout-helper";
import { Page } from "tns-core-modules/ui/page";
import * as frame from "tns-core-modules/ui/frame";

// >> article-require-scrollview-module
import * as scrollViewModule from "tns-core-modules/ui/scroll-view";
// << article-require-scrollview-module

class ScrollLayoutTest extends testModule.UITest<scrollViewModule.ScrollView> {

    public create(): scrollViewModule.ScrollView {
        let scrollView = new scrollViewModule.ScrollView();
        scrollView.orientation = "vertical";

        scrollView.width = { value: 200, unit: "px" };
        scrollView.height = { value: 300, unit: "px" };

        let btn = new button.Button();
        btn.text = "test";
        btn.width = { value: 500, unit: "px" };
        btn.height = { value: 500, unit: "px" };
        scrollView.content = btn;

        return scrollView;
    }

    public test_snippets() {
        // >> article-creating-scrollview
        var scrollView = new scrollViewModule.ScrollView();
        // << article-creating-scrollview
        TKUnit.assertTrue(scrollView !== null, "ScrollView should be created.");
    }

    public test_default_TNS_values() {
        let scroll = new scrollViewModule.ScrollView();
        TKUnit.assertEqual(scroll.orientation, "vertical", "Default this.testView.orientation");
        TKUnit.assertEqual(scroll.verticalOffset, 0, "Default this.testView.verticalOffset");
        TKUnit.assertEqual(scroll.horizontalOffset, 0, "Default this.testView.horizontalOffset");
    }

    public test_vertical_oriantation_creates_correct_native_view() {
        this.testView.orientation = "vertical";

        if (app.android) {
            TKUnit.assert(this.testView.android instanceof org.nativescript.widgets.VerticalScrollView, "android property should be instanceof org.nativescript.widgets.VerticalScrollView");
        }
        else if (app.ios) {
            TKUnit.assert(this.testView.ios instanceof UIScrollView, "ios property is UIScrollView");
        }
    }

    public test_horizontal_oriantation_creates_correct_native_view() {
        this.testView.orientation = "horizontal";

        if (app.android) {
            TKUnit.assert(this.testView.android instanceof org.nativescript.widgets.HorizontalScrollView, "android property should be instanceof org.nativescript.widgets.HorizontalScrollView");
        }
        else if (app.ios) {
            TKUnit.assert(this.testView.ios instanceof UIScrollView, "ios property is UIScrollView");
        }
    }

    public test_scrollableHeight_vertical_orientation_when_content_is_small() {
        this.testView.content.width = { value: 100, unit: "px" };
        this.testView.content.height = { value: 100, unit: "px" };
        this.waitUntilTestElementLayoutIsValid();

        TKUnit.assertEqual(this.testView.scrollableHeight, 0, "this.testView.scrollableHeight");
        TKUnit.assertEqual(this.testView.scrollableWidth, 0, "this.testView.scrollableWidth");
    }

    public test_scrollableHeight_vertical_orientation_when_content_is_big() {
        this.testView.content.width = { value: 100, unit: "px" };
        this.waitUntilTestElementLayoutIsValid();

        TKUnit.assertAreClose(layoutHelper.dip(this.testView.scrollableHeight), 200, 0.4, "this.testView.scrollableHeight");
        TKUnit.assertEqual(this.testView.scrollableWidth, 0, "this.testView.scrollableWidth");
    }

    public test_scrollableWidth_horizontal_orientation_when_content_is_small() {
        this.testView.orientation = "horizontal";
        this.testView.content.width = { value: 100, unit: "px" };
        this.testView.content.height = { value: 100, unit: "px" };
        this.waitUntilTestElementLayoutIsValid();

        TKUnit.assertEqual(this.testView.scrollableHeight, 0, "this.testView.scrollableHeight");
        TKUnit.assertEqual(this.testView.scrollableWidth, 0, "this.testView.scrollableWidth");
    }

    public test_scrollableWidth_horizontal_orientation_when_content_is_big() {
        this.testView.orientation = "horizontal";
        this.testView.content.height = { value: 100, unit: "px" };
        this.waitUntilTestElementLayoutIsValid();

        TKUnit.assertEqual(this.testView.scrollableHeight, 0, "this.testView.scrollableHeight");
        TKUnit.assertAreClose(layoutHelper.dip(this.testView.scrollableWidth), 300, 0.4, "this.testView.scrollableWidth");
    }

    public test_scrollToVerticalOffset_no_animation() {
        this.waitUntilTestElementLayoutIsValid();

        TKUnit.assertEqual(this.testView.verticalOffset, 0, "this.testView.verticalOffset");
        this.testView.scrollToVerticalOffset(layoutHelper.dp(100), false);
        TKUnit.assertAreClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.1, "this.testView.verticalOffset");
    }

    public test_scrollToVerticalOffset_with_animation() {
        this.waitUntilTestElementLayoutIsValid();

        TKUnit.assertEqual(this.testView.verticalOffset, 0, "this.testView.verticalOffset");
        this.testView.scrollToVerticalOffset(layoutHelper.dp(100), true);

        // No synchronous change. 
        TKUnit.assertEqual(this.testView.verticalOffset, 0, "this.testView.verticalOffset");

        TKUnit.waitUntilReady(() => { return TKUnit.areClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.9); });

        // The scrolling animation should be finished by now
        TKUnit.assertAreClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.9, "this.testView.verticalOffset");
    }

    public test_scrollToHorizontalOffset_no_animation() {
        this.testView.orientation = "horizontal";
        this.waitUntilTestElementLayoutIsValid();

        TKUnit.assertEqual(this.testView.horizontalOffset, 0, "this.testView.horizontalOffset");
        this.testView.scrollToHorizontalOffset(layoutHelper.dp(100), false);
        TKUnit.assertAreClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.1, "this.testView.horizontalOffset");
    }

    public test_scrollToHorizontalOffset_with_animation() {
        this.testView.orientation = "horizontal";
        this.waitUntilTestElementLayoutIsValid();

        TKUnit.assertEqual(this.testView.horizontalOffset, 0, "this.testView.horizontalOffset");
        this.testView.scrollToHorizontalOffset(layoutHelper.dp(100), true);

        // No synchronous change. 
        TKUnit.assertEqual(this.testView.horizontalOffset, 0, "this.testView.horizontalOffset");

        TKUnit.waitUntilReady(() => { return TKUnit.areClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.9); });

        // The scrolling animation should be finished by now
        TKUnit.assertAreClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.9, "this.testView.horizontalOffset");
    }

    public test_scrollView_persistsState_vertical() {
        this.waitUntilTestElementLayoutIsValid();

        this.testView.scrollToVerticalOffset(layoutHelper.dp(100), false);
        TKUnit.assertAreClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.1, "this.testView.verticalOffset before navigation");

        let page = new Page();
        let createFunc = () => {
            return page;
        };

        let entry: frame.NavigationEntry = { create: createFunc, animated: false };
        frame.topmost().navigate(entry);
        TKUnit.waitUntilReady(() => page.isLayoutValid);
        frame.topmost().goBack();

        // Wait for the page to reload.
        TKUnit.waitUntilReady(() => { return TKUnit.areClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.1); });

        // Check verticalOffset after navigation
        TKUnit.assertAreClose(layoutHelper.dip(this.testView.verticalOffset), 100, 0.1, "this.testView.verticalOffset after navigation");
    }

    public test_scrollView_persistsState_horizontal() {
        this.testView.orientation = "horizontal";
        this.waitUntilTestElementLayoutIsValid();

        this.testView.scrollToHorizontalOffset(layoutHelper.dp(100), false);

        TKUnit.assertAreClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.1, "this.testView.horizontalOffset before navigation");

        let page = new Page();
        let createFunc = () => {
            return page;
        };

        let entry: frame.NavigationEntry = { create: createFunc, animated: false };
        frame.topmost().navigate(entry);
        TKUnit.waitUntilReady(() => page.isLayoutValid);
        frame.topmost().goBack();

        // Wait for the page to reload.
        TKUnit.waitUntilReady(() => { return TKUnit.areClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.1); });

        // Check verticalOffset after navigation
        TKUnit.assertAreClose(layoutHelper.dip(this.testView.horizontalOffset), 100, 0.1, "this.testView.horizontalOffset after navigation");
    }

    public test_scrollView_vertical_raised_scroll_event() {
        var scrollY: number;
        this.testView.on(scrollViewModule.ScrollView.scrollEvent, (args: scrollViewModule.ScrollEventData) => {
            scrollY = args.scrollY;
        });

        this.waitUntilTestElementLayoutIsValid();

        this.testView.scrollToVerticalOffset(layoutHelper.dp(100), false);
        TKUnit.waitUntilReady(function () { return scrollY > 0; });
        TKUnit.assertEqual(scrollY, this.testView.verticalOffset);
    }

    public test_scrollView_horizontal_raised_scroll_event() {
        this.testView.orientation = "horizontal";

        var scrollX: number;
        this.testView.on(scrollViewModule.ScrollView.scrollEvent, (args: scrollViewModule.ScrollEventData) => {
            scrollX = args.scrollX;
        });

        this.waitUntilTestElementLayoutIsValid();

        this.testView.scrollToHorizontalOffset(layoutHelper.dp(100), false);
        TKUnit.waitUntilReady(function () { return scrollX > 0; });
        TKUnit.assertEqual(scrollX, this.testView.horizontalOffset);
    }

    public test_scrollView_vertical_raised_scroll_event_after_loaded() {
        this.waitUntilTestElementLayoutIsValid();

        var scrollY: number;
        this.testView.on(scrollViewModule.ScrollView.scrollEvent, (args: scrollViewModule.ScrollEventData) => {
            scrollY = args.scrollY;
        });

        this.testView.scrollToVerticalOffset(layoutHelper.dp(100), false);
        TKUnit.waitUntilReady(function () { return scrollY > 0; });
        TKUnit.assertEqual(scrollY, this.testView.verticalOffset);
        TKUnit.assertEqual(scrollY, layoutHelper.dp(100));
    }

    public test_scrollView_horizontal_raised_scroll_event_after_loaded() {
        this.testView.orientation = "horizontal";
        this.waitUntilTestElementLayoutIsValid();

        var scrollX: number;
        this.testView.on(scrollViewModule.ScrollView.scrollEvent, (args: scrollViewModule.ScrollEventData) => {
            scrollX = args.scrollX;
        });

        this.waitUntilTestElementLayoutIsValid();

        this.testView.scrollToHorizontalOffset(layoutHelper.dp(100), false);
        TKUnit.waitUntilReady(function () { return scrollX > 0; });
        TKUnit.assertEqual(scrollX, this.testView.horizontalOffset);
        TKUnit.assertEqual(scrollX, layoutHelper.dp(100));
    }

    public test_scrollView_horizontal_can_set_indicator_state() {
        this.testView.orientation = "horizontal";
        this.testView.scrollBarIndicatorVisible = true;
        this.waitUntilTestElementLayoutIsValid();

        if (app.ios) {
            TKUnit.assertEqual(this.testView.ios.showsHorizontalScrollIndicator, true);
        } else {
            TKUnit.assertEqual(this.testView.android.isHorizontalScrollBarEnabled(), true);
        }

        this.testView.scrollBarIndicatorVisible = false;
        this.waitUntilTestElementLayoutIsValid();

        if (app.ios) {
            TKUnit.assertEqual(this.testView.ios.showsHorizontalScrollIndicator, false);
        } else {
            TKUnit.assertEqual(this.testView.android.isHorizontalScrollBarEnabled(), false);
        }
    }

    public test_scrollView_vertical_can_set_indicator_state() {
        this.testView.orientation = "vertical";
        this.testView.scrollBarIndicatorVisible = true;
        this.waitUntilTestElementLayoutIsValid();

        if (app.ios) {
            TKUnit.assertEqual(this.testView.ios.showsVerticalScrollIndicator, true);
        } else {
            TKUnit.assertEqual(this.testView.android.isVerticalScrollBarEnabled(), true);
        }

        this.testView.scrollBarIndicatorVisible = false;
        this.waitUntilTestElementLayoutIsValid();

        if (app.ios) {
            TKUnit.assertEqual(this.testView.ios.showsVerticalScrollIndicator, false);
        } else {
            TKUnit.assertEqual(this.testView.android.isVerticalScrollBarEnabled(), false);
        }
    }
}

export function createTestCase(): ScrollLayoutTest {
    return new ScrollLayoutTest();
}
