import { Component, OnInit } from '@angular/core';
import { EmployeeWizardService } from '../employee-wizard.service';
import { EmployeeCoursesSliderComponent } from '../../shared/employee-course-card/employee-course-card.component';
import { EmployeeSearchBarComponent } from '../../shared/employee-search-bar/employee-search-bar.component';
import { CapabilityTransformationHttpService } from 'app/services/capability-transformation/capability-transformation-http.service';
import { Subject } from 'rxjs/Rx';
import { LocalStorageService } from 'angular-2-local-storage';

@Component({
  selector: 'app-employee-wizard-ct2',
  templateUrl: './employee-wizard-ct2.component.html',
  styleUrls: ['./employee-wizard-ct2.component.css']
})
export class EmployeeWizardCT2Component implements OnInit {
  private $destroyed = new Subject();

  currentUser: any;
  courseCategories = [];
  courses: any = [];
  selectedCourses: any = [];
  showGoto: any;

  constructor(
    private employeeWizardService: EmployeeWizardService,
    private coursesHttpService: CapabilityTransformationHttpService,
    private localStorageService: LocalStorageService
  ) { }

  ngOnInit() {
    this.currentUser = this.localStorageService.get('userInfo') || {};
    this.employeeWizardService.setCurrentPage(1);
    this.employeeWizardService.categories.takeUntil(this.$destroyed).subscribe((categoriesBySkill: any) => {
      this.courseCategories = categoriesBySkill.map((category) => {
        return { courseCategory: category.title, skillId: category.parentSkill.id };
      });
      this.coursesHttpService
      .getCourseByCategoryAndSkill(this.courseCategories)
      .subscribe(
      (courses: any) => {
        this.courses = courses.map(course => {
          course.selected = false;
          return course;
        });
      });
    });
  }

  selectCourse(course: any = {}) {
    //console.log(this.selectedCourses, course);
    course.selected = !course.selected;
    if(course.selected == true){
    this.selectedCourses.push({ userId: this.currentUser.userInfoMstrId, courseId: course.courseDetailsId, assessment: course.assessment });
    console.log('true', this.selectedCourses);
    }
    else if(course.selected == false){
      this.selectedCourses.splice(this.selectedCourses.indexOf(course.selected), 1);
      //this.selectedCourses = this.selectedCourses.filter(item => item.courseId !== course.courseDetailsId);
    console.log('false', this.selectedCourses);
    }
    this.employeeWizardService.setCourses(this.selectedCourses);
  }
}
