import { Component, Input, ChangeDetectionStrategy, Output, EventEmitter, TemplateRef } from '@angular/core';
import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';

import { SearchMeta } from '../store/model';
import { Attribute } from 'src/app/metamodel/model';

@Component({
    selector: 'app-datatable',
    templateUrl: 'datatable.component.html',
    styleUrls: ['datatable.component.css'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class DatatableComponent {
    @Input() datasetAttributeList: Attribute[];
    @Input() searchMeta: SearchMeta;
    @Input() searchData: any[];
    @Output() initSearchMeta: EventEmitter<{}> = new EventEmitter();
    @Output() getSearchData: EventEmitter<number> = new EventEmitter();

    bsModalRef: BsModalRef;
    thumbnailData = {
        isOpen: false,
        title: null,
        srcImage: null
    };

    initDatatable() {
        this.initSearchMeta.emit();
        this.getSearchData.emit(1);
    }

    getAttributeRenderer(attributeName: string): string {
        const attribute = this.datasetAttributeList.find(attribute => attribute.name === attributeName);
        return attribute.renderer;
    }

    getAttributeUriAction(attributeName: string, datum: string): string {
        const attribute = this.datasetAttributeList.find(attribute => attribute.name === attributeName);
        return attribute.uri_action;
    }

    openThumbnail(title: string, srcImage: string) {
        this.thumbnailData.title = title;
        this.thumbnailData.srcImage = srcImage;
        this.thumbnailData.isOpen = true;
    }

    closeThumbnail(event: null) {
        this.thumbnailData.isOpen = false;
    }
}