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

import { Component, OnDestroy } from '@angular/core';
import { UserSettingChangeEvent, Widget, WidgetContentComponent, WidgetFilter } from '@bci-web-core/pinboard';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

export class MyOwnWidgetUserSetting {
  public fancy: boolean;
}

export class MyOwnWidgetSetting {
  public settingWhichCantBeChangedByUser: string;
}

@Component({
  selector: 'bci-test-content',
  templateUrl: './my-own-widget-content.component.html',
  styleUrls: ['./my-own-widget-content.component.scss'],
})
export class MyOwnWidgetContentComponent implements WidgetContentComponent, OnDestroy {
  private readonly ngUnsubscribe = new Subject<void>();
  private _widget: Widget<MyOwnWidgetContentComponent, MyOwnWidgetUserSetting, MyOwnWidgetSetting>;

  ngOnDestroy(): void {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }

  get widget(): Widget<MyOwnWidgetContentComponent, MyOwnWidgetUserSetting, MyOwnWidgetSetting> {
    return this._widget;
  }

  set widget(widget: Widget<MyOwnWidgetContentComponent, MyOwnWidgetUserSetting, MyOwnWidgetSetting>) {
    this._widget = widget;
    this._widget.widgetFiltersChanged
      .pipe(takeUntil(this.ngUnsubscribe))
      .subscribe(() => this.pinboardFiltersChanged());
  }

  private pinboardFiltersChanged(): void {
    const searchTermFilter: WidgetFilter = this._widget.widgetFilters.find(
      (widgetFilter: WidgetFilter) => widgetFilter.key === 'searchTerm'
    );
    /* eslint-disable-next-line @typescript-eslint/no-unused-vars */
    const searchTerm: string = searchTermFilter.value;

    console.log('do something with the filters, e.g. query the recent events');
  }

  // 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 modifySettingsOnChange(settingChanged: UserSettingChangeEvent): void {
    if (settingChanged.cancelled) {
      return;
    }
    // Do some stuff when the user changes a setting
  }

  private userClickedSave(): void {
    console.log('Now the widget can do stuff after the user hit the save button!');
  }
}
