import { Component, OnInit } from '@angular/core';
import {MatDialog} from '@angular/material/dialog';
import {MatSnackBar} from '@angular/material/snack-bar';
import { Router } from '@angular/router';

import { LoggedUserData } from '../../../structures/logged-user-data';
import { InstructorRegisterData } from '../../../structures/instructor-register-data';

import { emptyInstructorRegisterDataFactory } from '../../../empty-objects/empty-instructor-register-data';

import { InstructorPrivacyAgreementDialogComponent } from '../../../instructor/components/instructor-privacy-agreement-dialog/instructor-privacy-agreement-dialog.component';

import { LoginService } from '../../../services/login.service';

//import { DATEPICKER_FORMATS } from '../../global-settings/datepicker-formats';

import * as Globals from '../../../globals';

//import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
//import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';

// import * as _moment from 'moment';
// import {default as _rollupMoment} from 'moment';
// const moment = _rollupMoment || _moment;

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

  model: InstructorRegisterData = emptyInstructorRegisterDataFactory();

  hidePassword: boolean = true;

  //isMock: boolean = false;
  
  maxDate = new Date();
  
  genders: string[] = Globals.genders;
  gendersPretty: string[] = Globals.gendersPretty;

  constructor(private loginService: LoginService, private snackBar: MatSnackBar, private router: Router, public dialog: MatDialog) { 
  }

  ngOnInit(): void {
  }

  register(): void {
    var result = this.loginService.registerInstructor(this.model).subscribe(
        resp => this.afterRegister(resp)
    );
  }

  afterRegister(response: LoggedUserData): void {
    if(response.logged == 'instructor' && response.instructor != null) {
      this.model = emptyInstructorRegisterDataFactory();
      this.snackBar.open('Přihlášení bylo úspěšné', 'x', {duration: Globals.snackbarSuccessTime, panelClass:'snackBarSuccess'});
      this.router.navigate([Globals.pages.mainNotApproved.path]);
    } else {
      this.snackBar.open('Přihlášení bylo neúspěšné', 'x', {duration: Globals.snackbarErrorTime, panelClass:'snackBarError'});
    }
  }

  openPrivacyDialog() {
    const dialogRef = this.dialog.open(InstructorPrivacyAgreementDialogComponent);
  }

}
