import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router';

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

import { environment } from '../../../environments/environment';
import { Criterion } from '../store/model';
import { Dataset, DatasetOutputFamily, OutputFamily, Category, Attribute } from '../../metamodel/model';
import * as searchActions from '../store/search.action';
import * as datasetActions from '../../metamodel/action/dataset.action';
import * as criteriaActions from '../../metamodel/action/criteria.action';
import * as outputActions from '../../metamodel/action/output.action';
import * as fromSearch from '../store/search.reducer';
import * as fromMetamodel from '../../metamodel/reducers';
import * as searchSelector from '../store/search.selector';
import * as metamodelSelector from '../../metamodel/selectors';

interface StoreState {
    search: fromSearch.State;
    metamodel: fromMetamodel.State;
}

@Component({
    selector: 'app-result',
    templateUrl: 'result.component.html',
    styleUrls: [ 'result.component.css' ]
})
export class ResultComponent implements OnInit {
    public apiPath: string = environment.apiUrl + '/search';
    public datasetName: Observable<String>;
    public currentStep: Observable<string>;
    public datasetList: Observable<Dataset[]>;
    public datasetAttributeList: Observable<Attribute[]>;
    public datasetOutputFamilyList: Observable<DatasetOutputFamily[]>;
    public outputFamilyList: Observable<OutputFamily[]>;
    public categoryList: Observable<Category[]>;
    public criteriaList: Observable<Criterion[]>;
    public outputList: Observable<number[]>;

    constructor(private route: ActivatedRoute, private store: Store<StoreState>) {
        this.datasetName = store.select(searchSelector.getDatasetName);
        this.currentStep = store.select(searchSelector.getCurrentStep);
        this.datasetList = store.select(metamodelSelector.getDatasetList);
        this.datasetAttributeList = this.store.select(metamodelSelector.getDatasetAttributeList);
        this.datasetOutputFamilyList = this.store.select(metamodelSelector.getDatasetOutputFamilyList);
        this.outputFamilyList = this.store.select(metamodelSelector.getOutputFamilyList);
        this.categoryList = this.store.select(metamodelSelector.getCategoryList);
        this.criteriaList = this.store.select(searchSelector.getCriteriaList);
        this.outputList = this.store.select(searchSelector.getOutputList);
    }

    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(new searchActions.ChangeStepAction('result')));
        this.route.paramMap.subscribe((params: ParamMap) => {
            const datasetName = params.get('dname');
            Promise.resolve(null).then(() => this.store.dispatch(new searchActions.SelectDatasetAction(datasetName)));
            this.store.dispatch(new criteriaActions.LoadDatasetAttributeListAction(datasetName));
            this.store.dispatch(new outputActions.LoadDatasetOutputFamilyAction(datasetName));
        });
        this.store.dispatch(new datasetActions.LoadDatasetListAction());
        this.store.dispatch(new outputActions.LoadOutputFamilyListAction());
        this.store.dispatch(new outputActions.LoadCategoryListAction());
    }
}