import {Component, Input, OnInit, ViewEncapsulation} from '@angular/core';
import {LearnigPathDetailsComponent} from "../../../../employee/employee-dashboard/learnig-path-details/learnig-path-details.component";
import {EmployeeHttpService} from "../../../../services/employee/employee-http.service";
import { NavBarService } from 'app/pgm/pgm.service';
import { Router, ActivatedRoute } from '@angular/router';
import {AuthService} from "../../../../services/auth.service";
import {LocalStorageService} from "angular-2-local-storage";
import _ from 'lodash';
import { environment } from '../../../../../environments/environment.dev';
@Component({
  selector: 'app-pgm-learning-path-details',
  templateUrl: './pgm-learning-path-details.component.html',
  styleUrls: ['./pgm-learning-path-details.component.scss'],
})
export class PgmLearningPathDetailsComponent implements OnInit {
  @Input() learningPathDetails: any;
  @Input() detailsType: string;
  empData: any;
  courseCollection: any;
  assessmentCollection: any;
  learningPathId: any;
  imagelink: string;
  skills: any;
  imageUrl: string = environment.imageUrl;
  constructor(private employeeHttpService: EmployeeHttpService,
              protected localStorageService: LocalStorageService,
              private navBarService: NavBarService,
              private route: ActivatedRoute,
              private AuthService: AuthService) {
    this.getAllSkills();
    this.empData = this.localStorageService.get('userInfo');
    this.courseCollection = [];
    this.assessmentCollection = [];
    this.route.params.subscribe(params => {
      if (params['id'] !== undefined) {
        this.learningPathId = params['id'];
      }
    });
  }

  ngOnInit() {
    this.imagelink = environment.imageUrl+'/assets/img/employee/newdashboard/';
    this.navBarService.setCurrentPage(7);
    if (this.learningPathId) {
      this.learningPathCardDetails(this.learningPathId);
    } else {
      this.getAllRelatedCourse();
      this.getAllRelatedAssessment();
    }

  }
  learningPathCardDetails(lpId) {
    const id = String(lpId) + '/' + String(this.empData.userInfoMstrId);
    console.log(id);
    // console.log(courseId);
    // this.showCourseSelection = true;
    // this.showArow = true;
    // this.loader = true;
    // this.getBackErrow.emit(true);
    this.employeeHttpService.getLearningPathCardDetails(id).subscribe(response => {
      console.log(response);
      this.learningPathDetails = response.learningPath;
      this.getAllRelatedDashboardAssessment();
      this.getAllRelatedDashboardCourse();
      // this.loader = false;
    }, (error) => {
      console.log(error);
    });
  }
  getAllRelatedDashboardAssessment() {
    const id = String(this.empData.userInfoMstrId) + '/' + String(this.learningPathDetails.userLpMapId);
    this.AuthService.getEmployeeAssessmentStatus(id).then(resp => {
      const records = resp;
      records.forEach(element => {
        if (element.assessmentStatus === 'REVIEW_PENDING') {
          element.assessmentStatus = 'INPROGRESS';
        }
      });
      if (records) {
        this.filterdata(records);
      }
    }).catch(err => {
      console.log(err);
    });
  }
  filterdata(records) {
    console.log(records);
    // this.getCountAssessment(records);
    this.assessmentCollection.primaryAssList = records.filter(obj => obj.eventMstrId === 1);
    this.assessmentCollection.technicalAssList = records.filter(obj => obj.eventMstrId === 2);
    this.assessmentCollection.businessAssList = records.filter(obj => obj.eventMstrId === 3);
  }
  // getAllRelatedDashboardCourse() {
  //   const id = String(this.empData.userInfoMstrId) + '/' + String(this.learningPathDetails.id);
  //   this.AuthService.getEmployeeAllCourse(id).then(resp => {
  //     this.courseCollection = resp.userCourseMapping.Open;
  //     console.log(resp);
  //   }).catch(err => {
  //     console.log(err);
  //   });
  // }
  getAllRelatedDashboardCourse() {
    const id = String(this.empData.userInfoMstrId) + '/' + String(this.learningPathDetails.id);
    this.AuthService.getEmployeeAllCourse(id).then(resp => {
      const courserecords = resp.userCourseMapping;
      if (courserecords['In Progress'] !== undefined) {
        const inProgressCourse = courserecords['In Progress'].length > 0 && courserecords['In Progress'].map(obj => this.courseCollection.push(obj));
      }
      if (courserecords.Completed !== undefined) {
        const completedCourse = courserecords.Completed.length > 0 && courserecords.Completed.map(obj => this.courseCollection.push(obj));
      }
      if (courserecords.Open !== undefined) {
        const openCourse = courserecords.Open.length > 0 && courserecords.Open.map(obj => this.courseCollection.push(obj));
      }
      if (courserecords.Overdue !== undefined) {
        const overdueCourse = courserecords.Overdue.length > 0 && courserecords.Overdue.map(obj => this.courseCollection.push(obj));
      }

      // this.allstatuscourserecordscopy = [...this.allstatuscourserecords];
      // this.AllcourserecordsCount = this.allstatuscourserecords.length;
      // this.getCount(this.allstatuscourserecords);
      console.log(this.courseCollection);
    }).catch(err => {
      console.log(err);
    });
  }

  getAllRelatedCourse() {
    const courseId = _.map(this.learningPathDetails.courseIds, 'id');
    let idAsString;
    courseId.forEach((element, i) => {
      // idAsString += String(element);
      if (i > 0) {
        idAsString += ',' + String(element);
      } else {
        idAsString = String(element);
      }

    });
    this.employeeHttpService.getLearningPathCourseDetails(idAsString).subscribe(response => {
      this.courseCollection = response.courseDetails;
      console.log(response);
    }, (error) => {

    });
    console.log(courseId);
  }

  getAllRelatedAssessment() {
    const assessmentIdCollection = {
      primaryAssIds: _.map(this.learningPathDetails.primaryAssIds, 'id'),
      technicalAssIds: _.map(this.learningPathDetails.technicalAssIds, 'id'),
      businessAssIds: _.map(this.learningPathDetails.businessAssIds, 'id'),
    };
    this.employeeHttpService.getLearningPathAssessmentDetails(assessmentIdCollection).subscribe(response => {
      console.log(response);
      this.assessmentCollection = response;
    }, (error) => {
      console.log(error);
    });
  }
  getAllSkills() {
    this.AuthService.getAllSkillTrackMstr().then(resp => {
      //  console.log(resp, 'getAllSkills');
      this.skills = resp.pgSkillTrackMstr;
    }).catch(err => {
      console.log(err);
    });
  }
  getcolor(x) {
    const status = x;
    let color = '';
    switch (status) {
      case 'Overdue':
        color = '#F97787';
        return color;
      case 'To Do':
        color = '#68CBFF';
        return color;
      case 'In Progress':
        color = '#F8C580';
        return color;
      case 'Under Review':
        color = '#9B9B9B';
        return color;
      case 'Review Completed':
        color = '#8BC2AC';
        return color;
      case 'Done':
        color = '#15CC83';
        return color;
      default:
        color = '#ffffff';
        return color;
    }
  }
  getStatusImageDetails(imgName, statusName) {
    let statusText = '';
    let img = '';
    switch (imgName) {
      case 'OVERDUE':
        statusText = 'Overdue';
        img = 'Overdue';
        return statusName === 'getStatusName' ? statusText : img;
      case 'TODO':
        statusText = 'To Do';
        img = 'To Do';
        return statusName === 'getStatusName' ? statusText : img;
      case 'INPROGRESS':
        statusText = 'In Progress';
        img = 'InProgress';
        return statusName === 'getStatusName' ? statusText : img;
      case 'DONE':
        statusText = 'Done';
        img = 'Done';
        return statusName === 'getStatusName' ? statusText : img;
    }
  }
}
