import { Component, OnInit } from '@angular/core';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import {
    IconButtonTypes,
    IConfirmationDialogData,
    IqserPermissionsService,
    ListingComponent,
    listingProvidersFactory,
    LoadingService,
    TableColumnConfig,
    Toaster,
} from '@iqser/common-ui';
import { getCurrentUser } from '@iqser/common-ui/lib/users';
import { getParam } from '@iqser/common-ui/lib/utils';
import { DOSSIER_TEMPLATE_ID, User, Watermark } from '@red/domain';
import { WatermarkService } from '@services/entity-services/watermark.service';
import { WatermarksMapService } from '@services/entity-services/watermarks-map.service';
import { Roles } from '@users/roles';
import { AdminDialogService } from '../../../services/admin-dialog.service';

@Component({
    templateUrl: './watermarks-listing-screen.component.html',
    styleUrls: ['./watermarks-listing-screen.component.scss'],
    providers: listingProvidersFactory(WatermarksListingScreenComponent),
})
export class WatermarksListingScreenComponent extends ListingComponent<Watermark> implements OnInit {
    readonly #dossierTemplateId = getParam(DOSSIER_TEMPLATE_ID);
    readonly iconButtonTypes = IconButtonTypes;
    readonly currentUser = getCurrentUser<User>();
    readonly roles = Roles;
    readonly tableColumnConfigs: TableColumnConfig<Watermark>[] = [
        { label: _('watermarks-listing.table-col-names.name'), sortByKey: 'searchKey', width: '2fr' },
        { label: _('watermarks-listing.table-col-names.status'), sortByKey: 'enabled', class: 'flex-center' },
        { label: _('watermarks-listing.table-col-names.created-by'), class: 'user-column' },
        { label: _('watermarks-listing.table-col-names.created-on'), sortByKey: 'dateAdded' },
        { label: _('watermarks-listing.table-col-names.modified-on'), sortByKey: 'dateModified' },
    ];
    readonly tableHeaderLabel = _('watermarks-listing.table-header.title');

    constructor(
        private readonly _loadingService: LoadingService,
        private readonly _watermarkService: WatermarkService,
        private readonly _watermarksMapService: WatermarksMapService,
        private readonly _dialogService: AdminDialogService,
        private readonly _toaster: Toaster,
        readonly permissionsService: IqserPermissionsService,
    ) {
        super();
        this.entitiesService.setEntities(this._watermarksMapService.get(this.#dossierTemplateId));
    }

    ngOnInit() {
        this._loadingService.stop();
    }

    async openConfirmDeleteWatermarkDialog(watermark: Watermark) {
        const isUsed = await this._watermarkService.isWatermarkUsed(watermark.id);

        const data: IConfirmationDialogData = {
            question: isUsed ? _('watermarks-listing.watermark-is-used') : null,
        };

        this._dialogService.openDialog('confirm', data, async () => {
            await this._deleteWatermark(watermark);
        });
    }

    async toggleStatus(watermark: Watermark): Promise<void> {
        this._loadingService.start();
        const updatedWatermark = await this._watermarkService.saveWatermark({ ...watermark, enabled: !watermark.enabled });
        this.entitiesService.replace(updatedWatermark);
        this._loadingService.stop();
    }

    getRouterLink(watermark: Watermark = null): string {
        return `/main/admin/dossier-templates/${this.#dossierTemplateId}/watermarks/${watermark ? watermark.id : 'create'}`;
    }

    private async _deleteWatermark(watermark: Watermark): Promise<void> {
        this._loadingService.start();
        await this._watermarkService.deleteWatermark(this.#dossierTemplateId, watermark.id);
        this.entitiesService.setEntities(this._watermarksMapService.get(this.#dossierTemplateId));
        this._toaster.success(_('watermarks-listing.action.delete-success'));
        this._loadingService.stop();
    }
}
