import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { RouterExtensions } from '@nativescript/angular';
import { FeedbackType } from 'nativescript-feedback';
import { transitionOptions } from '~/constants/shared.constants';
import { AdvertisingAllowedFunctionTypes } from '~/pages/advertising/utils/allowedAdvertisingFunctionTypes';
import { ApplicationAllowedFunctionTypes } from '~/pages/application/utils/allowedApplicationFunctionTypes';
import { feedbackMessage } from '~/pages/material/utils/feedback-messages';

@Component({
    selector: 'UnitLanding',
    moduleId: module.id,
    templateUrl: './unit-landing.component.html',
    styleUrls: ['./_unit-landing.component.scss']
})
export class UnitLandingComponent implements OnInit {

    unitId: number;
    allowedApplicationAccess: boolean = false;
    allowedAdvertisingAccess: boolean = false;

    exportTemplates = [
        {name: 'Instuktoři', id: 24},
        {name: 'Seznam kontaktů včetně rodičů', id: 60}
    ];

    constructor(
        protected route: ActivatedRoute,
        private routerExtensions: RouterExtensions
    ) {
    }

    ngOnInit(): void {
        this.unitId = +this.route.snapshot.params['unitId'];

        this.allowedApplicationAccess = ApplicationAllowedFunctionTypes.getPermission();
        this.allowedAdvertisingAccess = AdvertisingAllowedFunctionTypes.getPermission();
    }

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

    onNavTapUnit(path: string) {
        setTimeout(() => {
            this.routerExtensions.navigate([path, this.unitId], {transition: transitionOptions});
        }, 10);
    }

    onNavTapApplication() {
        if (!this.allowedApplicationAccess) {
            feedbackMessage(FeedbackType.Error, 'E-přihláška', `Vaše role nemá oprávnění k přístupu na tuto stránku`);

            return;
        }
        setTimeout(() => {
            this.routerExtensions.navigate(['/application'], {transition: transitionOptions});
        }, 10);
    }

    onNavTapAdvertising() {
        if (!this.allowedAdvertisingAccess) {
            feedbackMessage(FeedbackType.Error, 'Nábory', `Vaše role nemá oprávnění k přístupu na tuto stránku`);

            return;
        }
        setTimeout(() => {
            this.routerExtensions.navigate(['/advertising', true], {transition: transitionOptions});
        }, 10);
    }

    onExportTap(id: number) {
        setTimeout(() => {
            this.routerExtensions.navigate(['/export/newExport', id], {transition: transitionOptions});
        }, 10);
    }

    get infoIcon() {
        return String.fromCharCode(0xf05a);
    }

    get membersIcon() {
        return String.fromCharCode(0xf0c0);
    }

    get registrationIcon() {
        return String.fromCharCode(0xf080);
    }

    get advertisingIcon() {
        return String.fromCharCode(0xf500);
    }

    get applicationIcon() {
        return String.fromCharCode(0xf1d8);
    }

    get exportIcon() {
        return String.fromCharCode(0xf56e);
    }
}
