import { OnChanges } from '@angular/core/src/metadata/lifecycle_hooks';
import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { DomSanitizer, SafeResourceUrl, } from '@angular/platform-browser';
import { AuthService } from 'app/services/auth.service';
import { LocalStorageService } from 'angular-2-local-storage';
import { environment } from '../../../../../../environments/environment.dev';

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

  @Input() primaryAssessment: any;
  @Output() updateScore = new EventEmitter<string>();
  reportUrl: SafeResourceUrl;
  access = {
    active: false,
    inProgress: false,
    complete: false,
    report: true
  };
  rightContainer = 'container-right';
  imgScore = 'img-score';
  primaryResp: any;
  imageLink = '';
  userInfoMstrId;
  userInfoMstrList: any[];
  primaryAssessmentList: any[];
  email = '';
  assessmentId = '0';
  minPassMarks = 0;
  skillTrackMasterId = 0;
  constructor(private route: ActivatedRoute,
    private sanitizer: DomSanitizer,
    private AuthService: AuthService,
    private router: Router,
    private localStorageService: LocalStorageService) {
  }

  ngOnInit() {
    this.userInfoMstrList = [];
    this.primaryAssessmentList = [];
    this.imageLink = environment.imageUrl+'/assets/img/employee/dashboard/assessment';
    this.getAssessmentId(this.primaryAssessment);
    const empData = this.localStorageService.get('userInfo');
    if (empData['userInfoMstrId']) {
      this.userInfoMstrId = empData['userInfoMstrId'];
      this.email = empData['email'];
    }
  }

  ngOnChanges() {
  }

  /*START ASSESSMENT FUNCTION*/
  startAssessment(assessment) {
    this.access.active = false;
    this.access.inProgress = true;
    //  document.getElementById('todo').style = {display:'none'}
    this.hide('todo');
    this.hide('start-assessment');
    if (assessment.assessmentStatus === 'ASSIGNED') {
      this.updateStatus(assessment, 'INPROGRESS');
    }
    window.open(assessment.testLink, '_blank');
  }

  /*COMPLETE ASSESSMENT FUNCTION*/
  completeAssessment(assessment) {
    this.access.active = false;
    this.access.inProgress = false;
    this.access.complete = true;
    this.hide('inprogress');
    this.employeeAssessmentReport(assessment);
    this.updateStatus(assessment, 'DONE');

  }

  /*VIEW REPORT FUNCTION*/
  report(assessment) {
    this.access.report = false;
    this.rightContainer = 'container-right right-container';
    this.imgScore = 'img-score-left';
    this.employeeAssessmentReport(assessment);
  }

  /*SCORE TYPE CONVERSTION*/
  score(mark) {
    return Number(mark);
  }

  /*CLOSE REPORT FUNCTION*/
  closeReport() {
    this.access.report = true;
    this.rightContainer = 'container-right';
    this.imgScore = 'img-score';
  }

  /*STATUS CHECKING FUNCTION*/
  checkStatus(assessment) {
    if (assessment) {
      if (assessment.assessmentStatus === 'INPROGRESS') {
        this.access.inProgress = true;
        return true;
      } else if (assessment.assessmentStatus === 'ASSIGNED') {
        this.access.active = true;
        return true;
      } else if (assessment.assessmentStatus === 'DONE') {
        this.access.complete = true;
        return true;
      } else {
        this.access.active = true;
        return true;
      }
    } else {
      return false;
    }
  }

  /*UPDATE ASSESSMENT STATUS FUNCTION*/
  updateStatus(assessment, status) {
    this.AuthService.updateAssessmentStatus(assessment.eventDtlsId, status).then(response2 => {

      console.log(response2);
    }).catch(err => {
      console.log(err);
    });
  }

  /*ASSESSMENT REPORT FUNCTION*/
  employeeAssessmentReport(assessment) {
    console.log(assessment.user.email, assessment.assessmentMappingId);
    this.AuthService.getEmployeeMiniAssessmentPrimaryEvaluationReport(assessment.user.email, assessment.assessmentMappingId).then(responseData => {
      this.reportUrl = this.sanitizer.bypassSecurityTrustResourceUrl(responseData.candidate.testStatus.pdfReport);
      this.updateScore.emit('1');
    }).catch(err => {
      console.log(err);
    });
  }

  /*JAVASCRIPT CORE HIDE FUNCTION */
  hide(id) {
    const hide = document.getElementById(id);
    hide.style.display = 'none';
  }

  reschedule() {
    this.userInfoMstrList.push(this.userInfoMstrId);
    this.primaryAssessmentList.push(this.assessmentId);
    const payLoad = {
      selfAssessmentBean: [
      {
        skillTrackMstrId : this.skillTrackMasterId,
        primaryAssessmentId: this.primaryAssessmentList,
      }
    ],
      userInfoMstrId: this.userInfoMstrList,
      loggedInUserEmail: this.email,
      eventDtlsId: this.primaryAssessment.eventDtlsId,
      mandatory: false
  };

  this.AuthService.selfAssessment(payLoad).then(resp => {
      console.log(resp, 'selfAssessment');
      this.router.navigate(['/employee/dashboard']);
    }).catch(err => {
      console.log(err);
    });
  }

  getAssessmentId(primaryAssessment) {
   const primary: any[] = primaryAssessment['primary'];
    if (primary.length > 0) {
      primary.forEach(data => {
        this.assessmentId = data['assessmentId'];
        this.minPassMarks = data['minPassMarks'];
        this.skillTrackMasterId = data['pgSkillTrackMstr']['skillTrackMstrId'];
      });
    }
  }

}
