﻿AG.ClientAPI = {
    data: [],
    queue: [],
    isRunning: false,
    interval: false,
    getDataAsync: function (url, async, func) {
        var that = this;
        if (typeof (that.data[url]) == 'undefined') {
            $.ajax({
                url: url,
                type: 'GET',
                dataType: 'json',
                ifModified: true,
                cache: true,
                async: async,
                success: function (data) {
                    if (typeof (func) == 'function')
                        func(data);
                    that.data[url] = data;
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
        else {
            func(that.data[url]);
        }
    },
    getDirectData: function (url, async, func) {
        var that = this;
        if (typeof (that.data[url]) == 'undefined') {
            $.ajax({
                url: url,
                type: 'GET',
                dataType: 'json',
                ifModified: true,
                cache: true,
                async: async,
                success: function (data) {
                    that.data[url] = data;

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    return false;
                }
            })
        }
        return that.data[url];
    },
    run: function () {
        if (!this.interval) {
            this.interval = setInterval(this.intervalFunction, 10);
        }
    },
    intervalFunction: function () {
        if (!AG.ClientAPI.isRunning) {
            AG.ClientAPI.isRunning = true;
            for (var i = 0; i < AG.ClientAPI.queue.length; i++) {
                if ((AG.ClientAPI.queue[i].isLoop || !AG.ClientAPI.queue[i].isCalled) && AG.ClientAPI.queue[i].condition.call()) {
                    AG.ClientAPI.queue[i].isCalled = true;
                    if (typeof (AG.ClientAPI.queue[i].context) == 'function')
                        AG.ClientAPI.queue[i].callback.call(this, AG.ClientAPI.queue[i].context.call());
                    else
                        AG.ClientAPI.queue[i].callback.call(this, AG.ClientAPI.queue[i].context);
                }
            }
            var i = 0;
            while (i < AG.ClientAPI.queue.length) {
                if (AG.ClientAPI.queue[i].isCalled && !AG.ClientAPI.queue[i].isLoop) {
                    AG.ClientAPI.queue.splice(i, 1);
                }
                else {
                    i++;
                }
            }
            if (AG.ClientAPI.queue.length == 0) {
                clearInterval(AG.ClientAPI.interval);
                AG.ClientAPI.interval = false;
            }
            AG.ClientAPI.isRunning = false;
        }
    },
    add: function (callback, condition, context, isLoop) {
        this.queue.push({
            callback: callback,
            condition: condition,
            context: context,
            isLoop: isLoop ? true : false,
            isCalled: false
        });
        if (!this.interval) {
            this.run();
        }
    },
    findData: function (data, index, key) {
        var position = 0;
        try {
            if (typeof (index[data[key]]) != 'undefined')
                position = index[data[key]].position;
            else
                position = -1;
        }
        catch (Error) {
            position = -1;
        }
        return position;
    },
    checkRequest: function (obj, requestName) {
        var that = this;
        if (typeof (obj.pkConditions[requestName]) != 'undefined') {
            if (typeof (obj.data[requestName]) == 'undefined')
                return true;
            else
                return false;
        }
        else
            return false;
    },
    getCondition: function (data, getFields, orders, filters, joinTables, version, language) {
        var strConditions = "";
        strConditions += "<Conditions>";
        if (typeof language == 'undefined') {
            strConditions += '<Language>' + agLanguageName + '</Language>';
        }
        strConditions += "<Data>";
        if (data != null && typeof (data) != "undefined" && data.length > 0) {
            for (var j = 0; j < data.length; j++) {
                for (key in data[j]) {
                    if (typeof (data[j][key]) != "undefined" && data[j][key] != null) {
                        if (key != "length" && typeof (data[j][key].prototype) == "undefined") {
                            strConditions += "<" + key + ">";
                            strConditions += data[j][key];
                            strConditions += "</" + key + ">";
                        }
                    }
                }
            }
            if (typeof (version) != 'undefined' && version != null) {
                strConditions += '<Modified>' + version + '</Modified>';
            }
        }
        strConditions += "</Data>";
        if (joinTables != null && typeof (joinTables) != "undefined" && joinTables.length > 0) {
            strConditions += "<JoinTables>";
            for (var j = 0; j < joinTables.length; j++) {
                strConditions += "<Table>";
                if (typeof (joinTables[j].Name) != 'undefined')
                    strConditions += '<Name>' + joinTables[j].Name + '</Name>';
                if (typeof (joinTables[j].Alias) != 'undefined')
                    strConditions += '<Alias>' + joinTables[j].Alias + '</Alias>';
                if (typeof (joinTables[j].Join) != 'undefined')
                    strConditions += '<Join>' + joinTables[j].Join + '</Join>';
                if (typeof (joinTables[j].JoinColumn) != 'undefined')
                    strConditions += '<JoinColumn>' + joinTables[j].JoinColumn + '</JoinColumn>';
                if (typeof (joinTables[j].MainColumn) != 'undefined')
                    strConditions += '<MainColumn>' + joinTables[j].MainColumn + '</MainColumn>';
                if (joinTables[j].data != null && typeof (joinTables[j].data) != "undefined" && joinTables[j].data.length > 0) {
                    strConditions += "<Data>";
                    for (var i = 0; i < joinTables[j].data.length; i++) {
                        for (key in joinTables[j].data[i]) {
                            if (typeof (joinTables[j].data[i][key]) != "undefined" && joinTables[j].data[i][key] != null) {
                                if (key != "length" && typeof (joinTables[j].data[i][key].prototype) == "undefined") {
                                    strConditions += "<" + key + ">";
                                    strConditions += joinTables[j].data[i][key];
                                    strConditions += "</" + key + ">";
                                }
                            }
                        }
                    }
                    strConditions += "</Data>";
                }
                if (joinTables[j].fields != null && typeof (joinTables[j].fields) != "undefined") {
                    strConditions += "<Fields>";
                    if (joinTables[j].fields.static != null && typeof (joinTables[j].fields.static) != "undefined") {
                        for (var k = 0; k < joinTables[j].fields.static.length; k++) {
                            strConditions += "<Static>";
                            strConditions += joinTables[j].fields.static[k];
                            strConditions += "</Static>";
                        }
                    }
                    if (joinTables[j].fields.dynamic != null && typeof (joinTables[j].fields.dynamic) != "undefined") {
                        for (var k = 0; k < joinTables[j].fields.dynamic.length; k++) {
                            strConditions += "<Dynamic>";
                            strConditions += joinTables[j].fields.dynamic[k];
                            strConditions += "</Dynamic>";
                        }
                    }
                    if (joinTables[j].fields.fieldCategory != null && typeof (joinTables[j].fields.fieldCategory) != "undefined") {
                        for (var k = 0; k < joinTables[j].fields.fieldCategory.length; k++) {
                            strConditions += "<FieldCategory>";
                            strConditions += joinTables[j].fields.fieldCategory[k];
                            strConditions += "</FieldCategory>";
                        }
                    }
                    if (joinTables[j].fields.field != null && typeof (joinTables[j].fields.field) != "undefined") {
                        for (var k = 0; k < joinTables[j].fields.field.length; k++) {
                            strConditions += "<Field>";
                            if (joinTables[j].fields.field[k].Id != null && typeof (joinTables[j].fields.field[k].Id) != "undefined") {
                                strConditions += "<Id>";
                                strConditions += joinTables[j].fields.field[k].Id;
                                strConditions += "</Id>";
                            }
                            if (joinTables[j].fields.field[k].Name != null && typeof (joinTables[j].fields.field[k].Name) != "undefined") {
                                strConditions += "<Name>";
                                strConditions += joinTables[j].fields.field[k].Name;
                                strConditions += "</Name>";
                            }
                            if (joinTables[j].fields.field[k].DataType != null && typeof (joinTables[j].fields.field[k].DataType) != "undefined") {
                                strConditions += "<DataType>";
                                strConditions += joinTables[j].fields.field[k].DataType;
                                strConditions += "</DataType>";
                            }
                            strConditions += "</Field>";
                        }
                    }
                    strConditions += "</Fields>";
                }
                strConditions += "</Table>";
            }
            strConditions += "</JoinTables>";
        }
        if (filters != null && typeof (filters) != "undefined" && filters.length > 0) {
            strConditions += "<Filters>";
            for (var j = 0; j < filters.length; j++) {
                strConditions += "<Filter>";
                for (key in filters[j]) {
                    if (typeof (filters[j][key]) != "undefined" && filters[j][key] != null) {
                        if (key != "length" && typeof (filters[j][key].prototype) == "undefined") {
                            strConditions += "<" + key + "><![CDATA[";
                            strConditions += filters[j][key];
                            strConditions += "]]></" + key + ">";
                        }
                    }
                }
                strConditions += "</Filter>";
            }
            strConditions += "</Filters>";
        }
        if (getFields != null && typeof (getFields) != "undefined") {
            strConditions += "<Fields>";
            if (getFields.static != null && typeof (getFields.static) != "undefined") {
                for (var j = 0; j < getFields.static.length; j++) {
                    strConditions += "<Static>";
                    strConditions += getFields.static[j];
                    strConditions += "</Static>";
                }
            }
            if (getFields.dynamic != null && typeof (getFields.dynamic) != "undefined") {
                for (var j = 0; j < getFields.dynamic.length; j++) {
                    strConditions += "<Dynamic>";
                    strConditions += getFields.dynamic[j];
                    strConditions += "</Dynamic>";
                }
            }
            if (getFields.fieldCategory != null && typeof (getFields.fieldCategory) != "undefined") {
                for (var j = 0; j < getFields.fieldCategory.length; j++) {
                    strConditions += "<FieldCategory>";
                    strConditions += getFields.fieldCategory[j];
                    strConditions += "</FieldCategory>";
                }
            }
            if (getFields.field != null && typeof (getFields.field) != "undefined") {
                for (var j = 0; j < getFields.field.length; j++) {
                    strConditions += "<Field>";
                    if (getFields.field[j].Id != null && typeof (getFields.field[j].Id) != "undefined") {
                        strConditions += "<Id>";
                        strConditions += getFields.field[j].Id;
                        strConditions += "</Id>";
                    }
                    if (getFields.field[j].Name != null && typeof (getFields.field[j].Name) != "undefined") {
                        strConditions += "<Name>";
                        strConditions += getFields.field[j].Name;
                        strConditions += "</Name>";
                    }
                    if (getFields.field[j].DataType != null && typeof (getFields.field[j].DataType) != "undefined") {
                        strConditions += "<DataType>";
                        strConditions += getFields.field[j].DataType;
                        strConditions += "</DataType>";
                    }
                    strConditions += "</Field>";
                }
            }
            strConditions += "</Fields>";
        }
        if (orders != null && typeof (orders) != "undefined") {
            strConditions += "<Orders>";
            for (var j = 0; j < orders.length; j++) {
                strConditions += "<Order>";
                if (orders[j].Field != null && typeof (orders[j].Field) != "undefined") {
                    strConditions += "<Field>";
                    strConditions += orders[j].Field;
                    strConditions += "</Field>";
                }
                if (orders[j].Direction != null && typeof (orders[j].Direction) != "undefined") {
                    strConditions += "<Direction>";
                    strConditions += orders[j].Direction;
                    strConditions += "</Direction>";
                }
                strConditions += "</Order>";
            }
            strConditions += "</Orders>";
        }
        strConditions += "</Conditions>";
        return window.encodeURIComponent ? encodeURIComponent(strConditions) : escape(strConditions);
    },
    getConditions: function (obj, requestName) {
        var that = this;
        var strConditions = "";
        var condition = obj.conditions[obj.pkConditions[requestName].position];
        var version = null;
        if (typeof (obj.version) != 'undefined') {
            if (typeof (obj.version[requestName]) != 'undefined') {
                version = obj.version[requestName];
            }
        }
        if (requestName.indexOf('NoLanguage') > -1) {
            strConditions = that.getCondition(condition.data, condition.getFields, condition.orders, condition.filters, condition.joinTables, version,false);
        }
        else strConditions = that.getCondition(condition.data, condition.getFields, condition.orders, condition.filters, condition.joinTables, version);

        return strConditions;
    },
    createData: function (obj, requestName, data, getFields, orders, filters, joinTables, type, loop) {
        var that = this;
        var position = 0;
        var objResult;
        switch (type) {
            //function goi va duoc thuc hien ngay                                                                                                          
            case 0:
                that.addCondition(obj, requestName, data, getFields, orders, filters, joinTables, loop);
                if (loop > 0 && typeof (obj.version[requestName]) == 'undefined') {
                    obj.interval = setInterval('AG.ClientAPI.createData(' + obj.name + ',"' + requestName + '",[],[],{},[],null,1,' + loop + ')', loop);
                    setTimeout('window.location.reload()', 1200000);
                    obj.loadData(requestName);
                }
                if (typeof (loop) == 'undefined')
                    obj.loadData(requestName);
                objResult = that.getData(obj, requestName);
                break;
            case 1:
                obj.loadData(requestName);
                objResult = that.getData(obj, requestName);
                break;
            //goi lai function                                                                                                                     
            case 2:
                objResult = that.getData(requestName);
                break;
            default:
                that.addCondition(obj, requestName, data, getFields, orders, filters, joinTables, loop);
                obj.loadData(requestName);
                objResult = that.getData(obj, requestName);
                break;
        }
        return objResult;
    },
    addCondition: function (obj, requestName, data, getFields, orders, filters, joinTables, loop) {
        var that = this;
        var isLoop = loop ? loop : 0;
        var position = 0;
        if (typeof (obj.pkConditions[requestName]) == 'undefined') {
            position = obj.conditions.push({ data: data, getFields: getFields, orders: orders, requestName: requestName, filters: filters, joinTables: joinTables });
            obj.pkConditions[requestName] = {};
            obj.pkConditions[requestName].position = position - 1;
            obj.pkConditions[requestName].isLoop = isLoop;
            obj.pkConditions[requestName].isRun = false;
        }
    },
    getData: function (obj, requestName) {
        if (typeof (obj.data[requestName]) != 'undefined') {
            setTimeout('AG.ClientAPI.deleteData(' + obj.name + ',"' + requestName + '")', 30000000);
            return obj.data[requestName];
        }
        else
            return [];
    },
    deleteData: function (obj, requestName) {
        try {
            obj.data[requestName] = null;
            obj.conditions[obj.pkConditions[requestName]] = null;
            obj.pkConditions[requestName] = null;
        }
        catch (Error) {

        }
    }
}
AG.Config = {
    DataType: {
        ReportGroup: '00000000-0000-0000-1000-000000000002'
    },
    CompanyType: {
        Listed: '00000000-0000-0000-0000-000000000002',
        Fund: '00000000-0000-0000-0000-000000000005'
    },
    PutThroughTradeType: {
        PutAD: '00000000-0000-0000-0000-000000000001',
        PutExec: '00000000-0000-0000-0000-000000000002',
        PutDC: '00000000-0000-0000-0000-000000000003'
    },
    ShareholderType: {
        Shareholder: '00000000-0000-0000-0000-000000000001',
        MajorShareholder: '00000000-0000-0000-0000-000000000002'
    },
    BondBidingType: {
        Schedule: '00000000-0000-0000-0000-000000000001',
        Result: '00000000-0000-0000-0000-000000000002'
    },
    TradingResultType: {
        Outright: '2F3EFE0B-3ECA-48E5-900D-C1959B2A1ED7',
        Repos: '94082105-5040-46EF-94EA-311B030695FB'
    },
    Empty: '00000000-0000-0000-0000-000000000000'
}
