import { Component, OnInit, Input } from '@angular/core';
import { Router } from '@angular/router';
import { ReplaySubject } from 'rxjs';
import { FormControl } from '@angular/forms';

import { CourseService } from '../../../services/course.service';
import { SchoolService } from '../../../services/school.service';
import { CourseThemeService } from '../../../services/course-theme.service';

import { emptyQuestionnaireDataFactory } from '../../../empty-objects/empty-questionnaire-data';

import { CourseData } from '../../../structures/course-data';
import { QuestionnaireData } from '../../../structures/questionnaire-data';
import { CourseThemeData } from '../../../structures/course-theme-data';

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

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

  @Input() targetAfterSubmit: string = Globals.pages.allSchools.path;
  @Input() courseId: number = 0;

  @Input() questionnaireData: QuestionnaireData | null = emptyQuestionnaireDataFactory();
  model: QuestionnaireData = emptyQuestionnaireDataFactory();

  courseThemes: CourseThemeData[] | null = null;

  questionneireDataSavingStatus: number = Globals.savingStatus.none;

  personalDataAgreement: boolean = false;

  constructor(private courseService: CourseService, private schoolService: SchoolService, private courseThemeService: CourseThemeService, private router: Router) { 
    // nacteni temat kurzu
    this.courseThemeService.courseThemes$.subscribe((themes: CourseThemeData[] | null) => {
      this.courseThemes = themes;
    });
    this.courseThemeService.actualizeCourseThemes();

    // indikator ukladani
    this.courseService.saveStatusReset();
    this.courseService.questionnaireSaveStatus$.subscribe( val => this.questionneireDataSavingStatus = val );
  }

  ngOnInit(): void {
    if(this.questionnaireData != null) {
      this.model = JSON.parse(JSON.stringify(this.questionnaireData));
    }
    this.model.courseId = this.courseId; 
  }

  async saveData(): Promise<void> {
    var courseData = await this.courseService.saveQuestionnaireData(this.model);
    if(courseData != null) {
      this.router.navigate([this.targetAfterSubmit]);
    } else {
      //this.router.navigate([this.targetAfterSubmit]);
    }
  }

}
