
import { Component, OnInit, OnDestroy, OnChanges } from '@angular/core';
import { EmployeeNavBarService } from '../employee-nav-bar.service';
import { Router, ActivatedRoute } from '@angular/router';
import { AuthService } from 'app/services/auth.service';
import { LocalStorageService } from 'angular-2-local-storage';
import { ReviewScore, FeedBack, ReviewerFeedBack } from '../../../models/employee.model';
import { environment } from '../../../../environments/environment.dev';

@Component({
  selector: 'app-bny-employee-assessment',
  templateUrl: './bny-employee-assessment.component.html',
  styleUrls: ['./bny-employee-assessment.component.scss']
})
export class BNYEmployeeAssessmentComponent implements OnInit, OnChanges {

  assessmentId: string;
  eventDtlsId: any;
  assessmentData = false;
  assessmentDtls: any;
  primaryAssessment: any;
  technicalAssessment: any;
  businessAssessment: any;
  reviewScore: ReviewScore;
  score = 0;
  reviewerCount = 0;
  reviewerCompletedCount = 0;
  reviewerId = 0;
  reviewerFeedback: ReviewerFeedBack[];
  feedBack: ReviewerFeedBack;
  item = 0;
  isDataFetched = false;
  spinnerTitle = ['OVERALL', 'SCORE'];
  BNYAssessment = [
    { id: 1, name: 'Primary Assessment', img: environment.imageUrl+'/assets/img/employee/dashboard/assessment/Primary.png', desc: 'Objectives to evaluate your Technical skills and to measure the right metrics in your technical area.' },
    { id: 2, name: 'Technical Usecase Assessment', img: environment.imageUrl+'/assets/img/employee/dashboard/assessment/Usecase_illustration.png', desc: 'Simple Use cases to prove your technical skills. In short, Real-time problems should be solved using your Technical skills.' },
    { id: 3, name: 'Business Usecase Assessment', img: environment.imageUrl+'/assets/img/employee/dashboard/assessment/Usecase_illustration.png', desc: 'Simple Use cases to prove your technical skills. In short, Real-time problems should be solved using your Technical skills.' }
  ];
  constructor(
    protected localStorageService: LocalStorageService,
    private employeeNavBarService: EmployeeNavBarService,
    private authService: AuthService,
    private route: ActivatedRoute
  ) {
  }

  ngOnInit() {

    this.route.params.subscribe(params => {
      if (params['id'] !== undefined) {
        this.assessmentId = params['id'];
      }
    });
    this.route.params.subscribe(params => {
      if (params['eventId'] !== undefined) {
        this.eventDtlsId = params['eventId'];
      }
    });

    const empData = this.localStorageService.get('userInfo');
    console.log(empData, 'employeee Details');
    this.getAssessmetDetails(empData, this.eventDtlsId);
    //  this.getReviewerDetails();

    if (this.assessmentId === '0') {
      this.employeeNavBarService.setCurrentPage(7);
    } else if (this.assessmentId === '1') {
      this.employeeNavBarService.setCurrentPage(8);
    } else if (this.assessmentId === '2') {
      this.employeeNavBarService.setCurrentPage(9);
    }
  }

  ngOnChanges() {

  }

  updateScore(evt) {
    this.getAssessmetDetails(this.localStorageService.get('userInfo'), this.eventDtlsId);
  }

  getAssessmetDetails(empData, eventDtlsId) {
    console.log(empData, eventDtlsId, 'getAssessmetDetails params');
    this.authService.getEmployeeAssessmentDetails(
      {
        eventId: eventDtlsId,
        email: empData.email
      }).then(resp => {
        console.log(resp, 'response for assessment details particular event');
        const respData = resp;

        respData['user'] = {
          email: empData.email,
          adId: empData.adId,
          userInfoMstrId: empData.userInfoMstrId,
          fullName: empData.fullName,
        };
        this.assessmentDtls = respData;

        console.log(this.assessmentDtls, 'Assessment Details');

        if (respData.primary) {
          this.primaryAssessment = respData;
        } else if (respData.technical) {
          this.technicalAssessment = respData;
        } else if (respData.business) {
          this.businessAssessment = respData;
        }
        this.getReviewerDetails();
        this.assessmentData = true;
      }).catch(err => {
        console.log(err);
      });
  }

  getReviewerDetails() {
    // this.eventDtlsId = '28';
    // this.eventDtlsId = '118';
    // const reviewerId = '5';
    this.authService.getReviewerDetails(this.eventDtlsId).then(response => {
      // console.log('reviewData' + JSON.stringify(response));
      this.reviewScore = response;
      this.score = this.parseScore(parseFloat(this.reviewScore['averageScore']));
      this.getReviewerCount();
      this.getReviewCompletedCount();
      this.isDataFetched = true;
      this.assessmentId = this.assessmentId;
      this.reviewerId = 1;
    }).catch(err => {
      this.isDataFetched = true;
      console.log('Error Encounted while getting review details');
    });
  }

  parseScore(score) {
    return isNaN(score) ? 0 : score;
  }

  getReviewerCount() {
    let count = 0;
    this.reviewerFeedback = this.reviewScore.reviewerFeedback;
    this.reviewerFeedback.forEach(review => {
        count++;
    });
    this.reviewerCount = count;
    console.log('Reviewer Count ' + this.reviewerCount);
    this.feedBack = this.reviewerFeedback[0];
  }

  getReviewCompletedCount() {
    let count = 0;
    for (const entry of this.reviewerFeedback) {
      if (entry.status === 'PASSED' || entry.status === 'REJECTED') {
        count++;
      }
    }
    this.reviewerCompletedCount = count;
  }

  getAssessment(index) {
    this.feedBack = this.reviewerFeedback[index];
    this.assessmentId = this.assessmentId;
    this.item = index;
    this.reviewerId = index + 1;
  }

  getStatus(type) {
    const status = {
      'PASSED': 'Completed',
      'REJECTED': 'Completed',
      'PENDING': 'Reviewing',
      'default': 'Invalid'
    };
    return status[type] || status['default'];
  }

  getOuterStrokeColour(percent) {
    if (percent >= 65) {
      return '#3CB371';
    } else if (percent < 65 && percent >= 30) {
      return '#FF9933';
    } else if (percent < 30) {
      return '#F08080';
    }
  }

  showOverAllScore() {
    return this.reviewerCompletedCount === this.reviewerCount ? true : false;
  }

  validateAssessment() {
    if ((this.assessmentId === '1' || this.assessmentId === '2') && this.reviewerCount === 0) {
      return true;
    } else {
      return false;
    }
  }

}
