import { Component, HostListener,OnInit,Input} 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 {ActivatedRoute} from '@angular/router';
import {DomSanitizer,SafeResourceUrl,} from '@angular/platform-browser';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import {URLSearchParams} from "@angular/http";
import { environment } from '../../environments/environment.dev';

@Component({
  selector: 'app-pgcoachdetails',
  templateUrl: './pgcoachdetails.component.html',
  styleUrls: ['./pgcoachdetails.component.css']
})
export class PgcoachdetailsComponent implements OnInit {
  @Input() overAllFeedback: string='';
  @Input() overAllTechFeedback: string='';


  candt:{ };
  candidateInfo:any;
  candidateMasterdetail:any;
  userInfoData:any;
  candidateResult:any;
  assignCoachResponse:any;
  CandidateData=false;
  UserID :string;
  userIcon='';
  modal=''
  userId='';
  selectedEntry;

  role='technical';
  public fulName:string;

  PG='';
  coaches=[];
  name:string;
  fullName:string;

  testStatus:string;
  registerationCode: string[];
  respData: any;
  userInfoBY : {
    adId : string[]
    email : string[]
    name : string[]
    level : string[]
    skill : string[]
    bundleId : string[]
    directoryId : string[]
  }

  empData:{
    fullName:string,
    userInfoMstrId:any,
    email:string,
    location:string,
    city:string,
    skillId:string,
    skillTrackDtlsId: any,
    skillTrackDtlsName: string,
    skillTrackMstrId: string,
    description: string,
    status: any,
  }


  /*coachData:{
    userInfoDtlsId: string,
    adId: string,
    fullName:string,
    email:string,
    location:string,
    pgSkillTrackDtls:{
      pgSkillTrackMstr:{
        description:string,
        skillTrackName:string
      },
      description:string,
    },
    userInfoMstr :{
      status: any,
      userInfoMstrId: string
      pgLocation: any
    },
    experienceInYrs:string,
    experienceLevel:string,
    emailId:string,
    jobId:string,
    alternativeEmail:string,
    contactNumber:string,
    alternativeContact:string,
    governmentId:string,
  }
*/

coachData = {}
  eventDescDetails =[]
  eventDetails =[]
  coachTechDetails :any

  evaluatingskill='Evaluation Report';

  mainPGacpt:boolean
  objExp:{}
  Candidates=[];
  imageUrl = environment.imageUrl;
  constructor(protected localStorageService: LocalStorageService,
              private AuthService:AuthService,
              private router: Router,
              private route:ActivatedRoute,
              private activeRoute:ActivatedRoute,
              private navigate:NavigationService,
              private WorkspaceService:WorkspaceService,
              private sanitizer:DomSanitizer,
              private toastr: ToastsManager) {


    this.Candidates = [

    ];

    this.coaches = [];

    this.CandidateData=false;

    this.name = '';
    this.fullName = '';
    var empData;
    empData = this.localStorageService.get('userInfo');
    this.empData = empData;
    console.log(this.empData,"userInfo Detailssssss");

    if(window.location.origin == 'http://localhost:8080' || window.location.origin == 'http://localhost'){

      this.name = 'Coach';
      this.fullName = 'Coach Sabash'

     }
     else {
     if (this.empData['firstName']) {
     this.name = this.empData['firstName'];
     this.fullName = this.empData['fullName'];
     }
     else {
     this.name = '';
     this.fullName = ''
     }
     }

    var url_string = window.location.href;
    var url = new URL(url_string);


    //noinspection TypeScriptUnresolvedVariable

    this.route.params.subscribe( params =>
      this.UserID = params['id']
    );
   // this.UserID =  url.searchParams.get("uuid")

    var userDetail = this.localStorageService.get('userInfo');

    this.userInfoData = userDetail;

    this.AuthService.getEmployeeTestDetails(this.UserID).then( responseData => {

      console.log(responseData,'candidates Details');

      this.candt = responseData.pgUserInfoDtlsList[0];

      this.candidateInfo = responseData.pgUserInfoDtlsList[0];

      this.coachData =responseData.pgUserInfoDtlsList[0];
      var coachDetails =responseData.pgUserInfoDtlsList[0]

      if (coachDetails.userInfoMstr.pgUserSkillMapping.length == 0 || coachDetails.userInfoMstr.pgLocation.length == 0) {


      }
      else {

        this.getCoachCandidates(coachDetails.userInfoMstr.userInfoMstrId,coachDetails.userInfoMstr.pgLocation.locationId,coachDetails.userInfoMstr.pgUserSkillMapping[0].pgSkillTrackDtls.pgSkillTrackMstr.skillTrackMstrId)
      }
        console.log(this.candt,'candt Deatilssss')
      console.log(this.coachData,'Coach Deatilssss')

      //this.coachTechDetails = responseData['coachDetails'];


      //this.geteventDescDetails(responseData)


      //this.fulName = responseData.infoDetails.userInfoMstr.fullName;
      this.CandidateData=true;
    }).catch(err=>{
      console.log(err)
    });


    console.log(this.eventDescDetails);

  }


  ngOnInit() {



  }


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

      console.log(responseData,"getAllCoachCandidates");

      this.Candidates = responseData

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

  }


  gocfLanding(){
    this.router.navigate(['pgm/landing']);
  }

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

    this.userIcon = '';

  }

  geteventDescDetails(evntDtls,evntDesc){

    console.log(evntDtls,'event Detailsssssss')
    console.log(evntDesc,'event Desc Detailsssssss')



  }


  getCoachCandidate(Id){

    this.AuthService.getCandidate({userInfoMstrId:Id}).then( responseData => {

      console.log(responseData);

      this.candt = responseData;
      console.log("candidate::");
      console.log(this.candt);

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

  }


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

  }


  replace(cString) {

      return cString.replace('Launch', '');

  }


  getFirstName(fString){
    return fString.split(' ').slice(0, -1).join(' ');
  }




  getSubstring(text){

    return text.substring(0, 3);

  }




  showmodal(  val){
    /*this.PG = pg;
    this.testStatus = type
    if(this.PG == 'assignCoachtoCandidate'){
      this.getCoachesforCandidate("Chennai", 1);
    }*/
    this.modal= val;
  }

  closeModal(modal){
    this.modal = '';
  }
  setRole(Role){
    this.role = Role;
  }
  submitCoachtoCandidateAssignment(){
    this.modal = '';
  }
  onSelectionChange(entry) {
    //noinspection TypeScriptUnresolvedFunction
    this.selectedEntry =Object.assign({}, this.selectedEntry, entry);
  }

  getCoachesforCandidate(Location,SkillId){
    this.coaches = []
    this.AuthService.getAllCoachesforCandidate({location:Location, skillId:SkillId}).then(responseData =>{
      for(var value of responseData.usrInfoMstr){
        if(value.fullName !== null){
          this.coaches.push(value);
        }
      }
    }).catch(err=>{
      console.log(err)
    });
  }

  assignCoach(coach, playground, modal){

  }


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

  }

}
