(function(context,$) {

    describe("FindSession", function() {

        var fss;

        var appFake = {
            clientId: '12345',
            bindScreen: function(){},
            notify: function(){},
            ajaxError: function() { console.debug("ajaxError"); }
        };

        var jamClientFake = {
            TestLatency: function(clientID, fnName) {
                var js = fnName + '(' + JSON.stringify({clientID: clientID, latency: 50}) + ');';
                eval(js);
            }
        };

        beforeEach(function() {
            fss = null;
            // Use the actual screen markup
            JKTestUtils.loadFixtures('/app/views/clients/_findSession.html.erb');
            spyOn(appFake, 'notify');
        });

        var sessionLatencyReal;

        describe("RealSessionLatency", function() {
            beforeEach(function() {
                sessionLatencyReal = new JK.SessionLatency(jamClientFake);
                spyOn(sessionLatencyReal, 'sessionPings').andCallThrough();
                fss = new context.JK.FindSessionScreen(appFake);
                fss.initialize(sessionLatencyReal);
                fss.clearResults();
            });

            describe("loadSessions", function() {
                beforeEach(function() {
                    spyOn($, "ajax");
                });
                it("should query ajax for sessions", function() {
                    fss.afterShow({});
                    expect($.ajax).toHaveBeenCalled();
                });
            });

            /*describe("afterShow flow", function() {
                beforeEach(function() {
                    spyOn($, "ajax").andCallFake(function(opts) {
                        opts.success(TestGetSessionResponses.oneOfEach);
                    });
                });

                it("should output table rows for sessions", function() {
                    fss.afterShow({});
                    expect($(fss.getCategoryEnum().INVITATION.id + ' tr').length).toEqual(5);
                });

                it("should call sessionPings", function() {
                    fss.afterShow({});
                    expect(sessionLatencyReal.sessionPings).toHaveBeenCalled();
                });

            });*/
        });

        describe("FakeSessionLatency", function() {

            beforeEach(function() {
                sessionInfoResponses = {
                    "1": {id:"1", sortScore: 3},
                    "2": {id:"2", sortScore: 2}
                };
                sessionLatencyFake = {
                    sessionInfo: null
                };
                spyOn(sessionLatencyFake, 'sessionInfo').andCallFake(function(id) {
                    return sessionInfoResponses[id];
                });
                fss = new context.JK.FindSessionScreen(appFake);
                fss.initialize(sessionLatencyFake);
                fss.clearResults();
            });

            /*describe("renderSession", function() {

                describe("layout", function() {
                    var tbody;

                    beforeEach(function() {
                        var session = TestGetSessionResponses.oneOfEach[0];
                        alert(JSON.parse(session));
                        fss.setSession(session);
                        fss.renderSession(session.id);
                        tbody = $(fss.getCategoryEnum().INVITATION.id);
                    });

                    it("single session should render", function() {
                        expect($('tr', tbody).length).toEqual(1);
                    });

                    it("Should render genre", function() {
                        expect($('tr td', tbody).first().text()).toEqual('classical');
                    });

                    it("Should render description", function() {
                        expect($('tr td', tbody).first().next().text()).toEqual('Invited');
                    });

                    it("Should render musician count", function() {
                        expect($('tr td', tbody).first().next().next().text()).toEqual('1');
                    });

                    // TODO - test audience
                    // TODO - test latency
                    // TODO - test Listen

                    // it("Should render join link", function() {
                    //     expect($('tr td', tbody).last().text()).toEqual('JOIN');
                    // });

                });

                it("higher sortScore inserted before lower sortScore", function() {
                    var sessionLow = TestGetSessionResponses.oneOfEach[1];
                    var sessionHigh = TestGetSessionResponses.oneOfEach[0];
                    fss.setSession(sessionLow);
                    fss.setSession(sessionHigh);
                    fss.renderSession(sessionLow.id);
                    fss.renderSession(sessionHigh.id);

                    var tbody = $(fss.getCategoryEnum().INVITATION.id);
                    expect($('tr', tbody).length).toEqual(2);
                    expect($('tr', tbody).first().attr('data-sortScore')).toEqual('3');
                    expect($('tr', tbody).first().next().attr('data-sortScore')).toEqual('2');
                });
            });*/
        });

    });

 })(window,jQuery);