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 { emptyCourseDataFactory } from '../../../empty-objects/empty-course-data';

import { CourseData } from '../../../structures/course-data';
import { SchoolData } from '../../../structures/school-data';
import { CourseThemeData } from '../../../structures/course-theme-data';

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

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

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

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

  courseThemes: CourseThemeData[] | null = null;

  courseDataSavingStatus: number = Globals.savingStatus.none;

  schoolSearchText: string = "";
  allSchools: SchoolData[] | null = null;
  filteredSchools: ReplaySubject<SchoolData[]> = new ReplaySubject<SchoolData[]>(1);

  constructor(private courseService: CourseService, private schoolService: SchoolService, private courseThemeService: CourseThemeService, private router: Router) { 

    // nacteni skol
    this.schoolService.allSchools$.subscribe((schools: SchoolData[] | null) => {
      if(this.allSchools == null && schools != null) {
        this.filteredSchools.next(schools);
      }
      this.allSchools = schools;
    });
    this.schoolService.actualizeAllSchools();

    // nacteni temat kurzu
    this.courseThemeService.courseThemes$.subscribe((themes: CourseThemeData[] | null) => {
      this.courseThemes = themes;
    });
    this.courseThemeService.actualizeCourseThemes();

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

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

  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.saveCourseData(this.model);
    if(courseData != null) {
      this.router.navigate([Globals.pages.courseDetail.path + '/' + courseData.ID]);
    } else {
      //this.router.navigate([this.targetAfterSubmit]);
    }
  }

  filterSchools(): void {
    if(this.allSchools != null) {
      this.filteredSchools.next(this.allSchools.filter(school => school.name.toLowerCase().indexOf(this.schoolSearchText.toLowerCase()) > -1));
    }
  }

}
