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 { SchoolRegisterData } from '../../../structures/school-register-data';

import { emptySchoolRegisterDataFactory } from '../../../empty-objects/empty-school-register-data';

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

import { SchoolPrivacyAgreementDialogComponent } from '../../../school/components/school-privacy-agreement-dialog/school-privacy-agreement-dialog.component';

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

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

  model: SchoolRegisterData = emptySchoolRegisterDataFactory();

  hidePassword: boolean = true;

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

  ngOnInit(): void {
  }

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

  afterRegister(response: LoggedUserData): void {
    if(response.logged == 'school' && response.school != null) {
      this.model = emptySchoolRegisterDataFactory();
      this.snackBar.open('Registrace byla úspěšná', 'x', {duration: Globals.snackbarSuccessTime, panelClass:'snackBarSuccess'});
      this.router.navigate([Globals.pages.mainSchool.path]);
    } else {
      this.snackBar.open('Registrace byla neúspěšná', 'x', {duration: Globals.snackbarErrorTime, panelClass:'snackBarError'});
    }
  }

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

}
