import './index.js';

describe('Ticket Component vnTicketDescriptor', () => {
    let $httpParamSerializer;
    let $httpBackend;
    let controller;
    let $state;

    beforeEach(ngModule('ticket'));

    beforeEach(angular.mock.inject(($componentController, _$httpBackend_, $rootScope, $compile, _$state_, _$httpParamSerializer_) => {
        let $element = $compile(`<vn-autocomplete></vn-autocomplete>`)($rootScope);
        $state = _$state_;
        $state.getCurrentPath = () => {
            return [
                {state: {}},
                {state: {name: 'ticket'}}
            ];
        };
        $httpBackend = _$httpBackend_;
        $httpParamSerializer = _$httpParamSerializer_;
        controller = $componentController('vnTicketDescriptor', {$element});
        controller._ticket = {id: 2, invoiceOut: {id: 1}, client: {id: 101, email: 'client@email'}};
        controller.cardReload = ()=> {
            return true;
        };
    }));

    describe('showAddTurnDialog()', () => {
        it('should call controller.$.addTurn.show()', () => {
            controller.$.addTurn = {show: () => {}};
            spyOn(controller.$.addTurn, 'show');
            controller.showAddTurnDialog();

            expect(controller.$.addTurn.show).toHaveBeenCalledWith();
        });
    });

    describe('addTurn()', () => {
        it('should make a query and call $.addTurn.hide() and vnApp.showSuccess()', () => {
            controller.$.addTurn = {hide: () => {}};
            spyOn(controller.$.addTurn, 'hide');

            $httpBackend.expectPATCH(`TicketWeeklies`).respond();
            controller.addTurn(1);
            $httpBackend.flush();

            expect(controller.$.addTurn.hide).toHaveBeenCalledWith();
        });
    });

    describe('showDeleteTicketDialog()', () => {
        it('should call vnApp.showError() if the ticket isnt editable', () => {
            controller.ticket.tracking = {state: {alertLevel: 3}};
            spyOn(controller.vnApp, 'showError');
            controller.showDeleteTicketDialog();

            expect(controller.vnApp.showError).toHaveBeenCalledWith('This ticket cant be deleted');
        });

        it('should call deleteConfirmation.show() if the ticket is editable', () => {
            controller.ticket.tracking = {state: {alertLevel: 0}};
            controller.$.deleteConfirmation = {show: () => {}};
            spyOn(controller.$.deleteConfirmation, 'show');
            controller.showDeleteTicketDialog();

            expect(controller.$.deleteConfirmation.show).toHaveBeenCalledWith();
        });
    });

    describe('deleteTicket()', () => {
        it('should make a query and call vnApp.showSuccess() if the response is accept', () => {
            spyOn(controller.$state, 'go');
            spyOn(controller.vnApp, 'showSuccess');

            $httpBackend.expectPOST(`Tickets/2/setDeleted`).respond();
            controller.deleteTicket('accept');
            $httpBackend.flush();

            expect(controller.$state.go).toHaveBeenCalledWith('ticket.index');
            expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Ticket deleted');
        });
    });

    describe('showDeliveryNote()', () => {
        it('should open a new window showing a delivery note PDF document', () => {
            const params = {
                clientId: controller.ticket.client.id,
                ticketId: controller.ticket.id
            };
            const serializedParams = $httpParamSerializer(params);
            let expectedPath = `api/report/delivery-note?${serializedParams}`;
            spyOn(window, 'open');
            controller.showDeliveryNote();

            expect(window.open).toHaveBeenCalledWith(expectedPath);
        });
    });

    describe('sendDeliveryNote()', () => {
        it('should make a query and call vnApp.showMessage()', () => {
            spyOn(controller.vnApp, 'showMessage');

            const params = {
                recipient: 'client@email',
                clientId: controller.ticket.client.id,
                ticketId: controller.ticket.id
            };
            const serializedParams = $httpParamSerializer(params);

            $httpBackend.when('GET', `email/delivery-note?${serializedParams}`).respond();
            $httpBackend.expect('GET', `email/delivery-note?${serializedParams}`).respond();
            controller.sendDeliveryNote();
            $httpBackend.flush();

            expect(controller.vnApp.showMessage).toHaveBeenCalledWith('Notification sent!');
        });
    });

    describe('makeInvoice()', () => {
        it('should make a query and call $state.reload() method if the response is accept', () => {
            spyOn(controller.$state, 'reload');
            spyOn(controller.vnApp, 'showSuccess');

            $httpBackend.when('POST', 'Tickets/2/makeInvoice').respond();
            $httpBackend.expect('POST', 'Tickets/2/makeInvoice').respond();
            controller.makeInvoice('accept');
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Ticket invoiced');
            expect(controller.$state.reload).toHaveBeenCalledWith();
        });
    });

    describe('regenerateInvoice()', () => {
        it('should make a query and show a success snackbar if the response is accept', () => {
            spyOn(controller.vnApp, 'showSuccess');

            $httpBackend.when('POST', 'InvoiceOuts/1/regenerate').respond();
            $httpBackend.expect('POST', 'InvoiceOuts/1/regenerate').respond();
            controller.regenerateInvoice('accept');
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Invoice sent for a regeneration, will be available in a few minutes');
        });
    });

    describe('changeShipped()', () => {
        it('should make a query and change the shipped hour if the response is accept', () => {
            controller.ticket.id = 12;
            spyOn(controller.vnApp, 'showSuccess');
            spyOn(controller, 'cardReload');

            $httpBackend.when('POST', 'Tickets/12/updateEditableTicket').respond();
            $httpBackend.expect('POST', 'Tickets/12/updateEditableTicket').respond();
            controller.changeShipped('accept');
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Shipped hour updated');
            expect(controller.cardReload).toHaveBeenCalledWith();
        });
    });


    describe('showAddStowaway()', () => {
        it('should show a dialog with a list of tickets available for an stowaway', () => {
            controller.$.addStowaway = {};
            controller.$.addStowaway.show = jasmine.createSpy('show');
            controller.showAddStowaway();

            expect(controller.$.addStowaway.show).toHaveBeenCalledWith();
        });
    });

    describe('showRemoveStowaway()', () => {
        it('should show a dialog for an stowaway removal', () => {
            controller.$.deleteStowaway = {};
            controller.$.deleteStowaway.show = jasmine.createSpy('show');
            controller.showDeleteStowaway();

            expect(controller.$.deleteStowaway.show).toHaveBeenCalledWith();
        });
    });

    describe('canStowaway()', () => {
        it('should make a query and return if the ticket can be stowawayed', () => {
            controller.ticket.id = 16;
            spyOn(controller, 'isTicketModule').and.callThrough();
            $httpBackend.when('GET', 'Tickets/16/canHaveStowaway').respond(true);
            $httpBackend.expect('GET', 'Tickets/16/canHaveStowaway').respond(true);
            controller.canStowaway();
            $httpBackend.flush();

            expect(controller.canShowStowaway).toBeTruthy();
            expect(controller.isTicketModule).toHaveBeenCalledWith();
        });

        it('should not make a query if is not on the ticket module', () => {
            controller.ticket.id = 16;
            $state.getCurrentPath = () => {
                return [
                    {state: {}},
                    {state: {name: 'client'}}
                ];
            };
            spyOn(controller, 'isTicketModule').and.callThrough();
            controller.canStowaway();

            expect(controller.canShowStowaway).toBeUndefined();
            expect(controller.isTicketModule).toHaveBeenCalledWith();
        });
    });

    describe('recalculateComponents()', () => {
        it('should make a query and show a success snackbar', () => {
            spyOn(controller.vnApp, 'showSuccess');

            $httpBackend.when('POST', 'Tickets/2/recalculateComponents').respond();
            $httpBackend.expect('POST', 'Tickets/2/recalculateComponents').respond();
            controller.recalculateComponents();
            $httpBackend.flush();

            expect(controller.vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
        });
    });
});