(function () {
  'use strict';

  describe('Rentalhouses Route Tests', function () {
    // Initialize global variables
    var $scope,
      mockCity,
      CitiesService,
      RentalhousesService;

    // We can start by loading the main application module
    beforeEach(module(ApplicationConfiguration.applicationModuleName));

    // The injector ignores leading and trailing underscores here (i.e. _$httpBackend_).
    // This allows us to inject a service but then attach it to a variable
    // with the same name as the service.
    beforeEach(inject(function ($rootScope, _RentalhousesService_, _CitiesService_) {
      // Set a new global scope
      $scope = $rootScope.$new();
      CitiesService = _CitiesService_;
      RentalhousesService = _RentalhousesService_;

      mockCity = new CitiesService({
        _id: 'ha-noi',
        name: 'Ha Noi'
      });
    }));

    describe('Route Config', function () {
      describe('Main Route', function () {
        var mainstate;
        beforeEach(inject(function ($state) {
          mainstate = $state.get('rentalhouses');
        }));

        it('Should have the correct URL', function () {
          expect(mainstate.url).toEqual('/nhatro');
        });

        it('Should be abstract', function () {
          expect(mainstate.abstract).toBe(true);
        });

        it('Should have template', function () {
          expect(mainstate.template).toBe('<div class="container"><ui-view/></div>');
        });
      });

      describe('List Route', function () {
        var liststate,
          mockRentalhouse,
          RentalhousesListController;

        beforeEach(inject(function ($controller, $state) {
          liststate = $state.get('rentalhouses.list');

          // create mock Rentalhouse
          mockRentalhouse = new RentalhousesService({
            _id: '525a8422f6d0f87f0e407a33',
            title: 'Tìm nam ở ghép',
            content: 'Tìm nam ở ghép, ngoan ngoãn, chăm chỉ, sạch sẽ!',
            priceFrom: 500,
            priceTo: 2000,
            area: 20,
            location: {
              sector: 'So 47, ngo 165 Duong Quang Ham',
              city: mockCity
            },
            waterPrice: 23,
            electricPrice: 4,
            contact: 'Chi Hoa - 0164 814 3700',
            pictures: ['https://dy4j078ec5vka.cloudfront.net/142609571/medium']
          });

          // Initialize Controller
          RentalhousesListController = $controller('RentalhousesListController as vm', {
            $scope: $scope,
            cityResolve: [mockCity],
            rentalhouseResolve: [mockRentalhouse, mockRentalhouse]
          });
        }));

        it('Should have the correct URL', function () {
          expect(liststate.url).toEqual('/tim-kiem/:city?location&rentType&price');
        });

        it('Should not be abstract', function () {
          expect(liststate.abstract).toBe(undefined);
        });

        it('Should have templateUrl', function () {
          expect(liststate.templateUrl).toBe('modules/rentalhouses/client/views/list-rentalhouses.client.view.html');
        });

        it('should respond to URL', inject(function ($state) {
          expect($state.href(liststate, {
            city: 'ha-noi',
            location: 'Quan Hoa',
            price: '0,500'
          })).toEqual('/nhatro/tim-kiem/ha-noi?location=Quan%20Hoa&price=0%2C500');
        }));

        it('should attach Rentalhouses to the controller scope', function () {
          expect($scope.vm.rentalhouses.length).toEqual(2);
          expect($scope.vm.rentalhouses[0]).toEqual(mockRentalhouse);
          expect($scope.vm.rentalhouses[1]).toEqual(mockRentalhouse);
        });
      });

      describe('View Route', function () {
        var viewstate,
          RentalhousesController,
          mockRentalhouse;

        beforeEach(inject(function ($controller, $state, $templateCache) {
          viewstate = $state.get('rentalhouses.view');
          $templateCache.put('modules/rentalhouses/client/views/view-rentalhouse.client.view.html', '');

          // create mock Rentalhouse
          mockRentalhouse = new RentalhousesService({
            _id: '525a8422f6d0f87f0e407a33',
            title: 'Tìm nam ở ghép',
            content: 'Tìm nam ở ghép, ngoan ngoãn, chăm chỉ, sạch sẽ!',
            priceFrom: 500,
            priceTo: 2000,
            area: 20,
            location: {
              sector: 'So 47, ngo 165 Duong Quang Ham',
              city: mockCity
            },
            waterPrice: 23,
            electricPrice: 4,
            contact: 'Chi Hoa - 0164 814 3700',
            pictures: ['https://dy4j078ec5vka.cloudfront.net/142609571/medium']
          });

          // Initialize Controller
          RentalhousesController = $controller('RentalhousesController as vm', {
            $scope: $scope,
            cityResolve: [mockCity],
            rentalhouseResolve: mockRentalhouse
          });
        }));

        it('Should have the correct URL', function () {
          expect(viewstate.url).toEqual('/:rentalhouseId');
        });

        it('Should have a resolve function', function () {
          expect(typeof viewstate.resolve).toEqual('object');
          expect(typeof viewstate.resolve.rentalhouseResolve).toEqual('function');
        });

        it('should respond to URL', inject(function ($state) {
          expect($state.href(viewstate, {
            rentalhouseId: 1
          })).toEqual('/nhatro/1');
        }));

        it('should attach an Rentalhouse to the controller scope', function () {
          expect($scope.vm.rentalhouse._id).toBe(mockRentalhouse._id);
        });

        it('Should not be abstract', function () {
          expect(viewstate.abstract).toBe(undefined);
        });

        it('Should have templateUrl', function () {
          expect(viewstate.templateUrl).toBe('modules/rentalhouses/client/views/view-rentalhouse.client.view.html');
        });
      });

      describe('Create Route', function () {
        var createstate,
          RentalhousesController,
          mockRentalhouse;

        beforeEach(inject(function ($controller, $state, $templateCache) {
          createstate = $state.get('rentalhouses.create');
          $templateCache.put('modules/rentalhouses/client/views/form-rentalhouse.client.view.html', '');

          // create mock Rentalhouse
          mockRentalhouse = new RentalhousesService();

          // Initialize Controller
          RentalhousesController = $controller('RentalhousesController as vm', {
            $scope: $scope,
            cityResolve: [mockCity],
            rentalhouseResolve: mockRentalhouse
          });
        }));

        it('Should have the correct URL', function () {
          expect(createstate.url).toEqual('/dang-tin');
        });

        it('Should have a resolve function', function () {
          expect(typeof createstate.resolve).toEqual('object');
          expect(typeof createstate.resolve.rentalhouseResolve).toEqual('function');
        });

        it('should respond to URL', inject(function ($state) {
          expect($state.href(createstate)).toEqual('/nhatro/dang-tin');
        }));

        it('should attach an Rentalhouse to the controller scope', function () {
          expect($scope.vm.rentalhouse._id).toBe(mockRentalhouse._id);
          expect($scope.vm.rentalhouse._id).toBe(undefined);
        });

        it('Should not be abstract', function () {
          expect(createstate.abstract).toBe(undefined);
        });

        it('Should have templateUrl', function () {
          expect(createstate.templateUrl).toBe('modules/rentalhouses/client/views/form-rentalhouse.client.view.html');
        });
      });

      describe('Edit Route', function () {
        var editstate,
          RentalhousesController,
          mockRentalhouse;

        beforeEach(inject(function ($controller, $state, $templateCache) {
          editstate = $state.get('rentalhouses.edit');
          $templateCache.put('modules/rentalhouses/client/views/form-rentalhouse.client.view.html', '');

          // create mock Rentalhouse
          mockRentalhouse = new RentalhousesService({
            _id: '525a8422f6d0f87f0e407a33',
            title: 'Tìm nam ở ghép',
            content: 'Tìm nam ở ghép, ngoan ngoãn, chăm chỉ, sạch sẽ!',
            priceFrom: 500,
            priceTo: 2000,
            area: 20,
            location: {
              sector: 'So 47, ngo 165 Duong Quang Ham',
              city: mockCity
            },
            waterPrice: 23,
            electricPrice: 4,
            contact: 'Chi Hoa - 0164 814 3700',
            pictures: ['https://dy4j078ec5vka.cloudfront.net/142609571/medium']
          });

          // Initialize Controller
          RentalhousesController = $controller('RentalhousesController as vm', {
            $scope: $scope,
            cityResolve: [mockCity],
            rentalhouseResolve: mockRentalhouse
          });
        }));

        it('Should have the correct URL', function () {
          expect(editstate.url).toEqual('/:rentalhouseId/chinh-sua');
        });

        it('Should have a resolve function', function () {
          expect(typeof editstate.resolve).toEqual('object');
          expect(typeof editstate.resolve.rentalhouseResolve).toEqual('function');
        });

        it('should respond to URL', inject(function ($state) {
          expect($state.href(editstate, {
            rentalhouseId: 1
          })).toEqual('/nhatro/1/chinh-sua');
        }));

        it('should attach an Rentalhouse to the controller scope', function () {
          expect($scope.vm.rentalhouse._id).toBe(mockRentalhouse._id);
        });

        it('Should not be abstract', function () {
          expect(editstate.abstract).toBe(undefined);
        });

        it('Should have templateUrl', function () {
          expect(editstate.templateUrl).toBe('modules/rentalhouses/client/views/form-rentalhouse.client.view.html');
        });

        xit('Should go to unauthorized route', function () {

        });
      });

    });
  });
}());
