import {Component, Inject} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
import {ToastrService} from "ngx-toastr";
import {Router} from "@angular/router";
import {ConfigService} from "@services/config.service";

@Component({
  selector: 'app-convert-image-to-virtual',
  templateUrl: './convert-image-to-virtual.component.html',
  styleUrl: './convert-image-to-virtual.component.css'
})
export class ConvertImageToVirtualComponent {
  baseUrl: string;
  loading: boolean = true;
  extension: string = '';

  constructor(
    private http: HttpClient,
    public dialogRef: MatDialogRef<ConvertImageToVirtualComponent>,
    private toastService: ToastrService,
    private router: Router,
    private configService: ConfigService,
    @Inject(MAT_DIALOG_DATA) public data: { imageImageRepository: any }
  ) {
    this.baseUrl = this.configService.apiUrl;
  }

  ngOnInit(): void {
    this.loading = true;
  }

  save() {
    this.http.post<any>(`${this.baseUrl}${this.data.imageImageRepository['@id']}/convert-image-to-virtual`, {
      extension: this.extension
    }).subscribe({
      next: (response) => {
        this.toastService.success('Peticion de conversion de imagen enviada correctamente');
        this.dialogRef.close();
        this.router.navigate(['/commands-logs']);
      },
      error: error => {
        this.toastService.error(error.error['hydra:description']);
      }
    });
  }

  close() {
    this.dialogRef.close();
  }
}
