import {Component, OnInit, Input, ViewEncapsulation, TemplateRef, ViewChild, Output, EventEmitter} from '@angular/core';
import _ from "lodash";

import {el} from "@angular/platform-browser/testing/src/browser_util";
import {PagerService} from "../../admin/inner-admin-pages/right-admin-panel/user-mgmt/_services/pager.service";
// import {MatPaginator, MatTableDataSource} from '@angular/material';
@Component({
  selector: 'app-candidate-table',
  templateUrl: './candidate-table.component.html',
  styleUrls: ['./candidate-table.component.scss'],
  encapsulation: ViewEncapsulation.None
})

export class CandidateTableComponent implements OnInit {

  @Input() list: any;
  @Input() role: any;
  @Input() activeStatus: any;
  @ViewChild('nameSummaryCell')
  @Output() selectedData = new EventEmitter<object>();
  @Output() approve = new EventEmitter<object>();
  @Output() onSelectCandidate = new EventEmitter<object>();
  @Input() empData: any;
  @Output() selectedEmpForDet = new EventEmitter<any>();
  @Output() onSelectCoach = new EventEmitter<object>();
  @Output() onPrimaryChange = new EventEmitter<object>();
  @Output() onCodingChange = new EventEmitter<object>();
  @Output() onWhiteboardChange = new EventEmitter<object>();
  @Output() onleaderboardChange = new EventEmitter<object>();
  @Output() onfeedBackCC = new EventEmitter<object>();
  @Output() onfeedBackWC = new EventEmitter<object>();
  @Output() terminateWS = new EventEmitter<object>();
  nameSummaryCell: TemplateRef<any>;

  selected = [];
  stsClass = ''
  singleCheck:boolean;
  isRow: number;
  oneSelectedUserDtls = [];
  oneSelectedUser = [];
  candShowDet: any =[];

  statusJson =
    {
      METTL_INITIATED:'Initiated',
      METTL_IN_PROGRESS:'In progress',
      METTL_REVIEW:'Onhold',
      METTL_FAILED:'Rejected',

      METTL_CLEARED:'Awaiting Initiation',
      CODING_CHALLENGE_READY:'Awaiting Initiation',
      CODING_CHALLENGE_INITIATED:'Initiated',
      CODING_CHALLENGE_STARTED:'In progress',
      CODING_CHALLENGE_COMPLETED:'Onhold',
      CODING_CHALLENGE_REJECTED:'Rejected',

      CODING_CHALLENGE_CLEARED:'Discussion in progress',
      WHITEBOARD_READY:'Discussion in progress',
      WHITEBOARD_INITIATED:'Onhold',
      WHITEBOARD_REJECTED:'Rejected',

      WHITEBOARD_CLEARED:'In progress',
      LEADERSHIP_CONNECT_READY:'In progress',
      LEADERSHIP_CONNECT_REJECTED:'Rejected',
      LEADERSHIP_CONNECT_CLEARED:'Selected',
    }

  constructor() {
  }

  ngOnInit() {
    if(this.empData){
      console.log(this.empData);
    }

  }

  rowStyle(num) {
    if ( num % 2 == 0) {
      return 'rowEven'
    }else{
      return 'rowOdd'
    }
  }

  stsClassfunc(data, num){

    if ( num % 2 == 0) {
        return 'rowEven';
    } else {
        return 'rowOdd';
    }
  }

  hideCheck(list,sts){
    if(sts === 'S2'){
      let test = list.filter(obj => obj.userStatus == 'METTL_REVIEW');
      if(test.length === 0){
        return true;
      } else {
        return false
      }
    }
    if(sts === 'S3'){
      let test = list.filter(obj => obj.userStatus === 'METTL_CLEARED' ||  obj.userStatus === 'CODING_CHALLENGE_READY' ||  obj.userStatus === 'CODING_CHALLENGE_COMPLETED');
      if(test.length === 0){
        return true;
      } else {
        return false
      }
    }
    if(sts === 'S4'){
      let test = list.filter(obj => obj.userStatus === 'CODING_CHALLENGE_CLEARED'|| obj.userStatus === 'WHITEBOARD_READY');
      if(test.length === 0){
        return true;
      } else {
        return false
      }
    }
    if(sts === 'S5'){
      let test = list.filter(obj => obj.userStatus === 'WHITEBOARD_CLEARED' || obj.userStatus === 'LEADERSHIP_CONNECT_READY');
      if(test.length === 0){
        return true;
      } else {
        return false
      }
    }
  }



  selectedCandidate(id,status){
    this.approve.emit({userId:id,status:status})
  }

  onSelect({ selected }) {
    console.log('Select Event', selected, this.selected);


  }

  AllChanged (evnt) {
    console.log(evnt)
    this.singleCheck = evnt
    // document.getElementById('allSelect').checked = true;
    // document.getElementById('singleSelect').checked = true;
    this.oneSelectedUser = []

    if (evnt) {
      if (this.activeStatus === 'S2') {
        let list = this.list.filter(obj => obj.userStatus == 'METTL_REVIEW')
        list.forEach(item => {

          this.oneSelectedUser.push(item.userInfoMstrId)

        })
      } else if (this.activeStatus === 'S3') {
        let list = this.list.filter(obj => obj.userStatus == 'METTL_CLEARED' || obj.userStatus == 'CODING_CHALLENGE_READY')
        list.forEach(item => {

          this.oneSelectedUser.push(item.userInfoMstrId)

        })
        this.oneSelectedUserDtls = list
      }else if (this.activeStatus === 'S4') {
        let list = this.list.filter(obj => obj.userStatus == 'CODING_CHALLENGE_CLEARED' || obj.userStatus == 'WHITEBOARD_READY')
        list.forEach(item => {

          this.oneSelectedUser.push(item.userInfoMstrId)

        })
      }else if (this.activeStatus === 'S5') {
        let list = this.list.filter(obj => obj.userStatus == 'WHITEBOARD_CLEARED' || obj.userStatus == 'LEADERSHIP_CONNECT_READY')
        list.forEach(item => {

          this.oneSelectedUser.push(item.userInfoMstrId)

        })
      }else {

      this.list.forEach(item => {

        this.oneSelectedUser.push(item.userInfoMstrId)

      })
    }
  }
    if (this.activeStatus !== 'S3') {

      this.oneSelectedUserDtls = this.list;

    }

    this.onSelectCandidate.emit({userIds:this.oneSelectedUser,userDetails:this.oneSelectedUserDtls})

  }

  singleChanged(evnt,id,data) {
    console.log(evnt)
    if(evnt.target.checked){
      this.oneSelectedUser.push(id)
      this.oneSelectedUserDtls.push(data)

    } else {
      this.oneSelectedUser.splice(this.oneSelectedUser.indexOf(id), 1)
      this.oneSelectedUserDtls.splice(this.oneSelectedUserDtls.indexOf(data.userInfoMstrId), 1)
    }
    console.log(this.oneSelectedUser);
    console.log(this.oneSelectedUserDtls);
    this.onSelectCandidate.emit({userIds:this.oneSelectedUser,userDetails:this.oneSelectedUserDtls})
  }

  disableCheck(status,level){

    if(level === 'S2'){

        if(status === 'METTL_INITIATED'|| status === 'METTL_IN_PROGRESS'|| status === 'METTL_FAILED') {

          return true
        } else {
          return false
        }
    }
  }



  edit(event,data) {
    console.log('Edit into.. ', event);
    this.onSelectCoach.emit({event:event,user:[data]})
  }

  mouseleft(index) {
    this.isRow = 0.5
  }
  over(index) {
    if(this.oneSelectedUser.length == 0) {
      this.isRow = index
    }
  }

  primaryChange(id, status,modal,data){
    this.onPrimaryChange.emit({userId:[id],status:status,modal:modal,data:data})
  }
  InitiateCC(id, status,adId){
    this.onCodingChange.emit({userId:[id],status:status,adId:adId})
  }
  InitiateWC(id, status,modal,data,adId){
    this.onWhiteboardChange.emit({userId:[id],status:status,modal:modal,data:data,adId:adId})
  }
  InitiateLC(id, status,modal,data,adId){
    this.onleaderboardChange.emit({userId:[id],status:status,modal:modal,data:data,adId:adId})
  }

  feedBack(id, status,modal,data,adId){
    this.onfeedBackCC.emit({userId:id,status:status,modal:modal,data:data,adId:adId})
  }
  feedBackWC(id, status,modal,data){
    this.onfeedBackWC.emit({userId:id,status:status,modal:modal,data:data})
  }
  terminateWorkSpace(id,adId){
    this.terminateWS.emit({userId:id,adId:adId})
  }

  showDetail(data){
    console.log("candidate details -- ", JSON.stringify(data));
    console.log(this.empData);
    this.selectedEmpForDet.emit({candidateDetails: data, loggedInUserDetails :this.empData});
  }

}
