/**
 * This file is part of Anis Client.
 *
 * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

import { Component, OnInit, OnDestroy } from '@angular/core';

import { Observable, Subscription } from 'rxjs';
import { Store } from '@ngrx/store';

import { UserProfile } from 'src/app/auth/user-profile.model';
import { Instance } from 'src/app/metamodel/models';
import * as authActions from 'src/app/auth/auth.actions';
import * as authSelector from 'src/app/auth/auth.selector';
import * as instanceSelector from 'src/app/metamodel/selectors/instance.selector';
import { AppConfigService } from 'src/app/app-config.service';

/**
 * @class
 * @classdesc Portal home container.
 *
 * @implements OnInit
 * @implements OnDestroy
 */
@Component({
    selector: 'app-portal-home',
    templateUrl: 'portal-home.component.html'
})
export class PortalHomeComponent implements OnInit, OnDestroy {
    public favIcon: HTMLLinkElement = document.querySelector('#favicon');
    public links = [{ label: 'Home', icon: 'fas fa-home', routerLink: '/portal' }];
    public isAuthenticated: Observable<boolean>;
    public userProfile: Observable<UserProfile>;
    public userRoles: Observable<string[]>;
    public instanceList: Observable<Instance[]>;
    public userRolesSubscription: Subscription;

    constructor(private store: Store<{ }>, private config: AppConfigService) {
        this.isAuthenticated = store.select(authSelector.selectIsAuthenticated);
        this.userProfile = store.select(authSelector.selectUserProfile);
        this.userRoles = store.select(authSelector.selectUserRoles);
        this.instanceList = store.select(instanceSelector.selectAllInstances);
    }

    ngOnInit() {
        this.favIcon.href = 'favicon.ico';
        const adminLink = { label: 'Admin', icon: 'fas fa-tools', routerLink: '/admin' };
        if (!this.config.authenticationEnabled) {
            this.links.push(adminLink);
        } else {
            this.userRolesSubscription = this.userRoles.subscribe(userRoles => {
                if (userRoles.includes(this.config.adminRole)) {
                    this.links.push(adminLink);
                }
            });
        }
    }

    /**
     * Returns application base href.
     *
     * @return string
     */
    getBaseHref(): string {
        return this.config.baseHref;
    }

    /**
     * Checks if authentication is enabled.
     *
     * @return boolean
     */
    getAuthenticationEnabled(): boolean {
        return this.config.authenticationEnabled;
    }

    /**
     * Dispatches action to log in.
     */
    login(): void {
        this.store.dispatch(authActions.login());
    }

    /**
     * Dispatches action to log out.
     */
    logout(): void {
        this.store.dispatch(authActions.logout());
    }

    /**
     * Dispatches action to open profile editor.
     */
    openEditProfile(): void {
        this.store.dispatch(authActions.openEditProfile());
    }

    /**
     * Unsubscribes to user roles when component is destroyed.
     */
    ngOnDestroy(): void {
        if (this.userRolesSubscription) this.userRolesSubscription.unsubscribe();
    }
}