import {Component, OnInit} from '@angular/core';
import {RestService} from '../../services/rest.service';
import {NGXLogger} from 'ngx-logger';
import {ImageMetadata, StorageHealth} from '../../interfaces/interface';

@Component({
  selector: 'app-landing',
  templateUrl: './landing.component.html',
  styleUrls: ['./landing.component.css']
})
export class LandingComponent implements OnInit {

  storageHealth: StorageHealth;
  images: ImageMetadata[] = [];

  constructor(
    public logger: NGXLogger,
    private restService: RestService
  ) {
  }

  ngOnInit(): void {
    this.restService.healthCheck().toPromise().then(
      storageHealth => this.storageHealth = storageHealth
    ).catch(err => this.logger.error(err));

    this.loadImages();
  }

  loadImages(): void {
    this.restService.getAllImages().toPromise().then(images =>
      this.images = images.sort((im1, im2) => {
        if (im1.name > im2.name) {
          return 1;
        }

        if (im1.name < im2.name) {
          return -1;
        }

        return 0;
        })
    ).catch(err => this.logger.error(err));
  }


}
