import { Component, OnInit } from '@angular/core';
import { NavigationEnd, Router } from '@angular/router';
import { RouterExtensions } from '@nativescript/angular';
import { transitionOptions } from '~/constants/shared.constants';
import { ApplicationSettings as AppSettings } from '@nativescript/core';

import { USER_UNIT_ID } from '~/constants/login-constants';
import { MaterialAllowedFunctionTypes } from '~/pages/material/utils/allowedMaterialFunctionTypes';

@Component({
    selector: 'MyUnitPage',
    moduleId: module.id,
    templateUrl: './my-unit-page.component.html',
    styleUrls: ['./_my-unit-page.component.scss']
})
export class MyUnitPageComponent implements OnInit {
    pageLoading: boolean;
    transition: { name: string; duration: number; curve: string; };
    hasMaterialAllowedFunctionType: boolean;
    constructor(
        private routerExtensions: RouterExtensions,
        private router: Router,
    ) {
        this.transition = transitionOptions;
        this.router.events.subscribe((val) => {
            if (val instanceof NavigationEnd) {
                if (this.router.url === '/home/myUnit') {
                    this.pageLoading = false;
                }
            }
        });
    }
    onUnitNavItemTap(): void {
        this.routerExtensions.navigate(['/unit', AppSettings.getNumber(USER_UNIT_ID)], {
            transition: {
                name: 'fade'
            }
        });
    }
    ngOnInit(): void {
        this.pageLoading = false;
        this.hasMaterialAllowedFunctionType = MaterialAllowedFunctionTypes.getPermission();

    }
    onNavTap(navRoute: string): void {
        this.pageLoading = true;
        setTimeout(() => {
            this.routerExtensions.navigate([navRoute], { transition: transitionOptions });
        }, 10);
    }

}
