import { Component } from '@angular/core';
import { ConfigService } from '@services/config.service';
import { TranslateService } from '@ngx-translate/core';
import { ButtonConfig, IconButtonTypes, IqserPermissionsService } from '@iqser/common-ui';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { RouterHistoryService } from '@services/router-history.service';
import { LicenseService } from '@services/license.service';
import { Roles } from '@users/roles';
import type { User } from '@red/domain';
import { getCurrentUser } from '@iqser/common-ui/lib/users';

@Component({
    templateUrl: './license-screen.component.html',
    styleUrls: ['./license-screen.component.scss'],
})
export class LicenseScreenComponent {
    readonly roles = Roles;
    readonly currentUser = getCurrentUser<User>();
    readonly currentYear = new Date().getFullYear();
    readonly buttonConfigs: readonly ButtonConfig[] = [
        {
            label: _('license-info-screen.email-report'),
            action: (): void => this.sendMail(),
            type: IconButtonTypes.primary,
            helpModeKey: 'license_information',
            hide: !this.permissionsService.has(Roles.license.readReport),
        },
    ];

    constructor(
        readonly configService: ConfigService,
        readonly licenseService: LicenseService,
        readonly permissionsService: IqserPermissionsService,
        readonly routerHistoryService: RouterHistoryService,
        private readonly _translateService: TranslateService,
    ) {}

    sendMail(): void {
        const licenseCustomer = this.licenseService.selectedLicense.licensedTo;
        const subject = this._translateService.instant('license-info-screen.email.title', {
            licenseCustomer,
        }) as string;
        const lineBreak = '%0D%0A';
        const body = [
            this._translateService.instant('license-info-screen.email.body.analyzed', {
                pages: this.licenseService.selectedLicenseReport.numberOfAnalyzedPages,
            }),
            this._translateService.instant('license-info-screen.email.body.licensed', {
                pages: this.licenseService.totalLicensedNumberOfPages,
            }),
        ].join(lineBreak);
        const mail = this.licenseService.selectedLicense.licensedToEmail;
        window.location.href = `mailto:${mail}?subject=${subject}&body=${body}`;
    }
}
