﻿// *** Service Calling Proxy Class

function ServiceProxy(serviceUrl) {

    var _I = this;
    var TimeOut = 60000;
    
    this.serviceUrl = serviceUrl;

    // *** Call a wrapped object
    this.invoke = function (method, data, callback, error, waiting, InvokeTimeOutParameter) {

        if (data != null && data.req != null) {
            data.req.UserBrowser = navigator.userAgent;
        }

        // *** Convert input data into JSON - REQUIRES Json2.js
        var json = JSON2.stringifyWcf(data);

        // *** The service endpoint URL        
        var url = _I.serviceUrl + method;

        if (InvokeTimeOutParameter)
            TimeOut = InvokeTimeOutParameter;

        if (waiting)
            waiting.show();

        return $.ajax({
            url: url,
            data: json,
            type: "POST",
            processData: false,
            contentType: "application/json",
            timeout: TimeOut,
            dataType: "text",  // not "json" we'll parse
            success:
                    function (res) {
                        if (waiting)
                            waiting.hide();

                        if (res == null || res == "") {
                            return;
                        }

                        if (!callback) return;

                        setTimeout(function () {
                            try {
                                var result = {}

                                if (this.JSON)
                                    result = JSON.parse(res);
                                else
                                    result = JSON2.parse(res);

                                callback(result);

                            } catch (ex) {
                                PublishStringClientError(ex);
                            }
                        }, 0);

                        return;
                    },
            error:
                function (xhr, textStatus) {
                    try {
                        if (textStatus != "timeout") {
                        }
                        else {
                            if (waiting)
                                waiting.hide();

                            if (!error) return;
                            if (!xhr.responseText) {
                            } else
                                if (xhr.responseText != "") {

                                    var ret = {
                                        IsJSON: false,
                                        JSONResponse: null,
                                        HtmlResponse: null
                                    }

                                    var err;

                                    try {
                                        err = JSON2.parse(xhr.responseText);
                                        ret.IsJSON = true;
                                        ret.JSONResponse = err;
                                    } catch (e) {
                                        err = xhr.responseText;
                                        ret.IsJSON = false;
                                        ret.HtmlResponse = err;
                                    }

                                    if (method != "Log") {
                                        try {
                                            var c = new ServiceProxy(_I.serviceUrl);

                                            setTimeout(
                                            function () {
                                                c.invoke("Log", { req: { Message: xhr.responseText, Module: method, JSONData: JSON2.stringify(data)} }, function () { }, function () { });
                                            }, 0);

                                        } catch (ex) {

                                        }
                                    }
                                }
                                else {
                                }
                            return;
                        }
                    }
                    catch (ex) {
                    }
                }
        });
    }
}

