import { Component, OnInit, Input, OnChanges } from '@angular/core';
import { Router } from '@angular/router';
import { EmployeeReview, ReviewScore, FeedBack } from '../../../models/employee.model';
import { EmployeeHttpService } from '../../../services/employee/employee-http.service';
import { Subject } from 'rxjs/Subject';
import { environment } from '../../../../environments/environment.dev';

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

  @Input() reviewData: EmployeeReview | null;
  @Input() reviewerId;
  private $destroyed = new Subject();
  eventDetailsId = '';
  reviewScore: ReviewScore;
  score = 0;
  overallComments = '';
  feedbackQuestions: FeedBack[] = [];
  hideFeedbackRating = true;

  imagelink: string;

  constructor(private router: Router,
    private employeeHttpService: EmployeeHttpService) { }

  ngOnInit() {
    this.imagelink = environment.imageUrl+'/assets/img/employee/newdashboard/';
  }

  ngOnChanges() {
    this.eventDetailsId = String(this.reviewData.eventDetailsId);
    this.getReviewScore();
  }

  gotoDashBoard() {
    this.router.navigate(['/employee/dashboard']);
  }

  getReviewScore() {
    // this.eventDetailsId = '28';
    // this.reviewerId = '5';
    this.employeeHttpService.getEmployeeReviewScore(this.eventDetailsId, this.reviewerId).takeUntil(this.$destroyed)
      .subscribe(data => {
        //  console.log('reviewData' + JSON.stringify(data));
        this.reviewScore = data;
        this.score = this.parseScore((this.reviewScore.reviewerFeedback[0].score));
        this.overallComments = this.reviewScore.reviewerFeedback[0].comments;
        this.feedbackQuestions = this.reviewScore.reviewerFeedback[0].feedback;
        if (this.reviewScore.averageScore !== null) {
          this.hideFeedbackRating = false;
        }
      }, (error) => {
        console.log('Error Encounted while getting review score');
      });
  }

  getRating(value) {
    return value === 100 ? 'Yes' : 'No';
  }

  getAnswer(value) {
    return value != null ? value.replace('|', ',') : value;
  }

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

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

}
