import { Component, OnDestroy, OnInit } from '@angular/core';
import { BaseComponent } from '../../base/base.component';
import * as moment from 'moment';
import { WorkPreferenceQuestionModel } from '../../models/work-preference-question.model';
import { takeUntil } from 'rxjs/operators';
import { AssessmentService } from '../../services/assessment.service';
import { Router } from '@angular/router';
import { HelperService } from '../../services/helper.service';
import { QuestionModel } from '../../models/question.model';

@Component({
    selector: 'app-work-preference',
    templateUrl: './work-preference.component.html',
    styleUrls: ['./work-preference.component.scss']
})

export class WorkPreferenceComponent extends BaseComponent implements OnInit, OnDestroy {

    workPreferenceQuestion = new WorkPreferenceQuestionModel();
    assocs = ['CultureElement', 'CultureElement.Questions'];
    autoSaveInterval;
    autoSavedOn;
    autoSaveLabel = 'a few seconds ago';

    constructor(
        private as: AssessmentService,
        private hs: HelperService,
        private r: Router
    ) {
        super();
    }

    ngOnInit() {
        this.initiateWorkPreference();
        this.setAutoSaveInterval();
    }

    initiateWorkPreference(): void {
        this.isLoading = true;
        this.as.getWorkPreferences(this.assocs)
            .pipe(takeUntil(this.componentInView))
            .subscribe(response => {
                this.isLoading = false;
                this.autoSavedOn = new Date();
                this.autoSave();
                if (response.length > 0) {
                    this.workPreferenceQuestion = response[0];
                } else {
                    this.exit();
                }
            }, err => {
                this.isLoading = false;
                this.hs.handleHttpError(err);
            });
    }

    setAutoSaveInterval(): void {
        if (!this.autoSaveInterval) {
            setInterval(() => {
                this.autoSave();
            }, 60000);
        }
    }

    autoSave(): void {
        this.autoSaveLabel = moment(this.autoSavedOn).fromNow();
    }

    exit(): void {
        this.r.navigate(['/my-career-web/dashboard/home']);
    }

    submitAnswer(question: QuestionModel): void {
        const formData = {
            CultureElement: this.workPreferenceQuestion.CultureElement.ObjectID,
            SelectedQuestion: question.ObjectID,
            Continue: true
        };
        this.isLoading = true;
        this.as.saveWorkPreferences(formData)
            .subscribe(response => {
                this.isLoading = false;
                this.autoSavedOn = new Date();
                this.autoSave();
                if (response.NextQuestion) {
                    this.initiateWorkPreference();
                } else {
                    this.exit();
                }
            }, err => {
                this.isLoading = false;
                this.hs.handleHttpError(err);
            });
    }

    ngOnDestroy(): void {
        super.ngOnDestroy();
        if (this.autoSaveInterval) {
            clearInterval(this.autoSaveInterval);
        }
    }

}
