import { Component, OnInit } from '@angular/core';
import { RouterExtensions } from '@nativescript/angular';
import { Select, Store } from '@ngxs/store';
import { Feedback, FeedbackType } from 'nativescript-feedback';
import { Observable } from 'rxjs';
import { transitionOptions } from '~/constants/shared.constants';
import { EvaluationDetailOutput } from '../../entities/output/evaluationDetailOutput';
import { LoadAnswers } from '../../state-mgmt/evaluation-answers.actions';
import { AnswersState } from '../../state-mgmt/evaluation-answers.state';
import { LoadEvaluationDetail } from '../../state-mgmt/evaluation-detail.actions';
import { SearchEvaluationDetailState } from '../../state-mgmt/evaluation-detail.state';

@Component({
    selector: 'EvaluationDetail',
    moduleId: module.id,
    templateUrl: './evaluation-detail.component.html',
    styleUrls: ['./evaluation-detail.component.scss']
})
export class EvaluationDetailComponent implements OnInit {
    @Select(SearchEvaluationDetailState.isLoading) isLoading$: Observable<boolean>;
    @Select(SearchEvaluationDetailState.getEvaluationDetail) evaluationDetail$: Observable<EvaluationDetailOutput>;

    @Select(AnswersState.isLoading) answersIsLoading$: Observable<boolean>;

    othersIsLoading: boolean;
    evaluationDetail: EvaluationDetailOutput;

    constructor(
        private routerExtensions: RouterExtensions,
        private store: Store
    ) {}

    ngOnInit(): void {
        this.othersIsLoading = false;
        this.store.dispatch(new LoadEvaluationDetail());
        this.evaluationDetail$.subscribe(evaluationDetail => this.evaluationDetail = evaluationDetail);
    }

    // Back button action
    onBackButton(): void {
        this.routerExtensions.back();
    }

    // Loads actions after onCreate action and routes on survey page
    loadAnswers() {
        this.othersIsLoading = true;
        // Limits in ms
        const oneCallLimit = 100; // 100 ms
        const loadAnswersLimit = 5000; // 5 sec

        this.store.dispatch(new LoadAnswers(this.evaluationDetail.ID));
        let answersLoading = true;

        // We kill answers loading in case it takes more than 'loadAnswersLimit'
        const operationTimer = setTimeout(() => {
            clearInterval(loadAnswersInterval);
            new Feedback().show({
                title: 'Vypršel timeout',
                type: FeedbackType.Error,
                duration: 5000,
                message: 'Časový limit pro operaci vypršel.'
            });
            this.othersIsLoading = false;
            return;
        }, loadAnswersLimit);

        const loadAnswersInterval = setInterval(() => {
            this.answersIsLoading$.subscribe(loading => answersLoading = loading);
            // We will wait until the answers are loaded
            if (!answersLoading) {
                this.routerExtensions.navigate(['evaluation/answers', this.evaluationDetail.ID], { transition: transitionOptions });
                clearInterval(loadAnswersInterval);
                clearTimeout(operationTimer);
                this.othersIsLoading = false;
            }
        }, oneCallLimit);
    }
}
