import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { HitAWodService } from 'src/app/services/hit-a-wod.service';
import { BoxSetting } from 'src/app/models/box-setting.model';

@Component({
  selector: 'app-update-box-settings-dialog',
  templateUrl: './update-box-settings-dialog.component.html',
  styleUrls: ['./update-box-settings-dialog.component.css']
})
export class UpdateBoxSettingsDialogComponent implements OnInit {

  @Input() box_id:number;
  @Output() close:EventEmitter<any> = new EventEmitter();

  settings:BoxSetting[];

  constructor(private api:HitAWodService) { }

  ngOnInit(): void {
    if(this.box_id) {
      this.fetchSettings();
    }
  }

  save() {
    this.api.updateBoxSettings(this.box_id.toString(), this.settings).subscribe(
      data => {
        this.close.emit(data);
      }
    );
  }

  restaurar() {
    this.settings.forEach(s => {
      s.pivot.value = s.default_value;
    });
  }

  private fetchSettings() {
    this.api.getBoxSettings(this.box_id.toString()).subscribe(
      data => {
        this.settings = data.data;
      }
    );
  }

}
