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

import { PageInfo } from '../../structures/page-info';

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

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

@Component({
  selector: 'app-return-link-button',
  templateUrl: './return-link-button.component.html',
  styleUrls: ['./return-link-button.component.scss']
})
export class ReturnLinkButtonComponent implements OnInit {

  @Input() infoText: string = '';
  @Input() defaultPage: PageInfo | undefined = undefined;
  @Input() possiblePages: string[] | undefined = undefined;
  
  targetPage: PageInfo | undefined = undefined;
  targetLink: string | undefined = undefined;

  constructor(private urlService: UrlService) { }

  ngOnInit(): void {
    this.targetLink = this.urlService.getCheckpointUrl();
    if(this.possiblePages != undefined) {
      if(this.targetLink != undefined) {
        if(this.possiblePages.find(page => { return '/' + page == this.targetLink; }) != undefined) {
          this.targetPage = Globals.getPageInfoByUrl(this.targetLink);
          return;
        }
      }
    }

    // nejsou definovany mozne stranky pro navrat
    if (this.defaultPage != undefined) {
      this.targetPage = this.defaultPage;
      this.targetLink = this.defaultPage.path;
      return;
    }
    
    this.targetPage = Globals.pages.notFound;
  }

}
