import {Component, Inject} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';

import {APP_CONFIG, AppConfig} from '../../config/app.config';
import {HttpClient} from '@angular/common/http';
import {Router, NavigationEnd} from '@angular/router';
import {IAppConfig} from '../../config/iapp.config';

import {ProgressBarService} from '../progress-bar.service';
import {CartService} from '../cart.service';

@Component({
    selector: 'app-nav',
    templateUrl: './nav.component.html',
    styleUrls: ['./nav.component.scss']
})

export class NavComponent {
    auth: boolean;
    menuItems: any[];
    progressBarMode: string;
    cartItems: number;
    currentUser: any;
    userData: any;
    cartData: any;

    private translateService: TranslateService;

    constructor(@Inject(APP_CONFIG) appConfig: IAppConfig,
                private http: HttpClient,
                private router: Router,
                private cartService: CartService,
                private progressBarService: ProgressBarService,
                translateService: TranslateService) {
        this.loadConfig();
        this.translateService = translateService;
        this.loadMenus();
        this.auth = false;

        this.router.events.filter(event => event instanceof NavigationEnd).subscribe((event: NavigationEnd) => {
            this.auth = event.url !== '/';
        });

        this.currentUser = JSON.parse(sessionStorage.getItem(AppConfig.relativeURL + 'currentUser'));
        this.userData = JSON.parse(sessionStorage.getItem(AppConfig.relativeURL + 'userData'));
        this.cartData = JSON.parse(sessionStorage.getItem(AppConfig.relativeURL + 'cartData'));

        this.cartService.cartCount.subscribe(res => this.cartItems = res);

        this.progressBarService.updateProgressBar$.subscribe((mode: string) => {
            this.progressBarMode = mode;
        });
    }

    loadConfig() {
        this.http.get('./config.json')
            .subscribe((data) => {
                    AppConfig.baseURL = (<any>data).endpointUrl;
                    AppConfig.showPrices = (<any>data).showPrices;
                },
                (error: any) => {
                    console.error(error);
                });
    }

    openHome() {
        this.router.navigate([AppConfig.routes.home]);
    }

    openCart() {
        this.router.navigate([AppConfig.routes.cart]);
    }

    openSpecials() {
        this.router.navigate([AppConfig.routes.specials]);
    }

    openLists() {
        this.router.navigate([AppConfig.routes.lists]);
    }

    openSearch() {
        this.router.navigate([AppConfig.routes.search]);
    }

    openPassword() {
        this.router.navigate([AppConfig.routes.password]);
    }

    openCategories() {
        this.router.navigate([AppConfig.routes.categories]);
    }

    logOut() {
        this.auth = false;
        this.router.navigate([AppConfig.routes.login]);
    }

    private loadMenus(): void {
        this.translateService.get(['login'], {}).subscribe((texts: any) => {
            this.menuItems = [
                {link: '/', name: texts['login']}
            ];
        });
    }
}


