import { Component, Input, ChangeDetectionStrategy } from '@angular/core';
import { ToastrService } from 'ngx-toastr';

import { Criterion } from '../../store/model';

@Component({
    selector: 'app-url-display',
    templateUrl: 'url-display.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class UrlDisplayComponent {
    @Input() apiPath: string;
    @Input() instanceName: string;
    @Input() datasetName: string;
    @Input() criteriaList: Criterion[];
    @Input() outputList: number[];

    constructor(private toastr: ToastrService) { }

    getUrl() {
        let query = this.apiPath + '/search/' + this.datasetName + '?a=' + this.outputList.join(';');
        if (this.criteriaList.length > 0) {
            query += '&c=' + this.criteriaList.map(criterion => criterion.getCriterionStr()).join(';');
        }
        return query;
    }

    copyToClipboard(): void {
        const selBox = document.createElement('textarea');
        selBox.value = this.getUrl();
        document.body.appendChild(selBox);
        selBox.select();
        document.execCommand('copy');
        document.body.removeChild(selBox);
        this.toastr.success('Copied');
    }
}