import { Component, OnInit } from '@angular/core';
import { AuthorizationService } from '../../service/authorization.service';
import { CohortService } from '../../services/cohort/cohort.service';
import { NgxSpinnerService } from 'ngx-spinner';
import { ActivatedRoute, Router } from '@angular/router';
import { Location } from '@angular/common';

@Component({
  selector: 'app-student-exam-result',
  templateUrl: './student-exam-result.component.html',
  styleUrls: ['./student-exam-result.component.scss']
})
export class StudentExamResultComponent implements OnInit {
  time_zone = this.auth.currentTimeZone();
  Cohorts = [];
  bkpAllExam = [];
  allExam = [];
  SelectedTest: string = "";
  SelectedTypeTest: string = "";
  allTypeTest = [{ id: "", name: "---Select---" }, { id: "KA", name: "Knowledge Assessment" }, { id: "PT", name: "Practice Test" }];
  SelectedCohort: any;
  user_id: any;
  user_email: any;
  Name: any;
  Avatar: any;
  visibleKA: boolean = false;
  visiblePT: boolean = false;
  visibleCAT: boolean = false;
  userDetails: any;
  examDataPass = [];
  constructor(
    private location: Location,
    public auth: AuthorizationService,
    public service: CohortService,
    public spinner: NgxSpinnerService,
    public ActiveRoute: ActivatedRoute,
    public router: Router,
  ) {
    this.ActiveRoute.params.subscribe(async (params) => {
      try {
        let Details = JSON.parse(atob(params['id']));
        this.user_id = Details.user_id;
        let cohortIds = Details.cohortIds;
        this.user_email = Details.email;
        this.spinner.show();
        await this.getCohortByIds(cohortIds);
        await this.getCohortWiseUserAllExam(this.user_id);
      } catch (error) {
        this.router.navigate(['main/students']);
      }
    });
  }

  ngOnInit(): void {
  }

  back() {
    this.location.back();
  }
  async getCohortByIds(cohortIds) {
    await this.auth.getAuthenticatedUser().getSession(async (err, session) => {
      if (err) {
        await this.spinner.hide();
        this.router.navigate(['/login']);
      }
      else {
        var token = ''
        this.spinner.show();
        await this.service.getCohortByIds(cohortIds, token).then(async (data) => {
          if (data[0].status == 200) {
            var list = JSON.parse(<string>data[0].json);
            this.Cohorts = list.data;
            this.SelectedCohort = "";
          }
        });
        this.spinner.hide();
      }
    });
  }
  async getCohortWiseUserAllExam(pStr_User_id: any) {
    await this.auth.getAuthenticatedUser().getSession(async (err, session) => {
      if (err) {
        await this.spinner.hide();
        this.router.navigate(['/login']);
      }
      else {
        var token = ''
        this.spinner.show();
        await this.service.getCohortWiseUserAllExam(pStr_User_id, token).then(async (data) => {
          if (data[0].status == 200) {
            var list = JSON.parse(<string>data[0].json);
            this.bkpAllExam = list.data.length > 1 ? list.data[0] : [];
            this.userDetails = list.data.length > 1 ? list.data[1] : [];
            this.Name = this.userDetails.length > 0 ? this.userDetails[0].aliasname : "";
            this.Avatar = this.userDetails.length > 0 ? this.userDetails[0].avatar_url : "";
          }
        });
        this.spinner.hide();
      }
    });
  }
  async changeCohort() {
    this.resetAll();
  }
  async changeTestType() {
    this.SelectedTest = "";
    this.allExam = this.bkpAllExam.filter(x => x.type == this.SelectedTypeTest && x.cohort_id == this.SelectedCohort);
    this.changeTest();
  }
  async changeTest() {
    this.popupClose();
    this.visibleKA = false;
    if (this.SelectedTypeTest == "KA" && this.SelectedTest != "") {
      this.visibleKA = true;
      this.examDataPass = this.allExam.filter(x => x.type == this.SelectedTypeTest && x.cohort_id == this.SelectedCohort && x.type_id == this.SelectedTest)[0];
    }
    else if (this.SelectedTypeTest == "PT" && this.SelectedTest != "") {
      this.examDataPass = this.allExam.filter(x => x.type == this.SelectedTypeTest && x.cohort_id == this.SelectedCohort && x.type_id == this.SelectedTest)[0];
      this.visiblePT = true;
    }
    else if (this.SelectedTypeTest == "CAT" && this.SelectedTest != "") {
      this.visibleCAT = true;
    }
    else {
      this.popupClose();
    }
  }
  async resetAll() {
    this.visibleKA = false;
    this.visiblePT = false;
    this.visibleCAT = false;
    this.SelectedTypeTest = "";
    this.SelectedTest = "";
    this.changeTestType();
    this.changeTest();
  }
  async popupClose() {
    this.visibleKA = false;
    this.visiblePT = false;
    this.visibleCAT = false;
  }
}

