import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Select, Store } from '@ngxs/store';
import { RouterExtensions } from '@nativescript/angular';
import { Observable } from 'rxjs';
import { ActionsExecuting, actionsExecuting } from '@ngxs-labs/actions-executing';
import { CongCandidateContactOutput, CongCandidateDetailOutput, CongCandidateDetailPersonOutput } from '~/pages/event/entities/cong-candidate-detail.types';
import { LoadCongCandidateDetail, LoadCongCandidateFunctions, LoadCongCandidatePersonDetail, LoadCongCandidateQualificaton, LoadCongCandidateRecommendation } from '~/pages/event/state-mgmt/cong/cong-candidate-detail.actions';
import { CongCandidateDetailState } from '~/pages/event/state-mgmt/cong/cong-candidate-detail.state';
import { LoadCongCandidateContacts, LoadCongCandidateEducation } from './../../../state-mgmt/cong/cong-candidate-detail.actions';

@Component({
    selector: 'CongCandidateDetailPage',
    moduleId: module.id,
    templateUrl: './cong-candidate-detail-page.component.html',
})
export class CongCandidateDetailPageComponent implements OnInit {
    @Select(actionsExecuting([
        LoadCongCandidateDetail,
        LoadCongCandidatePersonDetail,
        LoadCongCandidateContacts,
        LoadCongCandidateEducation,
        LoadCongCandidateFunctions,
        LoadCongCandidateQualificaton,
        LoadCongCandidateRecommendation,
    ]))
    isLoading$: Observable<ActionsExecuting>;

    @Select(CongCandidateDetailState.getCandiateDetail)
    candidateDetail$: Observable<CongCandidateDetailOutput>;

    @Select(CongCandidateDetailState.getCandiatePersonDetail)
    candidatePersonDetail$: Observable<CongCandidateDetailPersonOutput>;
    @Select(CongCandidateDetailState.getContacts)
    contacts$: Observable<CongCandidateContactOutput[]>;
    @Select(CongCandidateDetailState.getEducation)
    education$: Observable<CongCandidateContactOutput[]>;
    @Select(CongCandidateDetailState.getRecommendations)
    recommendations$: Observable<CongCandidateContactOutput[]>;
    @Select(CongCandidateDetailState.getQualifications)
    qualifications$: Observable<CongCandidateContactOutput[]>;
    @Select(CongCandidateDetailState.getFunctions)
    functions$: Observable<CongCandidateContactOutput[]>;

    candidateId: number;
    constructor(
        private routerExtensions: RouterExtensions,
        private route: ActivatedRoute,
        private store: Store,
    ) {
    }
    ngOnInit(): void {
        this.route.params.subscribe(params => {
            this.candidateId = parseInt(params['id'], 10);
            this.store.dispatch([
                new LoadCongCandidateDetail(this.candidateId),
                new LoadCongCandidatePersonDetail(this.candidateId),
                new LoadCongCandidateContacts(this.candidateId),
                new LoadCongCandidateEducation(this.candidateId),
                new LoadCongCandidateFunctions(this.candidateId),
                new LoadCongCandidateQualificaton(this.candidateId),
                new LoadCongCandidateRecommendation(this.candidateId),
            ])
        })
    }
    onBackButton(): void {
        this.routerExtensions.back();
    }

}