(function(context, $) {

    describe("RecordingModel", function() {
        var recordingModel = null;
        var sessionModel = null;
        var app = null;
        var rest = null;
        var jamClient = null;
        var validRecordingData = null;
        beforeEach(function() {
            app = {  };
            sessionModel = { id: null };
            rest = { startRecording: null, stopRecording: null};
            jamClient = { StartRecording: null, StopRecording: null};
            recordingModel = new context.JK.RecordingModel(app, sessionModel, rest, jamClient);
            validRecordingData = {
                id: '1',
                recorded_tracks: [
                    { id: '1', track_id: '1', user_id: '1', 'client_id':'1' }
                ]
            }
        });

        it("constructs", function() {

        });

        it("allows start recording", function() {

            spyOn(sessionModel, 'id').andReturn('1');
            spyOn(rest, 'startRecording').andCallFake(function (req) {
                return $.Deferred().resolve(validRecordingData).promise();
            });
            spyOn(jamClient, 'StartRecording').andCallFake(function(recordingId, tracks) {
                eval(context.JK.HandleRecordingStartResult).call(this, recordingId, true);
            });
            spyOnEvent($(recordingModel), 'startingRecording');
            spyOnEvent($(recordingModel), 'startedRecording');

            expect(recordingModel.startRecording()).toBe(true);

            expect('startingRecording').toHaveBeenTriggeredOn($(recordingModel));
            expect('startedRecording').toHaveBeenTriggeredOn($(recordingModel));
        });

        it("allows stop recording", function() {

            spyOn(sessionModel, 'id').andReturn('1');
            spyOn(rest, 'startRecording').andCallFake(function (req) {
                return $.Deferred().resolve(validRecordingData).promise();
            });
            spyOn(rest, 'stopRecording').andCallFake(function (req) {
                return $.Deferred().resolve(validRecordingData).promise();
            });
            spyOn(jamClient, 'StartRecording').andCallFake(function(recordingId, tracks) {
                eval(context.JK.HandleRecordingStartResult).call(this, recordingId, true);
            });
            spyOn(jamClient, 'StopRecording').andCallFake(function(recordingId, tracks) {
                eval(context.JK.HandleRecordingStopResult).call(this, recordingId, true);
            });

            spyOnEvent($(recordingModel), 'stoppingRecording');
            spyOnEvent($(recordingModel), 'stoppedRecording');


            expect(recordingModel.startRecording()).toBe(true);
            expect(recordingModel.stopRecording()).toBe(true);

            expect('stoppingRecording').toHaveBeenTriggeredOn($(recordingModel));
            expect('stoppedRecording').toHaveBeenTriggeredOn($(recordingModel));
        });

    });

}(window, jQuery));