// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import { CommonModule } from "@angular/common";
import { Component } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { ActivatedRoute, Router, RouterModule } from "@angular/router";
import { firstValueFrom } from "rxjs";

import { JslibModule } from "@bitwarden/angular/jslib.module";
import { ActiveSendIcon } from "@bitwarden/assets/svg";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { SendView } from "@bitwarden/common/tools/send/models/view/send.view";
import { SendService } from "@bitwarden/common/tools/send/services/send.service.abstraction";
import { AuthType } from "@bitwarden/common/tools/send/types/auth-type";
import { ButtonModule, SvgModule, ToastService } from "@bitwarden/components";

import { PopOutComponent } from "../../../../platform/popup/components/pop-out.component";
import { PopupFooterComponent } from "../../../../platform/popup/layout/popup-footer.component";
import { PopupHeaderComponent } from "../../../../platform/popup/layout/popup-header.component";
import { PopupPageComponent } from "../../../../platform/popup/layout/popup-page.component";

// FIXME(https://bitwarden.atlassian.net/browse/CL-764): Migrate to OnPush
// eslint-disable-next-line @angular-eslint/prefer-on-push-component-change-detection
@Component({
  selector: "app-send-created",
  templateUrl: "./send-created.component.html",
  imports: [
    ButtonModule,
    CommonModule,
    JslibModule,
    PopOutComponent,
    PopupHeaderComponent,
    PopupPageComponent,
    RouterModule,
    PopupFooterComponent,
    SvgModule,
  ],
})
export class SendCreatedComponent {
  readonly AuthType = AuthType;
  protected sendCreatedIcon = ActiveSendIcon;
  protected send: SendView;
  protected daysAvailable = 0;
  protected hoursAvailable = 0;

  constructor(
    private i18nService: I18nService,
    private platformUtilsService: PlatformUtilsService,
    private sendService: SendService,
    private route: ActivatedRoute,
    private toastService: ToastService,
    private router: Router,
    private environmentService: EnvironmentService,
  ) {
    const sendId = this.route.snapshot.queryParamMap.get("sendId");

    this.sendService.sendViews$.pipe(takeUntilDestroyed()).subscribe((sendViews) => {
      this.send = sendViews.find((s) => s.id === sendId);
      if (this.send) {
        this.hoursAvailable = this.getHoursAvailable(this.send);
        this.daysAvailable = Math.ceil(this.hoursAvailable / 24);
      }
    });
  }

  get formattedExpirationTime(): string {
    if (!this.send?.deletionDate) {
      return "";
    }
    if (this.hoursAvailable < 24) {
      return this.hoursAvailable === 1
        ? this.i18nService.t("oneHour").toLowerCase()
        : this.i18nService.t("durationTimeHours", String(this.hoursAvailable)).toLowerCase();
    }
    return this.daysAvailable === 1
      ? this.i18nService.t("oneDay").toLowerCase()
      : this.i18nService.t("days", String(this.daysAvailable)).toLowerCase();
  }

  getHoursAvailable(send: SendView): number {
    const now = new Date().getTime();
    return Math.max(0, Math.ceil((send.deletionDate.getTime() - now) / (1000 * 60 * 60)));
  }

  async goBack() {
    await this.router.navigate(["/tabs/send"]);
  }

  async copyLink() {
    if (!this.send || !this.send.accessId || !this.send.urlB64Key) {
      return;
    }
    const env = await firstValueFrom(this.environmentService.environment$);
    const link = env.getSendUrl() + this.send.accessId + "/" + this.send.urlB64Key;
    this.platformUtilsService.copyToClipboard(link);
    this.toastService.showToast({
      variant: "success",
      title: null,
      message: this.i18nService.t("sendLinkCopied"),
    });
  }
}
