import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

import { SchoolService } from '../../../services/school.service';
import { UrlService } from '../../../services/url.service';

import { SchoolData } from '../../../structures/school-data';

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

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

  schoolData: SchoolData | null = null;

  previousUrl: string | undefined = undefined;

  defaultReturnPage = Globals.pages.allSchools;
  possibleReturnPages = [ Globals.pages.allSchools.path ];

  editSchoolLink = Globals.pages.editSchoolDetail.path;

  constructor(private route: ActivatedRoute, private urlService: UrlService, private schoolService: SchoolService) { }

  ngOnInit(): void {
    var idString = this.route.snapshot.paramMap.get('id');
    if(idString != null) {
      var id = +idString;
      if(id != NaN && id != Infinity && id != -Infinity) {
        this.loadSelectedSchool(id);
      }
    }

    this.urlService.saveCheckpoint(this.possibleReturnPages);
  }

  async loadSelectedSchool(id: number): Promise<void> {
    this.schoolData = await this.schoolService.getSchoolById(id);
  }

}
