import { Injectable } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { QuestionBase, RatingQuestion, MultiChoiceQuestion, SingleChoiceQuestion } from '../../models/employee.model';

@Injectable()
export class EmployeeFeedBackService {
  questionData = {};
  singleChoiceQuestions = [];
  multiChoiceQuestions = [];
  ratingQuestions = [];
  counter = 1;
  isQuestionsEmpty = true;
  questionsList: QuestionBase<any>[] = [];

  constructor() { }
  toFormGroup(questions: QuestionBase<any>[]) {
    const group: any = {};
    questions.forEach(question => {
      group[question.key] = question.required ? new FormControl(question.value || '', Validators.required)
        : new FormControl(question.value || '');
      group[question.feedback] = question.required ? new FormControl(question.value || '', Validators.required)
        : new FormControl(question.value || '');
    });
    return new FormGroup(group);
  }

  getOptions(answers: any[]) {
    const options: any[] = [];
    answers.forEach(answer => {
      options.push({ value: answer['option'] });
    });
    return options;
  }

  addSingleChoiceQuestions(questionArray: any[]) {
    if (questionArray.length > 0) {
      questionArray.forEach(question => {
        this.questionsList.push(new SingleChoiceQuestion({
          key: question.questionId,
          label: question.question,
          options: this.getOptions(question.answers),
          feedback: question.questionId + '-feedback',
          required: true,
          order: this.counter
        }));
        this.counter++;
      });
    }
  }

  addMultiChoiceQuestion(questionArray: any[]) {
    if (questionArray.length > 0) {
      questionArray.forEach(question => {
        this.questionsList.push(new MultiChoiceQuestion({
          key: question.questionId,
          label: question.question,
          options: this.getOptions(question.answers),
          feedback: question.questionId + '-feedback',
          required: true,
          order: this.counter
        }));
        this.counter++;
      });
    }
  }

  addRatingQuestion(questionArray: any[]) {
    if (questionArray.length > 0) {
      questionArray.forEach(question => {
        this.questionsList.push(new RatingQuestion({
          key: question.questionId,
          label: question.question,
          options: this.getOptions(question.answers),
          feedback: question.questionId + '-feedback',
          required: true,
          order: this.counter
        }));
        this.counter++;
      });
    }
  }

  addoverRatingQuestion() {
    this.questionsList.push(new RatingQuestion({
      key: 'overallRating',
      label: 'Overall Rating',
      feedback: 'overallRating-feedback',
      required: true,
      order: this.counter
    }));
  }

  getQuestions(questionData) {
    this.counter = 1;
    this.questionsList = [];
    this.questionData = questionData;
    // console.log('single choice questions ' + JSON.stringify(this.questionData['singleSelectQuestions']));
    if (this.questionData['singleSelectQuestions'] !== undefined) {
      this.singleChoiceQuestions = this.questionData['singleSelectQuestions'];
      this.addSingleChoiceQuestions(this.singleChoiceQuestions);
    }
    if (this.questionData['multipleSelectQuestions'] !== undefined) {
      this.multiChoiceQuestions = this.questionData['multipleSelectQuestions'];
      this.addMultiChoiceQuestion(this.multiChoiceQuestions);
    }
    if (this.questionData['customQuestions'] !== undefined) {
      this.ratingQuestions = this.questionData['customQuestions'];
      this.addRatingQuestion(this.ratingQuestions);
    }
    if (this.singleChoiceQuestions.length > 0 || this.multiChoiceQuestions.length > 0 || this.ratingQuestions.length > 0) {
      this.isQuestionsEmpty = false;
      this.addoverRatingQuestion();
    }
    return this.questionsList.sort((a, b) => a.order - b.order);
  }

  isFeedBackDataEmpty() {
    return this.isQuestionsEmpty;
  }
}
