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

import { Component, EventEmitter, Input, Output, ViewChild } from '@angular/core';
import { UserSettingChangeEvent } from '../models/user-setting-change-event';
import { Widget } from '../models/widget';
import { WidgetSettingsFormComponent } from './widget-settings-form/widget-settings-form.component';

@Component({
  selector: 'bci-widget-settings',
  templateUrl: './widget-settings.component.html',
  styleUrls: ['./widget-settings.component.scss'],
})
export class WidgetSettingsComponent {
  @Input() public settingsChangeDebounceTime = 0;
  @Input() public editMode = false;
  @Output() settingsChange = new EventEmitter<UserSettingChangeEvent>();
  @ViewChild(WidgetSettingsFormComponent) public widgetSettingsFormComponent: WidgetSettingsFormComponent;
  @Output() save = new EventEmitter<Widget>();
  @Output() cancel = new EventEmitter<Widget>();
  @Output() confirm = new EventEmitter<Widget>();

  public settingsOpen = false;

  public openWidgetSettings(widget: Widget): void {
    this.settingsOpen = true;
    setTimeout(() => this.widgetSettingsFormComponent.buildFormsForWidget(widget), 1);
  }

  public closeWidgetSettings(): void {
    this.settingsOpen = false;
  }

  public cancelEdit() {
    if (this.widgetSettingsFormComponent) {
      this.widgetSettingsFormComponent.cancelEdit();
    }
  }
}
