/**
 * 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, OnDestroy, OnInit } from '@angular/core';

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

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 datasetActions from 'src/app/metamodel/actions/dataset.actions';
import * as datasetFamilyActions from 'src/app/metamodel/actions/dataset-family.actions';
import * as surveyActions from 'src/app/metamodel/actions/survey.actions';
import * as instanceSelector from 'src/app/metamodel/selectors/instance.selector';
import { AppConfigService } from 'src/app/app-config.service';

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

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

    ngOnInit() {
        // Create a micro task that is processed after the current synchronous code
        // This micro task prevent the expression has changed after view init error
        Promise.resolve(null).then(() => this.store.dispatch(datasetFamilyActions.loadDatasetFamilyList()));
        Promise.resolve(null).then(() => this.store.dispatch(datasetActions.loadDatasetList()));
        Promise.resolve(null).then(() => this.store.dispatch(surveyActions.loadSurveyList()));
        this.instanceSubscription = this.instance.subscribe(instance => {
            if (instance.config.search.search_by_criteria_allowed) {
                this.links.push({ label: instance.config.search.search_by_criteria_label, icon: 'fas fa-search', routerLink: 'search' });
            }
            if (instance.config.search.search_multiple_allowed) {
                this.links.push({ label: instance.config.search.search_multiple_label, icon: 'fas fa-search-plus', routerLink: 'search-multiple' });
            }
            if (instance.config.documentation.documentation_allowed) {
                this.links.push({ label: instance.config.documentation.documentation_label, icon: 'fas fa-question', routerLink: 'documentation' });
            }
            if (instance.config.design.design_favicon !== '') {
                this.favIcon.href = `${this.config.apiUrl}/download-instance-file/${instance.name}/${instance.config.design.design_favicon}`;
            }
            this.title.innerHTML = instance.label;
        })
    }

    /**
     * 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;
    }

    /**
     * Returns API URL.
     *
     * @return string
     */
    getApiUrl(): string {
        return this.config.apiUrl;
    }

    /**
     * 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 instance when component is destroyed.
     */
    ngOnDestroy() {
        if (this.instanceSubscription) this.instanceSubscription.unsubscribe();
    }
}