
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 {Location} from "@angular/common";
import {AssessmentHttpService} from "../../services/assessment/assessment-http.service";
import {CapabilityTransformationHttpService} from "../../services/capability-transformation/capability-transformation-http.service";
import { UserInfo } from 'app/models/userInfo.model';
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 { environment } from '../../../environments/environment.dev';

@Component({
  selector: 'app-coach-employee-details',
  templateUrl: './coach-employee-details.component.html',
  styleUrls: ['./coach-employee-details.component.scss']
})

export class CoachEmployeeDetailsComponent implements OnInit {

  UserID :any
  CourseID: any
  userData: any;
  private sub: any;
  private $destroyed = new Subject();
  private currentUser: UserInfo;
  selectedTab = 0;
  role: RoleNames = RoleNames.Coach;
  course: Course;
  environment: String[] = ['Java', 'Eclipse', 'Node JS'];

  assessment: Assessment = new Assessment();
  employeeData = false
  imageUrl: string = environment.imageUrl;
  constructor(private navBarService: NavBarService,
             // private employeeNavBarService: EmployeeNavBarService,
              private authService: AuthService,
              private assessmentService: AssessmentHttpService,
              private coursesHttpService: CapabilityTransformationHttpService,
              private localStorageService: LocalStorageService,
              private router: Router,
              private route: ActivatedRoute,
              private location: Location) {
    this.navBarService.setCurrentPage(2);

    this.route.params.subscribe(params =>
      this.UserID = params['userId']
    )
    this.route.params.subscribe(params =>
      this.CourseID = params['courseId']
    )

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

      this.authService.getEmployeeTestDetails(this.UserID.toString()).then( responseData => {
        responseData = responseData.pgUserInfoDtlsList[0];
        console.log(responseData);
        if (!responseData.pgExperienceLevel) {

          responseData.pgExperienceLevel = {experienceLevel: ''}
        }
        if (!responseData.jobId) {

          responseData.jobId = ''
        }
        this.userData = responseData;
      })

      const courseId: number = this.CourseID;
      if (courseId) {
        this.coursesHttpService.getCourseByCourseId(courseId).subscribe(response => {
          this.course = response.courseDetails;
          this.assessmentService.getEmployeeCourseAssessment( this.UserID, this.course.courseDetailsId)
            .subscribe(
              (assessment: EmployeeCourseAssessmentResponse) => {
                console.log(assessment,'Assessment Detailssssss')
                const adId = assessment.adId;
                this.course.assessmentDetails = assessment;
                this.assessment.setEmployeeAssessment(this.course.assessmentDetails);
                this.authService.getAllSkill().then((skills: SkillsResponse) => {
                  const skillTrackDtls = skills.pgSkillTrackDtlsList.filter(skill => skill.skillTrackDtlsId === this.course.skillTrackId)[0] as PgSkillTrackDtlsList;
                  // Get skillTrackMstrId by skillTrackDtlsId obtained from the skillTrackId course field
                  if (skillTrackDtls) {
                    this.environment = [skillTrackDtls.pgSkillTrackMstr.skillTrackName, skillTrackDtls.pgSkillCategory.skillCategoryName, skillTrackDtls.skillTrackDtlsName];
                    this.authService.getAllAwsDirectoryBundle().then((dirs: DirectoryBundleResponse) => {
                      const awsDirectoryBundle = dirs.pgAwsDirectoryBundleList.filter( dir => dir.pgSkillTrackMstr.skillTrackMstrId === skillTrackDtls.pgSkillTrackMstr.skillTrackMstrId)[0] as PgAwsDirectoryBundleList;
                      if (awsDirectoryBundle) {
                        this.assessment.technicalUsecase.sandboxDir = {
                          sname: adId,
                          sbundleId: awsDirectoryBundle.bundleId,
                          sdirId: awsDirectoryBundle.directoryId
                        };
                        this.assessment = Object.assign(new Assessment(), this.assessment);
                      }
                    });
                  }
                });
                this.employeeData = true;
              }
            );
        });
      }


    }
  }


  ngOnInit() {
   // this.employeeNavBarService.setCurrentPage(5);
    this.currentUser = this.localStorageService.get('userInfo') as UserInfo;
    this.sub =  this.route.params.subscribe(params => {
      if (params['id'] !== undefined) {
        this.selectTab(+params['id']);
      }
    });
  }

  selectTab(selectedTab: number) {
    this.selectedTab = selectedTab;
    console.log(this.selectedTab,'selectedTab')
   // this.location.go('/employee/dashboard/assessment/' + selectedTab);
  }

  ngOnDestroy() {
    this.$destroyed.next();
    this.$destroyed.complete();
  }
}
