﻿// Ensure TypeScript compiler is load for VS versions
// below 2015.
// https://www.microsoft.com/en-us/download/details.aspx?id=48739
// Will need a TypeScript definition file for jQuery
// jquery.d.ts (see NuGet jQuery.TypeScript.DefinedType)
$(function ()
{
    var output: string = "";

    function DisplayLine(text: string): void
    {
        output = output + text + "<br/>";
    }

    // Interfaces
    var clockA: ClockA = new ClockA();
    var clockB: ClockB = new ClockB(3, 40);
    DisplayLine("Time is " + clockA.GetCurrentTime());

    var timePrinter: TimePrinter;
    timePrinter = new TimePrinter();
    timePrinter.Print(clockA);
    timePrinter.Print(clockB);

    // You can use interfaces to specify optional properties
    interface SquareOptions
    {
        colour?: string;        // ? means optional
        width?: number;
    }

    // Function returns an object literal with colour and area attributes.
    function CreateSquare(options: SquareOptions): { colour: string; area: number; }
    {
        var square: { colour: string; area: number; };
        
        // Create object literal with default values
        square = { colour: "white", area: 100 };

        // Check which optional values have been provided and use them
        if (options.colour) square.colour = options.colour;
        if (options.width) square.area = options.width * options.width;

        return square;
    }

    var square: { colour: string; area: number };

    // If the object literal meets the interface, it doesn't
    // have to explicitly implement it.
    // i.e. the object literals being passed to CreateSquare() all meet the interface
    // SquareOptions above.
    square = CreateSquare({});   // Empty object literal {}
    DisplayLine("Square colour " + square.colour + " area " + square.area);     // default "white" and 100
    square = CreateSquare({ colour: "red" });
    DisplayLine("Square colour " + square.colour + " area " + square.area);     // "red" and 100
    square = CreateSquare({ colour: "blue", width: 5 });
    DisplayLine("Square colour " + square.colour + " area " + square.area);     // "blue" and 25

  
    // In typescript, interfaces can also contain function signatures
    interface MatchingFunction
    {
        (number1: number, number2: number): boolean
    }

    // Read through an array of numbers checking them one at a time
    // to see if any pass the test.
    function ContainsNumber(num: number, numbers: number[], checkFunction: MatchingFunction): boolean
    {
        for (var i in numbers)
        {
            if (checkFunction(num, numbers[i]) == true)
            {
                return true;
            }
        }
        return false;
    }

    // Simple test, are the numbers equal
    // Must conform to signature in MatchingFunction interface
    function Test(num1: number, num2: number): boolean
    {
        return num1 == num2;
    }

    var matching: boolean = ContainsNumber(2, [1, 2, 3, 4], Test);
    DisplayLine("Array contain the number? " + matching);

    document.body.innerHTML = output;
});  