import { Component, Input, OnInit, OnChanges } from '@angular/core';
import { AuthService } from 'app/services/auth.service';
import { Router, ActivatedRoute } from '@angular/router';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import { ReviewerFeedBack } from '../../../../../models/employee.model';
import { environment } from '../../../../../../environments/environment.dev';

export enum STEP_STATUS {
  ASSIGNED = 'ASSIGNED',
  INPROGRESS = 'INPROGRESS',
  REVIEW_PENDING = 'REVIEW_PENDING',
  UNDER_REVIEW = 'UNDER_REVIEW',
  REVIEW_COMPLETED = 'REVIEW_COMPLETED',
  REVIEW_PASSED = 'REVIEW_PASSED',
  REVIEW_REJECTED = 'REVIEW_REJECTED',
  DONE = 'DONE',
}

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

  // @Input() technicalAssessment: any;
  _technicalAssessment: any;

  get technicalAssessment() {
    return this._technicalAssessment;
  }

  @Input()
  set technicalAssessment(role) {
    this._technicalAssessment = role;
  }
  @Input() reviewerFeedback: ReviewerFeedBack;
  @Input() reviewerId: Number;
  imageUrl = '';
  isSubmitEnabled = false;
  isSonarQReport = true;
  isRoport = true;
  isDetails = false;
  isResult = false;
  reports = true;
  isStart = true;
  reviewComplete = false;
  isunderReview = false;
  isDone = false;
  gitDetails: any;
  sonarReport: any;
  reviewScore = 0;
  resp = {
    'createUser': {
      'username': '',
    },
    'createRepo': {
      'web_url': '',
    },
    'userMapping': {
      'id': 10,
      'user_id': null,
      'name': 'Nithyananth1',
      'userName': null,
      'status': null,
      'access_level': 30,
      'errorBean': null
    },
    'errorBean': null
  };

  parseScore = (score) => isNaN(score) ? 0 : score;

  constructor(private authService: AuthService,
    private router: Router,
    private toastr: ToastsManager) {
    //  this.gitUserDetails(12,'login service authorized','sabashls','slakshmanan@altimetrik.com');
  }

  ngOnInit() {
    this.imageUrl = environment.imageUrl+'/assets/img/employee/dashboard/assessment/';
    console.log(this._technicalAssessment, 'technicalAssessment Child');

    this.checkStatus(this._technicalAssessment);
    // console.log(this.technicalAssessment(),'technicalAssessment Childddd');
  }

  ngOnChanges() {
    //  console.log('reviewFeedback Data' + JSON.stringify(this.reviewerFeedback));
    if (this.reviewerFeedback !== undefined) {
      if (this.reviewerFeedback.score !== null) {
        this.reviewScore = this.parseScore(this.reviewerFeedback.score);
      } else {
        this.reviewScore = 0;
      }
    }
  }


  createGitUser(assessment) {

    const json = {
      projectName: assessment.technical[0].usecaseTitle + assessment.user.adId,
      userName: assessment.user.adId,
      email: assessment.user.email,
      userInfoMstrId: assessment.user.userInfoMstrId,
      eventDtlsId: assessment.eventDtlsId,
      skillTrackMstrId: assessment.technical[0].skillTrackMstrId
    };

    this.authService.getCreateGITuser(json).then(resp => {
      console.log(resp, 'response');
      this.resp = resp;
    }).catch(err => {
      console.log(err, 'error Response');
    });

  }

  getSubmitForReview(assessment) {

    this.authService.getSubmitForReview(assessment.eventDtlsId, assessment.technical[0].id).then(resp => {
      console.log(resp);
      if (resp.status) {
        if (resp.status === 'error') {

        } else {
          this.isResult = true;
          this.isunderReview = true;
          this.isDetails = false;
        }
      } else {
        this.isResult = true;
        this.isunderReview = true;
        this.isDetails = false;
      }
    }).catch(err => {
      console.log(err);
    });
  }

  getUserGitDetails(assessment) {
    this.authService.getGITuserDetails(assessment.eventDtlsId).then(resp => {

      console.log(resp, 'response');
      console.log(resp.pgMembers[0].username, 'userName');

      this.resp.createUser.username = resp.pgMembers[0].username;
      this.resp.createRepo.web_url = resp.cloneUrl;

    }).catch(err => {
      console.log(err, 'error Response');
    });

  }

  getUserSonarReport(assessment) {
    this.authService.getUserSonarReport(assessment.eventDtlsId).then(resp => {
      console.log(resp, 'response');
      if (resp.url) {
        this.sonarReport = resp;
        this.isRoport = false;
        this.isDetails = true;
        this.isSubmitEnabled = true;
      } else {
        this.isSonarQReport = false;
        this.isDetails = true;
        this.isRoport = false;
        // this.toastr.error('Report not generated...');
      }
    }).catch(err => {
      console.log(err, 'error Response');
    });
  }

  viewReport(url) {
    console.log(this.sonarReport, 'sonarReport');
    window.open(this.sonarReport.url, '_blank');
  }


  /*STATUS CHECKING FUNCTION*/
  checkStatus(assessment) {
    console.log(assessment, 'businesss Assessment');
    if (assessment) {
      if (assessment.assessmentStatus === STEP_STATUS.ASSIGNED) {
        this.isRoport = true;
        this.isDetails = false;
        this.isResult = false;
        this.isStart = true;
      } else if (assessment.assessmentStatus === STEP_STATUS.INPROGRESS) {
        this.assessmentInit(assessment);
      } else if (assessment.assessmentStatus === STEP_STATUS.REVIEW_PENDING) {
        this.InitAssessmentComplete();
        this.isunderReview = true;
      } else if (assessment.assessmentStatus === STEP_STATUS.UNDER_REVIEW) {
        this.InitAssessmentComplete();
        this.isunderReview = true;
      } else if (assessment.assessmentStatus === STEP_STATUS.REVIEW_COMPLETED) {
        this.InitAssessmentComplete();
        this.reviewComplete = true;
      } else if (assessment.assessmentStatus === STEP_STATUS.REVIEW_PASSED) {
        this.InitAssessmentComplete();
        this.reviewComplete = true;
      } else if (assessment.assessmentStatus === STEP_STATUS.REVIEW_REJECTED) {
        this.InitAssessmentComplete();
        this.reviewComplete = true;
      } else if (assessment.assessmentStatus === STEP_STATUS.DONE) {
        this.InitAssessmentComplete();
        this.isDone = true;
      } else {
        return true;
      }
    } else {
    }
  }

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

  needHelpR() {
    this.reports = !this.reports;
  }

  generateRoport(assessment) {
    this.getUserSonarReport(assessment);
  }
  assessmentComplete(assessment) {
    this.updateStatus(assessment, STEP_STATUS.REVIEW_PENDING);
    this.getSubmitForReview(assessment);
  }

  assessmentStart(assessment) {
    this.isStart = false;
    this.isResult = false;
    this.isDetails = true;
    this.createGitUser(assessment);
    this.updateStatus(assessment, STEP_STATUS.INPROGRESS);
  }

  /*UPDATE ASSESSMENT STATUS FUNCTION*/
  updateStatus(assessment, status) {
    this.authService.updateAssessmentStatus(assessment.eventDtlsId, status).then(response => {
      console.log(response);
    }).catch(err => {
      console.log(err);
    });
  }

  /*Init load function*/
  assessmentInit(assessment) {
    this.isStart = false;
    this.isResult = false;
    this.isDetails = true;
    this.getUserGitDetails(assessment);
  }

  InitAssessmentComplete() {
    this.isResult = true;
    this.isDetails = false;
  }

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

  getReviewComments() {
    let comments = '';
    if (this.reviewerFeedback !== undefined) {
      comments = this.reviewerFeedback['comments'];
    }
    return comments;
  }

  getReviewAvatar() {
    if (this.reviewerFeedback !== undefined) {
      if (this.reviewerFeedback.feedback.length === 0) {
        return true;
      }
    } else {
      return false;
    }
  }

  gotoGitUrl(url) {
    window.open(url);
  }

}
