import { Component, Inject, OnInit } 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-import-image',
  templateUrl: './import-image.component.html',
  styleUrl: './import-image.component.css'
})
export class ImportImageComponent implements OnInit {
  baseUrl: string;
  loading: boolean = true;
  imageName: string = '';

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

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

  save() {
    console.log(this.data?.repositoryUuid)
    this.http.post<any>(`${this.baseUrl}/image-repositories/${this.data?.repositoryUuid}/import-image`, {
      name: this.imageName
    }).subscribe({
      next: (response) => {
        this.toastService.success('Peticion de importacion 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();
  }
}
