import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { AuthService } from '../services/auth.service';

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

  public form: FormGroup;
  public errorMessage: string;
  public successMessage: string;

  public data: any = {
    email: '',
    password: '',
  };

  constructor(private authService: AuthService) {
    this.form = new FormGroup({
      password: new FormControl(this.data.password, [Validators.required, Validators.minLength(3)]),
      email: new FormControl(this.data.email, [
        Validators.required,
        Validators.email,
      ]),
    });
  }

  public submitForm(): void {
    this.form.markAllAsTouched();
    if (this.form.valid){
      this.tryRegister({email: this.form.value.email, password: this.form.value.password});
    }
  }

  public clearForm(): void {
    this.form.reset();
  }

  tryRegister(value){
    /*this.authService.doRegister(value)
    .then(res => {
      console.log(res);
      this.errorMessage = '';
      this.successMessage = 'Your account has been created';
      console.log(this.successMessage);
    }, err => {
      console.log(err);
      this.errorMessage = err.message;
      this.successMessage = '';
      console.log(err.code + ' - ' + this.errorMessage);
    });*/
  }

  ngOnInit(): void {
  }

}
