import { Component, inject, input, OnInit  } from '@angular/core';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';

import { AlertService } from 'app/core/util/alert.service';
import SharedModule from 'app/shared/shared.module';
import { DurationPipe, FormatMediumDatetimePipe, FormatMediumDatePipe } from 'app/shared/date';
import { DataUtils } from 'app/core/util/data-util.service';
import { IDocument, NewDocument } from '../document.model';
import { DocumentService } from '../service/document.service';
import { AbstractResilientEntityComponent } from 'app/entities/resilient/resilient-entity.component';

@Component({
  standalone: true,
  selector: 'jhi-document-detail',
  templateUrl: './document-detail.component.html',
  imports: [
    SharedModule, 
    RouterModule, 
    DurationPipe, 
    FormatMediumDatetimePipe, 
    FormatMediumDatePipe
  ],
})
export class DocumentDetailComponent extends AbstractResilientEntityComponent<IDocument, NewDocument, DocumentService> implements OnInit {
  document = input<IDocument | null>(null);
  
  protected dataUtils = inject(DataUtils);
  protected documentService = inject(DocumentService);
  protected alertService = inject(AlertService);
  protected router = inject(Router);
  protected route = inject(ActivatedRoute);
  
  constructor(service: DocumentService) { super(service); }
  
  ngOnInit(): void {
    // Checks for messages in navigation state and shows the alert
    const navState = window.history.state;
    if (navState && navState.message) {
      this.alertService.addAlert({
        type: 'success',
        message: navState.message,
      });
    }
  }

  byteSize(base64String: string): string {
    return this.dataUtils.byteSize(base64String);
  }

  openFile(base64String: string, contentType: string | null | undefined): void {
    this.dataUtils.openFile(base64String, contentType);
  }

  previousState(): void {
    window.history.back();
  }
}
