import { Component, 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 '../../pgm.service';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import {CoachCandidateEvaluationService} from "../../../coach/shared/service/coach-candidate-evaluation.service";
import { environment } from '../../../../environments/environment.dev';

@Component({
  selector: 'app-pgm-coach-dash-board',
  templateUrl: './pgm-coach-dash-board.component.html',
  styleUrls: ['./pgm-coach-dash-board.component.css']
})
export class PgmCoachDashBoardComponent implements OnInit {


  userIcon='';
  calledclass = '';
  Id= 95;
  public registeredCandidates;
  public listCandidates;
  Location="Chennai";
  SkillId=1;
  filterActive = 'all';
  PGMCOACHLANDING= false;
  name : string
  fullName : string
  courseFilter: any = { coachDetails: { 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;
  allCoach=[];
  allInactiveCoach=[];
  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"
    }
  ];
  img_path = environment.imageUrl+'/assets/img/CFlanding/';
  img_path_1= environment.imageUrl+'/assets/img/';

  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(1);

    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 = 'SABASH'
      this.fullName = 'SABASH LS'
      this.getAllCandidate(1,1);
      //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.getAllCandidate(this.empData.location, this.empData.skills[0]);
    }
  }

  ngOnInit() {
  }
  getAllCandidate(Location, SkillId){
    console.log(Location, SkillId,"All Req Body Dataaaaaaaa");

    Promise.all([
      this.AuthService.getCoachesForAssignedCandidates({location:Location, skillId:SkillId}),
      this.AuthService.getAllCoachesForApplication(Location,SkillId)
      ])
      .then(arr => {

    console.log(arr)
    console.log(arr[0])
    console.log(arr[1])

        this.allCoach = arr[0];

    var InActCoach = arr[1];
    var InActCoachList = [];

    for(var i=0;i< InActCoach.length;i++){

      InActCoachList.push({coachDetails:InActCoach[i],infoDetails:[]})
    }


        this.allInactiveCoach = InActCoachList;

        console.log(InActCoachList,'=====>  allInactiveCoach');
        this.PGMCOACHLANDING = true;
        this.listCandidates = this.registeredCandidates;

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



    });

  }

  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);

  }


  goToCoachDetails($event){

    var url ='pgCoachdetails'+'?'+'uuid='+$event

    //window.location.href = window.location.origin+'/'+'pgCoachdetails'+'?'+'uuid='+$event

    this.router.navigate(['pgCoachdetails',$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_COMPLETED');
      });
    }
    else if(filterId == 'evaluation'){
      this.bussUseCaseEvalCandidate = this.filterbussUseCaseEvalCandidate.filter(function(item, index, array){
        return (item.infoDetails.userInfoMstr.status == 'MAIN_PG_COMPLETED' || item.infoDetails.userInfoMstr.status == 'MAIN_PG_COACH_REVIEWED');
      });
    }
    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_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 == 'MINI_PG_COMPLETED');
      });
    }
    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(id){

    console.log("updateCandidateStatus", id);
   this.AuthService.coachStatusUpdate(id.userId, id.status).then( responseData => {
      console.log(responseData)
      var responseDataCheck = responseData
      // var resData = responseData['data']

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

        if (id.status == 'ACTIVE') {
          this.toastr.success("Coach APPROVED ");
        }
        else if(id.status == 'REJECTED'){
          this.toastr.error("Coach REJECTED");
        }
      }
      else {
        console.log(responseData,'Else case Canditates Approval ')
      }

    // window.location.href = window.location.origin+'/'+'pgCoachdetails'+'?'+'uuid='+id.userId

     this.router.navigate(['pgCoachdetails',id.userId]);
    }).catch(err=>{
      console.log(err)
    });
  }


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

  }


}
