import {AfterViewInit, Component, Input, OnInit} from '@angular/core';
import {Configuration} from 'src/app/models/Page';
import {ActivatedRoute, Router} from "@angular/router";
import {UserService} from "../../services/user.service";


@Component({
    selector: 'app-page',
    templateUrl: './page.component.html',
    styleUrls: ['./page.component.css']
})
export class PageComponent implements OnInit, AfterViewInit {

    @Input() configuration?: Configuration;

    event: any;

    constructor(
        private route: ActivatedRoute,
        private router: Router,
        public userService: UserService
    ) {}

    loaded = false;

    ngOnInit(): void {
        if (this.userService?.community)
            this.registerParams("communityid", this.userService?.community?.communityid);

        this.loaded = false;
    }

    ngAfterViewInit(): void {
        setTimeout(() => {
            this.loaded = true;
        }, 500);
    }

    registerParams(paramName: string, value?: string) {
        let queryParams: any = {};
        queryParams[paramName] = value;

        if (this.configuration)
            this.configuration.selectedCommunity = this.userService?.community?.communityid;

        this.router.navigate(['.'], {
            relativeTo: this.route,
            queryParams: queryParams,
            queryParamsHandling: 'merge',
            skipLocationChange: false
        });
    }
}
