import { Component } from '@angular/core';
import { AngularMaterialModule } from '../../angular-material/angular-material.module';
import {
  FormControl,
  Validators,
  FormsModule,
  ReactiveFormsModule,
  FormGroup,
} from '@angular/forms';
import { Router, RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
import { AuthService } from '../authService/auth.service';
import { HttpClientModule } from '@angular/common/http';
import { LoadingSpinnerComponent } from '../../shared/loading-spinner/loading-spinner.component';
import { NotifierService } from '../../shared/notifierService/notifier.service';
import { ScoreTableComponent } from '../../score-table/score-table/score-table.component';
import { Firestore } from '@angular/fire/firestore';
import { HeaderLoginSignupResetpw } from '../../shared/header-login-signup-resetpw/header-lsr.component';
import { MatDialog } from '@angular/material/dialog';
import { pipe, switchMap } from 'rxjs';
import { User } from '../model/user.model';
import { PendingActionsService } from '../../score/pending-actions.service';
import { CustomDialogComponent } from '../../shared/custom-dialog/custom-dialog.component';
import { UserService } from '../../user-profile/user.service';

@Component({
  selector: 'app-signup',
  standalone: true,
  imports: [
    CommonModule,
    AngularMaterialModule,
    FormsModule,
    ReactiveFormsModule,
    RouterModule,
    HttpClientModule,
    LoadingSpinnerComponent,
    ScoreTableComponent,
    HeaderLoginSignupResetpw,
  ],
  templateUrl: './signup.component.html',
  styleUrl: './signup.component.css',
})
export class SignupComponent {
  /** Represents the login form group */
  signUpForm!: FormGroup;
  isLoading = false;

  constructor(private authService: AuthService, private dialog: MatDialog) {}

  ngOnInit() {
    // Initialize the login form with form controls and validators
    this.signUpForm = new FormGroup({
      email: new FormControl('', [
        Validators.required,
        Validators.email,
        Validators.pattern(
          /^[a-zA-Z0-9._%+-]+\.+[a-zA-Z0-9._%+-]+@(eviden\.com|atos\.net)$/
        ),
      ]),
    });
  }

  onSubmit() {
    const emailBig = this.signUpForm.get('email')?.value;
    const email = emailBig.toLowerCase();
    this.authService.signUp(email);
    this.dialog.open(CustomDialogComponent, {
      data: {
        title: 'E-Mail-Regristrierung',
        bodyText:
          'Eine Regristrierungs-Email wurde an deine Adresse gesendet. Bitte überprüfe dein Postfach und klicke auf den Link, um dich zu registrieren.',
      },
    });
    this.signUpForm.reset();
  }
}
