import * as TKUnit from "../../TKUnit";
// >> article-creating-view
import { isIOS, isAndroid } from "tns-core-modules/platform";
import * as utils from "tns-core-modules/utils/utils";
import * as helper from "../helper";
import * as viewModule from "tns-core-modules/ui/core/view";

// >> article-require-placeholder-module
import * as placeholderModule from "tns-core-modules/ui/placeholder";
// << article-require-placeholder-module

function creatingView(args) {
    let nativeView;
    if (isIOS) {
        nativeView = UITextView.new();
        nativeView.text = "Native";
    } else if (isAndroid) {
        nativeView = new android.widget.TextView(utils.ad.getApplicationContext());
        nativeView.setText("Native");
    }

    args.view = nativeView;
}

exports.creatingView = creatingView;
// << article-creating-view

export function test_placeholder_creatingView() {
    const p = new placeholderModule.Placeholder();
    p.on(placeholderModule.Placeholder.creatingViewEvent, (args: placeholderModule.CreateViewEventData) => {
        let nativeView;
        if (isIOS) {
            nativeView = UITextView.new();
            nativeView.text = "Native";
        } else if (isAndroid) {
            nativeView = new android.widget.TextView(utils.ad.getApplicationContext());
            nativeView.setText("Native");
        }

        args.view = nativeView;
    });

    function testAction(views: Array<viewModule.View>) {
        if (isIOS) {
            TKUnit.assert(p.nativeViewProtected instanceof UITextView, "nativeView property should be UITextView. Current value: " + p.nativeViewProtected);
        } else if (isAndroid) {
            TKUnit.assert(p.nativeViewProtected instanceof android.widget.TextView, "Native view should be android.widget.TextView. Current value: " + p.nativeViewProtected);
        }
    };

    helper.buildUIAndRunTest(p, testAction);
}

export function test_placeholder_will_not_crash_wihout_creatingView() {
   const p = new placeholderModule.Placeholder();

    function testAction(views: Array<viewModule.View>) {
        if (isIOS) {
            TKUnit.assert(p.ios === undefined, "ios property should be undefined. Current value: " + p.ios);
        } else if (isAndroid) {
            TKUnit.assert(p.android === undefined, "android view should be undefined. Current value: " + p.android);
        }
    };

    helper.buildUIAndRunTest(p, testAction);
}