
function extensionPanel(divElement, options) {
    var panel = this;
    this.divElement = divElement;
    this.options = jQuery.extend(true, {}, options);
    var favoriteCall = null;
    this.type = "favorites";
    panel.subscribers = [];
    var xhrMembers = null;
    //To store total records in an extension
    var extensionCount = 0;
    //To store current page number of members
    var currentPageExtension = 1;

    this.getConceptId = function() {
        return this.conceptId;
    }

    this.getDivId = function() {
        return this.divId;
    }

    this.getNextMarkerColor = function(color) {
        //console.log(color);
        var returnColor = 'black';
        if (color == 'black') {
            returnColor = 'green';
        } else if (color == 'green') {
            returnColor = 'purple';
        } else if (color == 'purple') {
            returnColor = 'red';
        } else if (color == 'red') {
            returnColor = 'blue';
        } else if (color == 'blue') {
            returnColor = 'maroon';
        } else if (color == 'maroon') {
            returnColor = 'green';
        }
        //console.log(returnColor);
        globalMarkerColor = returnColor;
        return returnColor;
    }
    panel.markerColor = panel.getNextMarkerColor(globalMarkerColor);

    this.setUpPanel = function (){
        var context = {
            divElementId: panel.divElement.id
        }
        $(divElement).html(JST["views/extensionPlugin/main.hbs"](context));
    }
    panel.setUpPanel();

    this.loadExtensions = function(){

        $.getJSON(options.serverUrl+"/csnoserv/api/explore/extension", function(result) {

        }).done(function(result) {

            var context = {
                divElementId: panel.divElement.id,
                extensions: result
            }
            $("#" + panel.divElement.id + "-panelBody").html(JST["views/extensionPlugin/body.hbs"](context));
            $('#' + panel.divElement.id + '-panelBody').find(".extension-item").click(function (event) {
                //Getting count
                for (let i = 0; i < result.length; i++) {
                    if( result[i].id == $(event.target).attr('data-concept-id')) {
                        extensionCount = parseInt(result[i].count);
                    }
                }
                //Reset page number
                currentPageExtension = 1;
                panel.loadMembers($(event.target).attr('data-concept-id'), $(event.target).attr('data-term'), 1);
                if($(event.target).attr('data-term') != ""){
                    channel.publish(panel.divElement.id, {
                        term: $(event.target).attr('data-term'),
                        module: $(event.target).attr("data-module"),
                        conceptId: $(event.target).attr('data-concept-id'),
                        source: panel.divElement.id
                    });
                }
                else
                {
                    alert("Some data-term error.");
                }
            });

        }).fail(function() {
            $("#" + panel.divElement.id + "-panelBody").html("<div class='alert alert-warning'><span class='i18n' data-i18n-id='i18n_ajax_failed'>No Extension found</span></div>");
        });

    }
    panel.loadExtensions();

    this.loadMembers = function(conceptId, term, paginate){

        var membersUrl =  options.serverUrl+"/csnoserv/api/explore/extension/members?moduleid="+conceptId+"&page="+paginate;

        var total;

        //Loading animation
        $('#' + panel.divElement.id + '-resultsTable').html("<div style='text-align: center;'><span class='text-muted'>Searching..</span><br/><i class='glyphicon glyphicon-refresh icon-spin'></i></div>");

        console.log(membersUrl);
        if (xhrMembers != null) {
            xhrMembers.abort();
            console.log("aborting call...");
        }
        xhrMembers = $.getJSON(membersUrl, function(result){

        }).done(function(result){
            //Total Pages
            var totalPagesExtension = Math.floor(extensionCount / 100);
            if( (extensionCount % 100) > 0 ) {
                totalPagesExtension += 1;
            }

            var context = {
                result: result,
                divElementId: panel.divElement.id,
                term: term,
                count:result.length,
                conceptId: conceptId
            };
            Handlebars.registerHelper('if_eq', function(a, b, opts) {
                if (opts != "undefined") {
                    if(a == b)
                        return opts.fn(this);
                    else
                        return opts.inverse(this);
                }
            });
            Handlebars.registerHelper('if_gr', function(a,b, opts) {
                if (a){
                    if(a > b)
                        return opts.fn(this);
                    else
                        return opts.inverse(this);
                }
            });

            if (result.length != 0){
                $("#" + panel.divElement.id + "-resultsTable").find(".more-row").remove();
                $('#' + panel.divElement.id + "-resultsTable").html(JST["views/extensionPlugin/members.hbs"](context));
                    $('#pagination-for-extension').twbsPagination({
                        startPage: currentPageExtension,
                        hideOnlyOnePage: true,
                        loop: true,
                        totalPages: totalPagesExtension,
                        visiblePages: "3",
                        onPageClick: function (event, page) {
                            currentPageExtension = page;
                        }
                    });
                //On click
                $("#pagination-for-extension").click(function(){
                    panel.loadMembers(conceptId, term, currentPageExtension);
                });
                //End
            }else{
                $("#" + panel.divElement.id + "-resultsTable").html("<tr><td class='text-muted' colspan='2'><span data-i18n-id='i18n_no_members' class='i18n'>This concept has no members</span></td></tr>");
            }
            $('#' + panel.divElement.id + '-resultsTable').find(".member-concept-row").click(function (event) {
                var clickedBadge = $(event.target).closest(".member-concept-row").find(".badge");
                channel.publish(panel.divElement.id, {
                    term: clickedBadge.attr('data-term'),
                    module: clickedBadge.attr("data-module"),
                    conceptId: clickedBadge.attr('data-concept-id'),
                    source: panel.divElement.id
                });
            });
        }).fail(function(){
            $("#" + panel.divElement.id + "-resultsTable").html("<tr><td class='text-muted' colspan='2'><span data-i18n-id='i18n_no_members' class='i18n'>This concept has no members</span></td></tr>");
        });
    }
}
