/*
 * Copyright (C) 2018 Robert Bosch GmbH Copyright (C) 2018 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Widget, WidgetContentComponent } from '@bci-web-core/pinboard';

export class MetricsUserSetting {
  public url: string; // TODO: somehow get the value off the pinboardVonfig and store it to this_widget.userSettingsValues
}

export class MetricsSetting {
  // public url: string;
}

@Component({
  selector: 'bci-metrics',
  templateUrl: './metrics.component.html',
  // ,
  // styleUrls: ['./metrics.component.scss']
})
export class MetricsComponent implements OnInit, WidgetContentComponent {
  _widget: Widget;
  _selectedUrl: string;
  metrics = '';

  constructor(private _http: HttpClient) {}

  ngOnInit() {
    this._selectedUrl = this._widget.getUserSettingValue('url');
    this.loadData();
  }

  get widget(): Widget<MetricsComponent> {
    return this._widget;
  }

  set widget(widget: Widget<MetricsComponent>) {
    this._widget = widget;

    this._widget.widgetSaved.subscribe(() => this.userClickedSave());
  }

  // eslint-disable-next-line no-empty-function
  resize(): void {}

  /* eslint-disable-next-line no-empty-function, @typescript-eslint/no-unused-vars */
  update(resizeAfter?: boolean): void {}

  private userClickedSave() {
    this._selectedUrl = this._widget.getUserSettingValue('url');
    this.loadData();
  }

  loadData() {
    this._http
      .get(this._selectedUrl)
      // .pipe(catchError((error: string) => this.metrics = error));
      .subscribe(
        (value: string) => (this.metrics = value),
        (error: string) => (this.metrics = error)
      );
  }
}
