import { Component, OnInit, Input } from '@angular/core';
import {
    ScanningResultService,
    ScanningDetailResult
} from '../service/index';
import { ErrorHandler } from '../error-handler/index';

import { toPromise } from '../utils';
import { GRID_COMPONENT_HTML } from './scanning.html';
import { SCANNING_STYLES } from './scanning.css';

@Component({
    selector: 'hbr-scan-result-grid',
    styles: [SCANNING_STYLES],
    template: GRID_COMPONENT_HTML
})
export class ResultGridComponent implements OnInit {
    scanningResults: ScanningDetailResult[] = [];
    @Input() tagId: string;

    constructor(
        private scanningService: ScanningResultService,
        private errorHandler: ErrorHandler
    ) { }

    ngOnInit(): void {
        this.loadResults(this.tagId);
    }

    showDetail(result: ScanningDetailResult): void {
        console.log(result.id);
    }

    loadResults(tagId: string): void {
        toPromise<ScanningDetailResult[]>(this.scanningService.getScanningResults(tagId))
            .then((results: ScanningDetailResult[]) => {
                this.scanningResults = results;
            })
            .catch(error => { this.errorHandler.error(error) })
    }
}
