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

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

import { AbstractSearchComponent } from './abstract-search.component';
import { ConeSearch, Criterion, Resolver, SvomKeyword } from '../../store/models';
import * as searchActions from '../../store/actions/search.actions';
import * as coneSearchActions from '../../store/actions/cone-search.actions';
import * as coneSearchSelector from '../../store/selectors/cone-search.selector';
import * as svomJsonKwActions from '../../store/actions/svom-json-kw.actions';
import * as svomJsonKwSelector from '../../store/selectors/svom-json-kw.selector';

/**
 * @class
 * @classdesc Search criteria container.
 */
@Component({
    selector: 'app-criteria',
    templateUrl: 'criteria.component.html'
})
export class CriteriaComponent extends AbstractSearchComponent {
    public resolver: Observable<Resolver>;
    public resolverIsLoading: Observable<boolean>;
    public resolverIsLoaded: Observable<boolean>;
    public svomKeywords: Observable<SvomKeyword[]>;

    constructor(protected store: Store<{ }>) {
        super(store);
        this.resolver = this.store.select(coneSearchSelector.selectResolver);
        this.resolverIsLoading = this.store.select(coneSearchSelector.selectResolverIsLoading);
        this.resolverIsLoaded = this.store.select(coneSearchSelector.selectResolverIsLoaded);
        this.svomKeywords = this.store.select(svomJsonKwSelector.selectSvomKeywords);
    }

    ngOnInit(): void {
        // 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(searchActions.changeStep({ step: 'criteria' })));
        Promise.resolve(null).then(() => this.store.dispatch(searchActions.checkCriteria()));
        super.ngOnInit();
    }

    /**
     * Dispatches action to add the given criterion to the search.
     *
     * @param  {Criterion} criterion - The criterion.
     */
    addCriterion(criterion: Criterion): void {
        this.store.dispatch(searchActions.addCriterion({ criterion }));
    }

    /**
     * Dispatches action to remove the given criterion ID to the search.
     *
     * @param  {number} idCriterion - The criterion ID.
     */
    deleteCriterion(idCriterion: number): void {
        this.store.dispatch(searchActions.deleteCriterion({ idCriterion }));
    }

    /**
     * Dispatches action to add cone search.
     *
     * @param  {ConeSearch} coneSearch - The cone search.
     */
    addConeSearch(coneSearch: ConeSearch): void {
        this.store.dispatch(coneSearchActions.addConeSearch({ coneSearch }));
    }

    /**
     * Dispatches action to remove the cone search.
     */
    deleteConeSearch(): void {
        this.store.dispatch(coneSearchActions.deleteConeSearch());
    }

    /**
     * Dispatches action to retrieve object coordinates.
     *
     * @param  {string} name - The object name.
     */
    retrieveCoordinates(name: string): void {
        this.store.dispatch(coneSearchActions.retrieveCoordinates({ name }));
    }

    selectSvomAcronym(acronymSelected: string): void {
        this.store.dispatch(svomJsonKwActions.selectAcronym({ acronymSelected }));
    }

    resetSvomKeywords(): void {
        Promise.resolve(null).then(() => this.store.dispatch(svomJsonKwActions.resetKw()));
    }
}