import { AppState } from './../../store/app.reducers';
import { Store } from '@ngrx/store';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { Component, OnInit } from '@angular/core';
import { ValidateStrongPassword, ValidateMatchingPasswords } from 'src/app/shared/custom.validators';
import { RequestRegistrationAction } from '../store/auth.actions';
import { AbstractFormComponent } from '../abstract-form.component';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.scss']
})
export class RegisterComponent extends AbstractFormComponent implements OnInit {
  registrationForm: FormGroup;

  constructor(private store: Store<AppState>) {
    super();
  }
  
  ngOnInit() {
    this.registrationForm = new FormGroup({
      'firstName': new FormControl(null, Validators.required),
      'lastName': new FormControl(null, Validators.required),
      'email': new FormControl(null, [Validators.required, Validators.email]),
      'company': new FormControl(null, Validators.required),
      'position': new FormControl(null, Validators.required),
      'password': new FormControl(null, [Validators.required, Validators.minLength(8), ValidateStrongPassword]),
      'confirmPassword': new FormControl(null, [Validators.required]),
    }, {
      validators: ValidateMatchingPasswords
    });
  }
  
  onRegisterSubmit(){
    this.store.dispatch(new RequestRegistrationAction(this.registrationForm));
  }

}
