import { Component, HostListener,Input,OnInit} from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { LocalStorageService } from 'angular-2-local-storage';
import {Router, ActivatedRoute} from '@angular/router';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import {forEach} from "@angular/router/src/utils/collection";
import _ from "lodash";
import {NavBarService} from "../coach.service";

import { Subject } from 'rxjs/Rx';
import { Course } from 'app/models/capability-transformation/course-list.model';
import { Roles, RoleNames } from 'app/models/auth/auth.model';
import { SkillsResponse, PgSkillTrackDtlsList } from 'app/models/skills.model';
import { DirectoryBundleResponse, PgAwsDirectoryBundleList } from 'app/models/directory-bundle-model';
import { Assessment } from 'app/models/assessment/assessment.model';
import { ProgressStateSetupWorkspaceService } from 'app/employee/employee-dashboard/employee-sandbox/progress-state-setup-workspace.service';
import { EmployeeCourseAssessmentResponse } from 'app/models/assessment/employee-assessment.model';
import {DomSanitizer} from "@angular/platform-browser";
import {CourseListTabs, createCourseListTabs} from "../../models/capability-transformation/course-list.model";
import { environment } from '../../../environments/environment.dev';


@Component({
  selector: 'app-course-employecomponent',
  templateUrl: './course-employecomponent.component.html',
  styleUrls: ['./course-employecomponent.component.scss']
})
export class CourseEmployecomponentComponent implements OnInit {

  UserID : any;
  candt : any
  PGMCandidateDtls = false;
  masterSkills: any[];
  userData:{
    userInfoDtlsId: string,
    adId: string,
    fullName:string,
    email:string,
    location:string,
    profileName:string,
    profileId:string,
    linkedInId:string,
    googleId:string,
    facebookId:string,
    city:string,
    country:string;
    otherDesc:string,
    userInfoMstr :{
      firstNm:string,
      fullName:string,
      email:string,
      status: any,
      userInfoMstrId: string,
      pgLocation:{
        city:string
      }
      pgUserSkillMapping:[{
        pgSkillTrackDtls:{
          pgSkillTrackMstr:{
            description:string,
            skillTrackName:string
          },
          description:string,
        },
      }]
    },
    experienceLevel:string,
    emailId:string,
    addressLine1:string,
    jobId:string,
    alternativeEmail:string,
    contactNumber:string,
    alternativeContact:string,
    governmentId:string,
  }
  masterSkillIds= [];
  skillDetails: any[];

  empStatus = false
  courses: any = [];

  coursesLists: CourseListTabs = createCourseListTabs();

  courseListNames = [];
  imageUrl: string = environment.imageUrl;
  constructor(protected localStorageService: LocalStorageService,
              private AuthService:AuthService,
              private router:Router,
              private route:ActivatedRoute,
              private toastr: ToastsManager,
              private navBarService: NavBarService,
              public sanitizer: DomSanitizer) {

    this.navBarService.setCurrentPage(2);
    this.route.params.subscribe( params =>
      this.UserID = params['id']
    )

    if(!this.UserID || this.UserID == null) {
      this.router.navigate(['coach/landing']);
    }
    else {

      this.AuthService.getEmployeeTestDetails(this.UserID).then(resp=>{
        console.log(resp,'responseDataaaaa')
        resp =resp.pgUserInfoDtlsList[0];
        var response = resp;
        this.userData = resp;


        if(resp.pgExperienceLevel){
        }
        else {
          resp.pgExperienceLevel = {experienceLevel : ''}
        }

        this.candt = resp;

        this.PGMCandidateDtls = true
        this.masterSkills = _.uniqBy(this.userData.userInfoMstr.pgUserSkillMapping, 'pgSkillTrackDtls.pgSkillTrackMstr.description');

        this.masterSkills.forEach(usrSkills => {
          this.masterSkillIds.push(usrSkills.pgSkillTrackDtls.pgSkillTrackMstr.skillTrackMstrId);
        });
        console.log(this.masterSkillIds,'MasterSkillIdsssss');

        this.empStatus = true;
      }).catch(err => {

        console.log(err,'error')
        // this.router.navigate(['TA'])
      })


      this.AuthService.getEmployeeCourses(this.UserID).then(resp => {

        console.log(resp,'getEmployeeCourses');

        resp = resp.userCourseMapping;

        Object.keys(resp
        ).forEach(courseListTypeName => {
          if (resp[courseListTypeName].length > 0) {
            console.log(courseListTypeName,'courseListTypeName')
            this.coursesLists.all[courseListTypeName].courses = resp[courseListTypeName].map(course => {
              return Object.assign(course.pgCourseDetails, course);
            }) as Course[];
          }
        });
        this.courseListNames = Object.keys(this.coursesLists.all).map( courseListName => this.coursesLists.all[courseListName].name);

        /* this.coursesLists.all.Open.courses.forEach(course => {
           if(course.recommended == true){
         //    this.coursesLists.all.Recommended.courses.push(course)
           }
         })*/

        /*  TODO DELETE THE WISHLIST FOR PGM FLOW FOR PARTICULAR EMPLOYEE */
        //  delete  this.coursesLists.all.Open;


        console.log(this.coursesLists.all);

      //  this.coursesLists.all[0] = coursesLists.all.Assessment


      }).catch(err => {
        console.log(err,'Error Calling this method');
      })


    }


  }

  ngOnInit() {
  }

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

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

}
