describe('PatientDetailCtrl', function(){
    "use strict";

    var $scope, $rootScope, $controller, $modal, $routeParams, $httpBackend;
    var Flow, patientLoader, Episode, opalTestHelper;
    var patient, controller, metadata, profile;

    beforeEach(function(){
        module('opal');
        module('opal.test');
        patientLoader = jasmine.createSpy();

        inject(function($injector){
            $rootScope   = $injector.get('$rootScope');
            $scope       = $rootScope.$new();
            $controller  = $injector.get('$controller');
            $modal       = $injector.get('$modal');
            $routeParams = $injector.get('$routeParams');
            $httpBackend = $injector.get('$httpBackend');
            Episode      = $injector.get('Episode');
            opalTestHelper = $injector.get('opalTestHelper');
        });

        // $rootScope.fields = fields;
        // patient = {episodes: [new Episode(angular.copy(episodeData))] };
        patient = opalTestHelper.newPatient($rootScope);
        metadata = opalTestHelper.getMetaData();
        profile = opalTestHelper.getUserProfile();

        Flow = {exit: jasmine.createSpy().and.returnValue({
          then: function(fn){ fn(); }
        }) };

        controller = $controller('PatientDetailCtrl', {
            $scope      : $scope,
            $routeParams: $routeParams,
            Flow        : Flow,
            patient     : patient,
            profile     : profile,
            metadata    : metadata,
            patientLoader: patientLoader
        });

    });

    describe('initialization', function(){

        it('should set up state', function(){
            expect($scope.patient).toEqual(patient)
        });

        it('should set up the metadata', function(){
            expect($scope.metadata).toEqual(metadata);
        });

        it('should call switch_to_view', function() {
            spyOn($scope, 'switch_to_view');
            $routeParams.view = 'myview';
            $scope.initialise();
            expect($scope.switch_to_view).toHaveBeenCalledWith('myview');
        });

        it('should set the episode', function() {
            spyOn($scope, 'switch_to_episode');
            $routeParams.view = '123';
            $scope.initialise();
            expect($scope.switch_to_episode).toHaveBeenCalledWith(0);
        });

        it('should hoist the metaddata', function(){
            expect($scope.metadata).toBe(metadata);
        });
    });

    describe('switch_to_episode()', function() {

        it('should switch to the episode', function() {
            var mock_event = {preventDefault: jasmine.createSpy()};
            $scope.switch_to_episode(0, mock_event);
            expect($scope.view).toBe(null);
            expect($scope.episode).toBe(patient.episodes[0]);
        });

    });

    describe('refresh()', function(){
      beforeEach(function(){
        var nameChanged = opalTestHelper.getEpisodeData();
        nameChanged.demographics[0].first_name = "Gerald";
        patient = {
          episodes: [new Episode(nameChanged)],
          demographics: [nameChanged.demographics[0]]
        };

        patientLoader.and.returnValue({
          then: function(fn){
            fn(patient);
          }
        });
      });

      it('should refresh the patient', function(){
        $scope.refresh();
        expect($scope.patient.demographics[0].first_name).toBe("Gerald");
      });

      it('should update the current episode on the scope', function(){
        $scope.refresh();
        expect($scope.episode.demographics[0].first_name).toBe("Gerald");
      });
    });

    describe('switch_to_view()', function() {

        it('should switch', function() {
            $scope.switch_to_view('wat');
            expect($scope.view).toEqual('wat');
        });

    });

    describe('dischargeEpisode()', function() {

        it('should should return null if readonly', function() {
            profile.readonly = true;
            expect($scope.dischargeEpisode()).toBe(null);
        });

        it('should call Flow.', function() {
            $httpBackend.expectGET('/api/v0.1/userprofile/').respond({});
            profile.readonly = false;
            $scope.dischargeEpisode();
            $rootScope.$apply();
        });

    });

});
