import { Component, HostListener,OnInit} from '@angular/core';
import { AuthService } from '../../../services/auth.service';
import { LocalStorageService } from 'angular-2-local-storage';
import {Router} from '@angular/router';
import {NavigationService} from '../../../services/navigation/navigation.service'
import {WorkspaceService} from '../../../services/workspace.service';
import { NavBarService } from '../../coach.service';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import {CoachCandidateEvaluationService} from 'app/coach/shared/service/coach-candidate-evaluation.service';
import { environment } from '../../../../environments/environment.dev';
@Component({
  selector: 'app-coachcandidatedashboard',
  templateUrl: './coachcandidatedashboard.component.html',
  styleUrls: ['./coachcandidatedashboard.component.css']
})
export class CoachcandidatedashboardComponent implements OnInit {


  
  userIcon='';
  calledclass = '';
  Id= 95;
  public registeredCandidates;
  public listCandidates;
  Location="Chennai";
  SkillId=1;
  filterActive = 'all';
  img_path = environment.imageUrl+'/assets/img/Taflow/';
  loader = true;
  CFLANDING= false;
  name : string
  fullName : string
  courseFilter: any = { infoDetails: { fullName: '' }};

  empData: {
    accessToken: any,
    loginType: any,
    fullName: any,
    firstName: any,
    userInfoMstrId: any,
    roleName: any,
    roles: any,
    skills: any,
    location: any
  };

  primarySkillCandidateCount = 0;
  techUseCaseCandidateCount = 0;
  bussUseCaseCandidateCount = 0;
  primarySkillEvalCandidates=[];
  techUseCaseEvalCandidate=[];
  bussUseCaseEvalCandidate=[];
  filterprimarySkillEvalCandidates=[];
  filtertechUseCaseEvalCandidate=[];
  filterbussUseCaseEvalCandidate=[];

  primaryFilter= [
    {
      "id": "all",
      "name": "All"
    },
    {
      "id": "inProgress",
      "name": "In Progress"
    },
    {
      "id": "notScheduled",
      "name": "Schedule"
    },
    {
      "id": "evaluation",
      "name": "Evaluation"
    },
    {
      "id": "completed",
      "name": "Completed"
    }
  ];
  filter = [
    {
      "id": "all",
      "name": "All"
    },
    {
      "id": "inProgress",
      "name": "In Progress"
    },
    {
      "id": "evaluation",
      "name": "Evaluation"
    },
    {
      "id": "completed",
      "name": "Completed"
    }
  ];

  constructor(protected localStorageService: LocalStorageService,
              private AuthService:AuthService,
              private router: Router,
              private navigate:NavigationService,
              private navBarService: NavBarService,
              private toastr: ToastsManager,
              private WorkspaceService:WorkspaceService,
              private CoachCandidateEvaluationService:CoachCandidateEvaluationService) {

    this.navBarService.setCurrentPage(0);
    this.CoachCandidateEvaluationService.setProperActiveTab(0);
    var empData
    empData = this.localStorageService.get('userInfo');
    this.empData = empData
    console.log(this.empData,"userInfo Detailssssss")
    if(window.location.origin == 'http://localhost:8080'){
      this.name = 'UDAYANI'
      this.fullName = 'UDAYANI CHAVA'
      this.getCoachCandidates(11);
      //this.getCoachCandidates(13,"Chennai", 1);
    }
    else {
      if (this.empData['firstName']) {
        this.name = this.empData['firstName'];
        this.fullName = this.empData['fullName'];
      }
      else {
        this.name = ''
        this.fullName = ''
      }
      //this.getCoachCandidates(this.empData.userInfoMstrId,this.empData.location, this.empData.skills[0]);
      this.getCoachCandidates(this.empData.userInfoMstrId);
    }
  }

  ngOnInit() {
  }
  getCoachCandidates(Id){
    console.log(Id,"All Req Body Dataaaaaaaa");
    this.AuthService.getAllCandidateCoach({coachId:Id}).then( responseData => {

      console.log(responseData,responseData.length);

      this.registeredCandidates = responseData;
      this.CFLANDING = true;
      this.loader = false;
      this.listCandidates = this.registeredCandidates;

      this.primarySkillEvalCandidates = [];
      this.techUseCaseEvalCandidate = [];
      this.bussUseCaseEvalCandidate = [];



      for(var value of this.registeredCandidates){

        if(value.eventDetails.length != 0) {

          if ( value.infoDetails.userInfoMstr.status == 'METTL_SCHEDULED' ||
            value.infoDetails.userInfoMstr.status == 'METTL_REVIEW' ||
            value.infoDetails.userInfoMstr.status == 'METTL_COACH_ASSIGNED' ||
            value.infoDetails.userInfoMstr.status == 'METTL_APPROVED' ||
            value.infoDetails.userInfoMstr.status == 'METTL_REJECTED' ) {
            console.log('value.infoDetails.userInfoMstr.status',value.infoDetails.userInfoMstr.status)
            this.primarySkillEvalCandidates.push(value);

            this.primarySkillCandidateCount = this.primarySkillEvalCandidates.length;

          }
          else if ( value.infoDetails.userInfoMstr.status == 'COACH_ASSIGNED' ||
            value.infoDetails.userInfoMstr.status == 'MINI_PG_STARTED'  ||
            value.infoDetails.userInfoMstr.status == 'MINI_PG_COMPLETED' ||
            value.infoDetails.userInfoMstr.status == 'MINI_PG_COACH_APPROVED' ||
            value.infoDetails.userInfoMstr.status == 'MINI_PG_COACH_REJECTED' ) {

            this.techUseCaseEvalCandidate.push(value);
            this.techUseCaseCandidateCount = this.techUseCaseEvalCandidate.length

          }
          else if (value.infoDetails.userInfoMstr.status == 'MAIN_PG_COACH_ASSIGNED' ||
            value.infoDetails.userInfoMstr.status == 'MAIN_PG_MANAGER_APPROVED' ||
            value.infoDetails.userInfoMstr.status == 'MAIN_PG_COMPLETED' ||
            value.infoDetails.userInfoMstr.status == 'MAIN_PG_MANAGER_REJECTED' ||
            value.infoDetails.userInfoMstr.status == 'MAIN_PG_COACH_REVIEWED') {

            this.bussUseCaseEvalCandidate.push(value);
            this.bussUseCaseCandidateCount = this.bussUseCaseEvalCandidate.length

          } else {
            let noCandidatesRecord = "No Candidates Record found"
          }
        }
      }

      this.filterprimarySkillEvalCandidates = this.primarySkillEvalCandidates;
      this.filtertechUseCaseEvalCandidate = this.techUseCaseEvalCandidate;
      this.filterbussUseCaseEvalCandidate = this.bussUseCaseEvalCandidate;

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

  }

  callMe(){
    this.userIcon = 'logout';
    console.log('callme');

  }

  Initials(cString) {
    if(cString) {
      return cString.replace(/\W*(\w)\w*/g, '$1').toUpperCase()
    }
  }

  getSubstring(text){
    if(text) {
      return text.substring(0, 3);
    }
  }


  getTrim(text){

    return text.substring(6);

  }


  goToCandidateDetails($event){
    this.router.navigate(['coach/candidateDetails',$event]);

  }
  scheduleMettl($event){
    this.router.navigate(['coach/candidateDetails',$event]);
  }



  All(){
    this.filterActive = 'all';

    this.registeredCandidates = this.listCandidates;
  }

  New(){
    this.filterActive = 'new';

    this.registeredCandidates = this.listCandidates.filter(function(item, index, array){

      return (item.infoDetails.userInfoMstr.status == 'COACH_ASSIGNED'
      || item.infoDetails.userInfoMstr.status == 'MINI_PG_COACH_ASSIGNED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_COACH_ASSIGNED'
      || item.infoDetails.userInfoMstr.status == 'METTL_APPROVED');

    });
  }
  inProgress(){
    this.filterActive = 'inProgress';

    this.registeredCandidates = this.listCandidates.filter(function(item, index, array){

      return (item.infoDetails.userInfoMstr.status == 'MINI_PG_STARTED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_STARTED'
      || item.infoDetails.userInfoMstr.status == 'COACH_ASSIGNED'
      || item.infoDetails.userInfoMstr.status == 'MINI_PG_COACH_ASSIGNED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_COACH_ASSIGNED' );

    });
  }
  coachEvaluation(){
    this.filterActive = 'coachEvaluation';

    this.registeredCandidates = this.listCandidates.filter(function(item, index, array){

      return (item.infoDetails.userInfoMstr.status == 'MINI_PG_COMPLETED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_COMPLETED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_COMPELTED' || item.infoDetails.userInfoMstr.status == 'MAIN_PG_COMPELTED');

    });

  }

  completedTestnEvaluation() {
    this.filterActive = 'completed';

    this.registeredCandidates = this.listCandidates.filter(function (item, index, array) {

      return (item.infoDetails.userInfoMstr.status == 'MINI_PG_COACH_APPROVED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_COACH_REVIEWED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_COMPLETED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_STARTED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_COACH_ASSIGNED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_MANAGER_APPROVED'
      || item.infoDetails.userInfoMstr.status == 'MAIN_PG_MANAGER_REJECTED');

    });
  }

  filterBusiness(filterId){
    console.log("filterbusiness",filterId);
    if(filterId == 'all' ){
      this.bussUseCaseEvalCandidate = this.filterbussUseCaseEvalCandidate;
    }
    else if(filterId == 'inProgress'){
      this.bussUseCaseEvalCandidate = this.filterbussUseCaseEvalCandidate.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'MAIN_PG_STARTED' ||  item.infoDetails.userInfoMstr.status == 'MAIN_PG_COACH_ASSIGNED');
      });
    }
    else if(filterId == 'evaluation'){
      this.bussUseCaseEvalCandidate = this.filterbussUseCaseEvalCandidate.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'MAIN_PG_COMPLETED' );
      });
    }
    else if(filterId == 'completed'){
      this.bussUseCaseEvalCandidate = this.filterbussUseCaseEvalCandidate.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'MAIN_PG_MANAGER_APPROVED' || item.infoDetails.userInfoMstr.status == 'MAIN_PG_COACH_REVIEWED' || item.infoDetails.userInfoMstr.status == 'MAIN_PG_MANAGER_REJECTED');
      });
    }
  }

  filterTechnical(filterId){
    console.log("filterTechnical",filterId);
    if(filterId == 'all' ){
      this.techUseCaseEvalCandidate = this.filtertechUseCaseEvalCandidate;
    }
    else if(filterId == 'inProgress'){
      this.techUseCaseEvalCandidate = this.filtertechUseCaseEvalCandidate.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'MINI_PG_STARTED' || item.infoDetails.userInfoMstr.status == 'COACH_ASSIGNED');
      });
    }
    else if(filterId == 'evaluation'){
      this.techUseCaseEvalCandidate = this.filtertechUseCaseEvalCandidate.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'MINI_PG_COMPLETED');
      });
    }
    else if(filterId == 'completed'){
      this.techUseCaseEvalCandidate = this.filtertechUseCaseEvalCandidate.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'MINI_PG_COACH_APPROVED' || item.infoDetails.userInfoMstr.status == 'MINI_PG_COACH_REJECTED');
      });
    }


  }
  filterPrimary(filterId){
    console.log("filterPrimary",filterId);
    if(filterId == 'all' ){
      this.primarySkillEvalCandidates = this.filterprimarySkillEvalCandidates;
    }
    else if(filterId == 'notScheduled'){
      this.primarySkillEvalCandidates = this.filterprimarySkillEvalCandidates.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'METTL_COACH_ASSIGNED' );
      });
    }
    else if(filterId == 'inProgress'){
      this.primarySkillEvalCandidates = this.filterprimarySkillEvalCandidates.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'METTL_SCHEDULED' );
      });
    }
    else if(filterId == 'evaluation'){
      this.primarySkillEvalCandidates = this.filterprimarySkillEvalCandidates.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'METTL_REVIEW' );
      });
    }
    else if(filterId == 'completed'){
      this.primarySkillEvalCandidates = this.filterprimarySkillEvalCandidates.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'METTL_APPROVED' || item.infoDetails.userInfoMstr.status == 'METTL_REJECTED' );
      });
    }

  }
  updateCandidateStatus(status, id){

    console.log("updateCandidateStatus",status, id);
    this.AuthService.candidateStatusUpdate({candidateStatus:status,fromId:this.empData.userInfoMstrId,toId:id}).then( responseData => {
      console.log(responseData)
      var responseDataCheck = responseData
      // var resData = responseData['data']

      if(responseData['status'] == 'success'){

        if (status == 'METTL_APPROVED') {
          this.toastr.success("Candidate primary skill evaluation approved ");

        }
        else if(status == 'METTL_REJECTED'){
          this.toastr.success("Candidate primary skill evaluation rejected");
        }

      }
      else {
        console.log(responseData,'Else case Canditates Approval ')
      }

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


  logout(){
    console.log("logout");
    this.localStorageService.clearAll();
    localStorage.clear();
    this.router.navigate(['']);

  }
}
