import { Component, OnInit } from '@angular/core';
import { ServicesService } from '../../services.service';
import { Router, ActivatedRoute } from '@angular/router';
import { NgxSpinnerService } from 'ngx-spinner';
import { AuthserviceService } from '../../authservice.service';
import { LivequeserviceService } from '../../services/livequestion/livequeservice.service';

@Component({
  selector: 'app-livequestion',
  templateUrl: './livequestion.component.html',
  styleUrls: ['./livequestion.component.scss'],
})
export class LivequestionComponent implements OnInit {
  Questions = [];
  SingleQue = [];
  QuestionKey = 0;
  TestId: any;
  ShowAnswer: boolean = false;
  AnswerButton = 'Show Answer';

  constructor(
    public auth: AuthserviceService,
    public service: ServicesService,
    public spinner: NgxSpinnerService,
    public ActiveRoute: ActivatedRoute,
    public router: Router,
    public LivequeserviceService: LivequeserviceService
  ) {
    this.service.headershow = true;
    this.service.SearchValue = true;
    var access = window.localStorage.getItem('accessitem');
    if (access) {
      var set = JSON.parse(<string>access);
      if (set) {
        this.service.access.CATaccess = set.CAT_access ? true : false;
        this.service.access.Knowaccess = set.KA_access ? true : false;
        this.service.access.Practiceaccess = set.PT_access ? true : false;
        this.service.access.livechat_access = set.livechat_access ? true : false;
      }
    }
    this.ActiveRoute.params.subscribe(async (params) => {
      try {
        this.TestId = JSON.parse(atob(params['id']));
      } catch (error) {
        this.router.navigate(['login']);
      }
    });
  }

  ngOnInit(): void {
    this.getAllLiveQuestionTestWise();
  }
  async getAllLiveQuestionTestWise() {
    this.spinner.show();
    await this.auth.getAuthenticatedUser().getSession(async (err, session) => {
      if (err) {
        await this.spinner.hide();
        this.router.navigate(['/login']);
      } else {
        var token = session
        await this.LivequeserviceService.GetAllLiveQuestionTestWise(
          this.TestId.id,
          token
        ).then(async (data) => {
          if (data[0].status == 200) {
            var list = JSON.parse(<string>data[0].json);
            this.Questions = list.data.questions;
            if (this.Questions.length > 0) {
              let No = 1;
              this.Questions.forEach((element) => {
                element['QuestionNo'] = No++;
              });
              this.SingleQue.push(this.Questions[this.QuestionKey]);
            }
          }
        });
        this.spinner.hide();
      }
    });
  }
  nextQuestion() {
    this.SingleQue = [];
    this.ShowAnswer = false;
    this.AnswerButton = 'Show Answer';
    this.QuestionKey++;
    if (this.QuestionKey < this.Questions.length) {
      this.SingleQue.push(this.Questions[this.QuestionKey]);
    }
  }
  showAnswer() {
    this.ShowAnswer = !this.ShowAnswer;
    this.ShowAnswer
      ? (this.AnswerButton = 'Hide Answer')
      : (this.AnswerButton = 'Show Answer');
  }
}
