/**
 * This file is part of Anis Client.
 *
 * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';

import { Instance, Attribute, Dataset } from 'src/app/metamodel/models';
import { Pagination, SearchQueryParams, Criterion, ConeSearch } from 'src/app/instance/store/models';

/**
 * @class
 * @classdesc Search result datatable tab component.
 */
@Component({
    selector: 'app-datatable-tab',
    templateUrl: 'datatable-tab.component.html',
    styleUrls: [ 'datatable-tab.component.scss' ],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class DatatableTabComponent {
    @Input() datasetSelected: string;
    @Input() instance: Instance;
    @Input() datasetList: Dataset[];
    @Input() attributeList: Attribute[];
    @Input() outputList: number[];
    @Input() criteriaList: Criterion[];
    @Input() coneSearch: ConeSearch;
    @Input() queryParams: SearchQueryParams;
    @Input() dataLength: number;
    @Input() sampRegistered: boolean;
    @Input() data: any[];
    @Input() dataIsLoading: boolean;
    @Input() dataIsLoaded: boolean;
    @Input() selectedData: any[];
    @Output() retrieveData: EventEmitter<Pagination> = new EventEmitter();
    @Output() addSelectedData: EventEmitter<number | string> = new EventEmitter();
    @Output() deleteSelectedData: EventEmitter<number | string> = new EventEmitter();
    @Output() broadcast: EventEmitter<string> = new EventEmitter();
    @Output() startTaskCreateResult: EventEmitter<{ format: string, selectedData: boolean, broadcastVo: boolean }> = new EventEmitter();
    @Output() startTaskCreateArchive: EventEmitter<{ selectedData: boolean }> = new EventEmitter();
    @Output() downloadFile: EventEmitter<{url: string, fileId: string, datasetName: string, filename: string}> = new EventEmitter();

    getData() {
        const dataset = this.getDataset();
        const columnRa = this.attributeList.find(a => a.id === dataset.cone_search_column_ra);
        const columnDec = this.attributeList.find(a => a.id === dataset.cone_search_column_dec);
        return this.data.map(d => ({ "x": +d[columnRa.label], "y": +d[columnDec.label] }));
    }

    /**
     * Returns selected dataset for the search.
     *
     * @return Dataset
     */
     getDataset(): Dataset {
        return this.datasetList.find(dataset => dataset.name === this.datasetSelected);
    }
}