﻿// *** 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 == "") && error) {
                            if (error)
                                error(res);
                            return;
                        }

                        if (!callback) return;

                        setTimeout(function () {
                            try {
                                var jsonParser = (this.JSON ? this.JSON : JSON2);

                                var result = jsonParser.parse(res);

                                if (typeof result == "string") {
                                    result = jsonParser.parse(result);
                                    result = Util.JSONMinimumDeserializer(result);
                                }

                                callback(result);

                            } catch (ex) {
                                PublishStringClientError(ex);
                                if (error)
                                    error(res);
                            }
                        }, 0);

                        return;
                    },
            error:
                function (xhr, textStatus) {
                    try {
                        if (textStatus != "timeout") {
                        }
                        else {
                            if (waiting)
                                waiting.hide();

                            if (xhr.responseText && 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) {
                                    }
                                }
                            }

                            //Deve executar o método erro
                            if (error)
                                error(xhr);

                            return;
                        }
                    }
                    catch (ex) {
                    }
                }
        });
    }
}

