import ngModule from '../module';

class Controller {
    constructor($scope, $state, $http, $translate, vnApp, aclService, $httpParamSerializer) {
        this.$scope = $scope;
        this.$state = $state;
        this.$http = $http;
        this.$translate = $translate;
        this.vnApp = vnApp;
        this.aclService = aclService;
        this.$httpParamSerializer = $httpParamSerializer;
        this.moreOptions = [
            {callback: this.showPickupOrder, name: 'Show Pickup order'},
            {callback: this.confirmPickupOrder, name: 'Send Pickup order'},
            {callback: this.confirmDeleteClaim, name: 'Delete claim', acl: 'salesAssistant'}
        ];
    }

    onMoreOpen() {
        let options = this.moreOptions.filter(option => {
            const hasAclProperty = Object.hasOwnProperty.call(option, 'acl');

            return !hasAclProperty || (hasAclProperty && this.aclService.hasAny([option.acl]));
        });
        this.$scope.moreButton.data = options;
    }

    onMoreChange(callback) {
        callback.call(this);
    }

    get claim() {
        return this._claim;
    }

    set claim(value) {
        this._claim = value;

        if (!value) return;

        this._quicklinks = {
            btnOne: {
                icon: 'person',
                state: `client.card.summary({id: ${value.clientFk}})`,
                tooltip: 'Client card'
            },
            btnTwo: {
                icon: 'icon-ticket',
                state: `ticket.card.summary({id: ${this.claim.ticketFk}})`,
                tooltip: 'Claimed ticket'
            }
        };
    }

    set quicklinks(value = {}) {
        this._quicklinks = Object.assign(value, this._quicklinks);
    }

    get quicklinks() {
        return this._quicklinks;
    }

    showPickupOrder() {
        const params = {
            clientId: this.claim.clientFk,
            claimId: this.claim.id
        };
        const serializedParams = this.$httpParamSerializer(params);
        let url = `api/report/claim-pickup-order?${serializedParams}`;
        window.open(url);
    }

    confirmPickupOrder() {
        this.$scope.confirmPickupOrder.show();
    }

    sendPickupOrder(response) {
        if (response === 'accept') {
            const params = {
                recipient: this.claim.client.email,
                clientId: this.claim.clientFk,
                claimId: this.claim.id
            };
            const serializedParams = this.$httpParamSerializer(params);
            const url = `email/claim-pickup-order?${serializedParams}`;
            this.$http.get(url).then(
                () => this.vnApp.showMessage(this.$translate.instant('Notification sent!'))
            );
        }
    }

    confirmDeleteClaim() {
        this.$scope.confirmDeleteClaim.show();
    }

    deleteClaim(response) {
        if (response === 'accept') {
            this.$http.delete(`Claims/${this.claim.id}`).then(() => {
                this.vnApp.showSuccess(this.$translate.instant('Claim deleted!'));
                this.$state.go('claim.index');
            });
        }
    }
}

Controller.$inject = ['$scope', '$state', '$http', '$translate', 'vnApp', 'aclService', '$httpParamSerializer'];

ngModule.component('vnClaimDescriptor', {
    template: require('./index.html'),
    controller: Controller,
    bindings: {
        claim: '<',
        tags: '<',
        quicklinks: '<'
    }
});