import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import {MatDialog} from '@angular/material/dialog';

import { InstructorData } from '../../../structures/instructor-data';

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

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

import { InstructorService } from '../../../services/instructor.service';
import { UrlService } from '../../../services/url.service';

import * as Globals from '../../../globals';
import { NgForm } from '@angular/forms';

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

  @Input() targetAfterSubmit: string = Globals.pages.myProfile.path;

  @Input() instructorData: InstructorData = emptyInstructorDataFactory();
  model: InstructorData = emptyInstructorDataFactory();

  instructorProfileLink = Globals.pages.instructorProfile;

  maxDate = new Date();

  genders: string[] = Globals.genders;
  gendersPretty: string[] = Globals.gendersPretty;

  @ViewChild('opportunityForm', { static: true }) ngForm: NgForm | undefined;

  constructor(private instructorService: InstructorService, private urlService: UrlService, private router: Router, public dialog: MatDialog) {
  }

  ngOnInit(): void {
    this.model = JSON.parse(JSON.stringify(this.instructorData));
    if(this.targetAfterSubmit == Globals.pages.myProfile.path) {
      this.targetAfterSubmit = '/' + this.targetAfterSubmit;
    } else {
      this.targetAfterSubmit = '/' + this.targetAfterSubmit + '/' + this.instructorData.ID;
    }
  }

  ngAfterViewInit(): void {
    this.ngForm?.form.valueChanges.subscribe(x => {
    })
  }

  saveData(): void {
    this.instructorService.saveInstructorData(this.model);
    this.router.navigate([this.targetAfterSubmit]);//Globals.pages.instructorProfile + '/' + this.instructorData.ID
  }

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

}
