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 { LoginService } from '../../../services/login.service';
import { CourseThemeService } from '../../../services/course-theme.service';

import { emptyCourseDataFactory } from '../../../empty-objects/empty-course-data';

import { CourseData } from '../../../structures/course-data';
import { FreeScopeData } from '../../../structures/free-scope-data';
import { CourseThemeData } from '../../../structures/course-theme-data';

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

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

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

  @Input() courseData: CourseData | null = null;
  model: CourseData = emptyCourseDataFactory();

  courseThemes: CourseThemeData[] | null = null;

  freeScopes: FreeScopeData[] | null = null;

  ownScope: boolean = false;// prepinac mezi vyberem z nabizenych terminu a vlastnimi daty

  personalDataAgreement: boolean = false;

  courseDataSavingStatus: number = Globals.savingStatus.none;

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

    // nacteni volnych terminu
    this.getFreeScopes();

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

  async getFreeScopes(): Promise<void> {
    this.freeScopes = await this.courseService.getFreeScopesFromServer();
  }

  ngOnInit(): void {
    if(this.courseData != null) {
      this.model = JSON.parse(JSON.stringify(this.courseData));
    }
    this.model.state = 'zadost';
    this.addSchoolId();
  }

  async addSchoolId(): Promise<void> {
    var loggedSchool = await this.loginService.getLoggedSchool();
    if(loggedSchool != null) {
      this.model.schoolId = loggedSchool.ID;
    }
  }

  async scopeChanged(event: any): Promise<void> {
    var datesArr = event.value.split('|');
    this.model.dateStart = new Date(datesArr[0]);
    this.model.dateEnd = new Date(datesArr[1]);
  }

  async saveData(): Promise<void> {
    if(this.model.dateStart != null){
      this.model.dateStart = new Date(this.model.dateStart);
      this.model.dateStart.setHours(12);
    }
    if(this.model.dateEnd != null){
      this.model.dateEnd = new Date(this.model.dateEnd);
      this.model.dateEnd.setHours(12);
    }
    var courseData = await this.courseService.enlistCourseData(this.model);
    if(courseData != null) {
      this.router.navigate([this.targetAfterSubmit]);
    } else {
      //this.router.navigate([this.targetAfterSubmit]);
    }
  }

}
