﻿// *** Controller Calling Proxy Class
function controllerProxy(controllerURL) {
    var _I = this;
    this.controllerURL = controllerURL;    

    // *** Call a wrapped object
    this.invoke = function (method, data, callback, error, bare) {
        // *** Convert input data into JSON - REQUIRES Json2.js        
        var json = JSON.stringify(data);

        // *** The service endpoint URL        
        var url = _I.controllerURL + method;

        $.ajax({
            url: url,
            data: json,
            type: "POST",
            processData: false,
            contentType: "application/json",
            timeout: 10000,
            dataType: "text",  // not "json" we'll parse
            async: false,
            success:
                    function (res) {
                        if (!callback) return;

                        // *** Use json library so we can fix up MS AJAX dates
                        var result = JSON.parse(res);                        

                        // *** Bare message IS result                        
                        //if (bare)
                        if (true)
                        { callback(result); return; }

                        // *** Wrapped message contains top level object node
                        // *** strip it off                        
                        for (var property in result) {                            
                            callback(result[property]);
                            break;
                        }
                    },
            error: function (xhr) {
                if (!error) return;
                if (xhr.responseText) {
                    var err = JSON.parse(xhr.responseText);
                    if (err)
                        error(err);
                    else
                        error({ Message: "Unknown server error." })
                }
                return;
            }
        });
    }
}

//// *** Service Calling Proxy Class
//function serviceProxy(serviceUrl) {    
//    var _I = this;
//    this.serviceUrl = serviceUrl;

//    // *** Call a wrapped object
//    this.invoke = function(method, data, callback, error, bare) {
//        // *** Convert input data into JSON - REQUIRES Json2.js
//        var json = JSON.stringify(data);

//        // *** The service endpoint URL        
//        var url = _I.serviceUrl + method;

//        $.ajax({
//            url: url,
//            data: json,
//            type: "POST",
//            processData: false,
//            contentType: "application/json",
//            timeout: 10000,
//            dataType: "text",  // not "json" we'll parse
//            success:
//                    function(res) {                        
//                        if (!callback) return;
//                        
//                        // *** Use json library so we can fix up MS AJAX dates
//                        var result = JSON.parse(res);

//                        // *** Bare message IS result
//                        if (bare)
//                        { callback(result); return; }

//                        // *** Wrapped message contains top level object node
//                        // *** strip it off
//                        for (var property in result) {
//                            callback(result[property]);
//                            break;
//                        }
//                    },
//            error: function(xhr) {
//                if (!error) return;
//                if (xhr.responseText) {
//                    var err = JSON.parse(xhr.responseText);
//                    if (err)
//                        error(err);
//                    else
//                        error({ Message: "Unknown server error." })
//                }
//                return;
//            }
//        });
//    }
//}

$.url = function (url) {
    return $('base').attr('href') + url;
}

// *** Create a static instance
//var Proxy = new serviceProxy($.url('RealTime.svc/'));

// *** Create a static instance
var Proxy = new controllerProxy($.url('RealTime/'));

function GetCurrentPrice(ListingID, ListingPriceDIV, LocalPriceDIV, ListingIncrementDIV, LocalIncrementDIV, NextListingPriceDIV, NextLocalPriceDIV) {
    Proxy.invoke("GetCurrentPrice", { listingID: ListingID },
    function (result) {        
        if (result == null) return;
        $(ListingPriceDIV).text(result.DisplayListingPrice);
        $(LocalPriceDIV).text(result.DisplayLocalPrice);
        $(ListingIncrementDIV).text(result.DisplayListingIncrement);
        $(LocalIncrementDIV).text(result.DisplayLocalIncrement);
        $(NextListingPriceDIV).text(result.DisplayNextListingPrice);
        $(NextLocalPriceDIV).text(result.DisplayNextLocalPrice);
    },
    function (error) { window.alert(error); });
}

function GetEndDTTM(ListingID, EndDTTMDIV) {
    Proxy.invoke("GetCurrentEndDTTM", { listingID: ListingID },
    function(result) {
        $(EndDTTMDIV).text(result);        
    },
    function(error) { window.alert(error); });
}

function GetCurrentTime(DivClass) {    
    Proxy.invoke("GetCurrentTime", {},
    function(result) {
        $(DivClass).text(result);               
    },
    function(error) { window.alert(error); });
}

function EmailInvoice(template, invoiceID, recipient) {
    Proxy.invoke("EmailInvoice", { template: template, invoiceID: invoiceID, recipient: recipient },
    function (result) {
        
    },
    function (error) { window.alert(error); });
}
