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

@Component({
  selector: 'page-unavailable',
  templateUrl: './unavailable.page.html',
  styleUrls: ['./unavailable.page.scss']
})
export class PageUnavailable implements OnInit {

  targetRoute = '';

  constructor(
    private location: Location,
    private router: Router,
    private route: ActivatedRoute) { }

  ngOnInit(): void {
    this.route.queryParams.subscribe({
      next: (params) => {
        if (params['target']) {
          this.targetRoute = params['target'];
          this.router.navigate([], { queryParams: { target: null }, queryParamsHandling: 'merge', skipLocationChange: true });
        }
      }
    });

  }

  retry() {
    if (!this.targetRoute || this.targetRoute === "unavailable" || this.targetRoute === "/unavailable") {
      this.location.back;
    } else {
      this.router.navigate([this.targetRoute]);
    }
  }

}
