(function(context,$) {

    "use strict";

    context.JK = context.JK || {};
    context.JK.RateSessionDialog = function(app) {
        var logger = context.JK.logger;
        var dialogId = 'rate-session-dialog';
        var $scopeSelector = "[layout-id='rate-session-dialog']";
        var clientId = context.JK.JamServer.clientID;

        function reset() {
            clientId = context.JK.JamServer.clientID;
            $('#btn-rate-session-up', $scopeSelector).removeClass('selected');
            $('#btn-rate-session-down', $scopeSelector).removeClass('selected');
            $('#txt-rate-session-comment',"[layout-id='rate-session-dialog']").val('');
        }

        function showDialog() {
            if (clientId) {
                reset();
                $.ajax({
                    type: "GET",
                    url: "/api/participant_histories/"+clientId
                }).done(function (response) {
                    if (response && 
                        response.hasOwnProperty('should_rate_session') && 
                        true==response['should_rate_session']) {
                        app.layout.showDialog(dialogId);
                    }
                });
                return true;
            } 
            return false;
        }

        function closeDialog() {
            app.layout.closeDialog(dialogId);
        }
        
        function getRating() {
            if ($('#btn-rate-session-down', $scopeSelector).hasClass('selected')) {
                console.log("user chose a bad rating")
                return -1;
            } else if ($('#btn-rate-session-up', $scopeSelector).hasClass('selected')) {
                console.log("user chose a good rating")
                return 1;
            }
            console.log("user chose no rating")
            return 0;
        }

        function getComment() {
            return $('#txt-rate-session-comment',"[layout-id='rate-session-dialog']").val();
        }

        function events() {
            $('#btn-rate-session-cancel', $scopeSelector).click(function(evt) {
                closeDialog();
                return false;
            });
            $('#btn-rate-session-up', $scopeSelector).click(function(evt) {
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected')
                } else {
                    $(this).addClass('selected');
                }
                if ($('#btn-rate-session-down').hasClass('selected')) {
                    $('#btn-rate-session-down').removeClass('selected')
                }
                return false;
            });
            $('#btn-rate-session-down', $scopeSelector).click(function(evt) {
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected')
                } else {
                    $(this).addClass('selected');
                }
                if ($('#btn-rate-session-up').hasClass('selected')) {
                    $('#btn-rate-session-up').removeClass('selected')
                }
                return false;
            });
            $('#btn-rate-session-send', $scopeSelector).click(function(evt) {
                var rr = getRating(), cc = getComment();
                if (0 == rr && 0 == cc.length) {
                    closeDialog();
                    return false;
                }
                if (0 == rr) {
                    context.JK.Banner.showNotice('Please select the thumbs up or thumbs down when leaving a rating.<br/><br/>Thank you!')
                    return false;
                }
                var url = "/api/participant_histories/"+clientId+"/rating";
                // get backend details too
                if(context.jamClient.getAllClientsStateMap) {
                    var backendDetails = context.jamClient.getAllClientsStateMap()
                    console.log("got backend details", backendDetails)
                }

                $.ajax({
                    type: "POST",
                    url: url,
                    data: { rating: getRating(), comment: getComment(), backend_details: backendDetails }
                }).done(function (response) {
                    var qq = getRating();
                    if (0 < qq) {
                        context.JK.GA.trackSessionQuality(context.JK.GA.SessionQualityTypes.good);
                    } else if (0 > qq){
                        context.JK.GA.trackSessionQuality(context.JK.GA.SessionQualityTypes.poor);
                    }
                });
                closeDialog();
                return false;
            });
        }

        function beforeShow(data) {
            // confirm user should see dialog
        }

        function afterShow(data) {
        }

        function initialize() {
            var dialogBindings = {
                'beforeShow' : beforeShow,
                'afterShow' : afterShow
            };
            app.bindDialog(dialogId, dialogBindings);
            events();
        }

        this.initialize = initialize;
        this.showDialog = showDialog;
    };
})(window,jQuery);
