/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

import { Component, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatTableDataSource } from '@angular/material/table';
import { SpServiceRegistration } from '@streampipes/platform-services';
import { ConfigurationService } from '../../shared/configuration.service';
import { DialogService, PanelType } from '@streampipes/shared-ui';
import { SpExtensionsServiceDetailsDialogComponent } from '../../dialog/extensions-service-details/extensions-service-details-dialog.component';

@Component({
    selector: 'sp-registered-extensions-services',
    templateUrl: './registered-extensions-services.component.html',
    styleUrls: ['./registered-extensions-services.component.scss'],
})
export class SpRegisteredExtensionsServiceComponent {
    displayedColumns: string[] = ['status', 'name', 'group', 'action'];
    @ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;
    dataSource = new MatTableDataSource<SpServiceRegistration>();

    constructor(
        private configurationService: ConfigurationService,
        private dialogService: DialogService,
    ) {
        this.getRegisteredServices();
    }

    getRegisteredServices(): void {
        this.configurationService
            .getRegisteredExtensionsServices()
            .subscribe(res => {
                this.dataSource.data = res;
            });
    }

    openServiceDetails(serviceReg: SpServiceRegistration) {
        this.dialogService.open(SpExtensionsServiceDetailsDialogComponent, {
            panelType: PanelType.STANDARD_PANEL,
            title: 'Service details',
            width: '70vw',
            data: {
                serviceReg,
            },
        });
    }
}
