import { Component, inject, input, OnInit  } from '@angular/core';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';
import { Observable, finalize, filter, tap, mergeMap, EMPTY, of } from 'rxjs';
import { HttpResponse } from '@angular/common/http';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';

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 { IInputDataUpload, NewInputDataUpload } from '../input-data-upload.model';
import { InputDataUploadService } from '../service/input-data-upload.service';
import { InputDataUploadLogLogsDialogComponent } from 'app/entities/input-data-upload-log/logs/input-data-upload-log-logs-dialog.component';
import { IActivityProgress } from 'app/resilient/resilient-activity/resilient-activity.model';
import { ResilientActivityService } from 'app/resilient/resilient-activity/service/resilient-activity.service';
import { UploadStatus } from 'app/entities/enumerations/upload-status.model';
import { ResilientActivityActionsButtonComponent } from 'app/resilient/resilient-activity/actions-button/resilient-activity-actions-button.component';
import { ResilientActivityProgressComponent } from 'app/resilient/resilient-activity/progress/resilient-activity-progress.component';
import { AbstractResilientEntityComponent } from 'app/entities/resilient/resilient-entity.component';

@Component({
  standalone: true,
  selector: 'jhi-input-data-upload-detail',
  templateUrl: './input-data-upload-detail.component.html',
  imports: [
    SharedModule, 
    RouterModule, 
    DurationPipe, 
    FormatMediumDatetimePipe, 
    FormatMediumDatePipe, 
    ResilientActivityActionsButtonComponent,
    ResilientActivityProgressComponent
  ],
})
export class InputDataUploadDetailComponent extends AbstractResilientEntityComponent<IInputDataUpload, NewInputDataUpload, InputDataUploadService> implements OnInit {
  inputDataUpload = input<IInputDataUpload | null>(null);
  
  protected dataUtils = inject(DataUtils);
  protected inputDataUploadService = inject(InputDataUploadService);
  protected modalService = inject(NgbModal);
  protected alertService = inject(AlertService);
  protected router = inject(Router);
  protected route = inject(ActivatedRoute);
  protected resilientActivityService = inject(ResilientActivityService);
  
  public UploadStatus = UploadStatus;

  constructor(service: InputDataUploadService) { 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();
  }
  
  handleActivityInvoked(message: string): void {
    this.reloadPage();
  }
  
  showLogs(): void {
    const modalRef = this.modalService.open(InputDataUploadLogLogsDialogComponent, { size: 'lg', backdrop: 'static' });
    modalRef.componentInstance.inputDataUpload = this.inputDataUpload();
  }
  
  /* Refresh the current record. Probably something was invoked in the server and the record mus be updated */
  reloadPage(): void {
    // Get the current entity ID from the route
    const entityId = this.route.snapshot.params['id'];

    // Navigate to the same detail page with the same ID
    const currentUrl: string = this.router.url;
    this.router.navigateByUrl('/', { skipLocationChange: true }).then(() => {
      this.router.navigate(['/input-data-upload', entityId, 'view']);
    });
  }
}
