import {Component, OnInit} from '@angular/core';
import {AuthService} from './classes/servicess/auth.service';
import { Router} from '@angular/router';
import {Dialog} from './classes/base_components/dialog/dialog';
import {ToasterConfig} from 'angular5-toaster/dist';
import {JsonapiCore} from 'ngx-jsonapi';


@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
export class AppComponent {
    message;
    toasterconfig: ToasterConfig;
    test;

    constructor(
        private authService: AuthService,
        private router: Router,
        private dialog: Dialog,
        private jsonapiCore: JsonapiCore,
    ) {

        /***
         * in vase gereftan data az background notification firebase hast
         */
        this.toasterconfig = new ToasterConfig({
            limit: 5,
            showCloseButton: false,
            timeout: 5000
        });
        jsonapiCore.loadingsStart = (): void => {
            // this.loading = 'LOADING...';
        };
        jsonapiCore.loadingsDone = (): void => {
            // this.loading = '';
            jsonapiCore.loadingsOffline = (error): void => {
                dialog.openForAlert('دسترسی به سرور را کنترل نمایید');
            };
            // this.loading = 'No connection!!!';
        };
        jsonapiCore.loadingsError = (errors): void => {
            // this.loading = 'No connection 2!!!';
            if (errors['errors'][0]['status'] === '401') {
                // todo in test shavad
                // this.router.navigateByUrl('/auth/passwordSignin');
                // this.authService.logout();
            } else if (errors['errors'][0]['status'] === '403') {
                dialog.openForAlert('شما دسترسی ندارید.');
            } else {
                dialog.openForAlert('دسترسی به سرور را کنترل نمایید');
            }
        };
    }



    // prop() {
    //   let toast: Toast = {
    //     requestType: 'info',
    //     title: 'salam',
    //     body: ToasterButtonComponent,
    //     bodyOutputType: BodyOutputType.Component,
    //
    //   };
    //   this.toastService.pop(toast);
    // }
}

