import { Observable, of } from 'rxjs';
import { PickerProvider } from '~/core/entities/picker-provider';
import { EvaluationRadioOptions } from './evaluation-radio-options';
import { EvaluationAnswerAllOutput } from './output/evaluationAnswersOutput';

export class EvaluationSurvey {
    answers: EvaluationAnswerAllOutput[];

    currentSection: string = '';
    currentSet: string = '';
    currentSetDesc: string = '';

    // Current set/page in UI
    currentSetOrder: number = 0;
    // Number of sets/pages in UI
    maxSetOrder: number = 0;

    // Index of first question of set/page in UI
    currentFromIndex: number = 0;
    // Index of last question of set/page in UI
    currentToIndex: number = 0;

    constructor(answers: EvaluationAnswerAllOutput[]) {
        this.answers = answers;
        // Sorting function for received input
        this.answers.sort((a1, a2) => {
            if (a1.SetOrder > a2.SetOrder) {
                return 1;
            }

            if (a1.SetOrder < a2.SetOrder) {
                return -1;
            }

            if (a1.CriteriaOrder > a2.CriteriaOrder) {
                return 1;
            }

            if (a1.CriteriaOrder < a2.CriteriaOrder) {
                return -1;
            }

            return 0;
        });

        // Load all answers at once
        this.answers.forEach((answer: EvaluationAnswerAllOutput) => {
            answer.radioOptions = new EvaluationRadioOptions(answer.Answer1, answer.Answer2, answer.Answer3, answer.Answer4, answer.Answer);
        });

        if (this.answers.length > 0) {
            this.maxSetOrder = this.answers[this.answers.length - 1].SetOrder;
            this.currentSetOrder = this.answers[0].SetOrder;
            this.currentSection = this.answers[0].EvaluationSection;
            this.currentSet = this.answers[0].EvaluationSet;
            this.currentSetDesc = this.answers[0].SetDescription;

            // Sets currentToIndex to last question of first set
            while (this.currentToIndex + 1 < this.answers.length && this.answers[this.currentToIndex + 1].SetOrder === this.currentSetOrder) {
                this.currentToIndex++;
            }
        }
    }

    // Current set of questions
    getCurrentSet(): EvaluationAnswerAllOutput[] {
        return this.answers.slice(this.currentFromIndex, this.currentToIndex + 1);
    }

    // Loads next set of questions/next page in UI
    nextSet(): void {
        if (this.currentSetOrder < this.maxSetOrder) {
            this.currentSetOrder++;
            this.currentToIndex++;
            this.currentFromIndex = this.currentToIndex;

            while (this.currentToIndex + 1 < this.answers.length && this.answers[this.currentToIndex + 1].SetOrder === this.currentSetOrder) {
                this.currentToIndex++;
            }

            this.currentSection = this.answers[this.currentFromIndex].EvaluationSection;
            this.currentSet = this.answers[this.currentFromIndex].EvaluationSet;
            this.currentSetDesc = this.answers[this.currentFromIndex].SetDescription;
        }
    }

    // Loads previous set of questions/previous page in UI
    prevSet(): void {
        if (this.currentSetOrder > 1) {
            this.currentSetOrder--;
            this.currentFromIndex--;
            this.currentToIndex = this.currentFromIndex;

            while (this.currentFromIndex - 1 >= 0 && this.answers[this.currentFromIndex - 1].SetOrder === this.currentSetOrder) {
                this.currentFromIndex--;
            }

            this.currentSection = this.answers[this.currentFromIndex].EvaluationSection;
            this.currentSet = this.answers[this.currentFromIndex].EvaluationSet;
            this.currentSetDesc = this.answers[this.currentFromIndex].SetDescription;
        }
    }

    isFirstPage(): boolean {
        return this.currentFromIndex === 0;
    }

    isLastPage(): boolean {
        return this.currentToIndex === (this.answers.length - 1);
    }
}
