import { SelectData } from '../../core/model/select-data.model';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { Component, OnInit, Inject } from '@angular/core';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { Attacker } from '../model/attacker.model';

@Component({
  selector: 'app-attacker-edit',
  templateUrl: './attacker-edit.component.html',
  styleUrls: ['./attacker-edit.component.scss']
})
export class AttackerEditComponent implements OnInit {

  public attackerForm: FormGroup = this.initAttackerForm();
  public editMode: boolean = false;
  public genders: SelectData[] = [];
  public localities: SelectData[] = [];

  constructor(@Inject(MAT_DIALOG_DATA) private data: any) {
    this.genders = data.genders;
    this.localities = data.localities;
    if (this.data.attacker){
      this.attackerForm = this.initAttackerForm(this.data.attacker);
      this.editMode = true;
    }
  }

  ngOnInit() {
  }

  private initAttackerForm(attacker?: Attacker): FormGroup{
    let formControls = {
      'id': new FormControl(attacker ? attacker.id : null),
      'name': new FormControl(attacker ? attacker.name : null, [Validators.required, Validators.maxLength(100)]),
      'gender': new FormControl(attacker ? attacker.gender: null),
      'age': new FormControl(attacker ? attacker.age : null),
      'attackLocality': new FormControl(attacker ? attacker.attackLocality : null),
      'residence': new FormControl(attacker ? attacker.residence : null, Validators.maxLength(60)),
      'origin': new FormControl(attacker ? attacker.origin: null, Validators.maxLength(500))
    };
    return new FormGroup(formControls);
  }
}
