import { Location } from '@angular/common';
import { UserInfo } from 'app/models/userInfo.model';
import { LocalStorageService } from 'angular-2-local-storage';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subject } from 'rxjs/Rx';
import { EmployeeNavBarService } from '../employee-nav-bar.service';
import { Course } from 'app/models/capability-transformation/course-list.model';
import { Roles, RoleNames } from 'app/models/auth/auth.model';
import { AuthService } from 'app/services/auth.service';
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 { Router, ActivatedRoute } from '@angular/router';
import { CapabilityTransformationHttpService } from 'app/services/capability-transformation/capability-transformation-http.service';
import { AssessmentHttpService } from 'app/services/assessment/assessment-http.service';
import { EmployeeMiniAssessmentResponse } from 'app/models/assessment/employee-assessment.model';
import {EmployeeAssessmentComponent} from "../employee-assessment/employee-assessment.component";
import { environment } from '../../../../environments/environment.dev';
import { CreateEnvironmentComponent } from '../../../admin/inner-admin-pages/right-admin-panel/create-environment/create-environment.component';

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

 export class EmployeeMiniAssessmentComponent implements OnInit, OnDestroy {
  private sub: any;
  private $destroyed = new Subject();
  private currentUser: UserInfo;
  selectedTab = 0;
  role: RoleNames = RoleNames.Employee;
  course: Course;
  environment: String[] = ['Java', 'Eclipse', 'Node JS'];
  assessment: Assessment = new Assessment();
  miniAssessment: any;
  AssessmentID: any;
  imageUrl: string = environment.imageUrl;
  constructor(
    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.route.params.subscribe( params =>
      this.AssessmentID = params['id']
    );
    if (this.AssessmentID) {
      this.currentUser = this.localStorageService.get('userInfo') as UserInfo;
      console.log(this.currentUser, 'this.currentUser')
        this.assessmentService.getEmployeeMiniAssessment(this.currentUser.userInfoMstrId, this.AssessmentID)
          .subscribe(
            (assessment: EmployeeMiniAssessmentResponse) => {
              console.log(assessment, 'EmployeeMiniAssessmentResponse')
              this.miniAssessment = assessment;
              this.assessment.setEmployeeMiniAssessment(assessment);
              this.authService.getAllSkill().then((skills: SkillsResponse) => {
                console.log(skills,'skills')
                 const skillTrackDtls = skills.pgSkillTrackDtlsList.filter(skill => skill.skillTrackDtlsId === 7)[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 === 7)[0] as PgAwsDirectoryBundleList;
                    if (awsDirectoryBundle) {
                      this.assessment.technicalUsecase.sandboxDir = {
                        sname: this.currentUser.adId,
                        sbundleId: awsDirectoryBundle.bundleId,
                        sdirId: awsDirectoryBundle.directoryId
                      };
                      this.assessment = Object.assign(new Assessment(), this.assessment);

                      console.log(this.assessment,'this.assessment')
                    }
                  });
                }
              });
            }
          );
    }
  }

  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;
   // this.location.go('/employee/dashboard/assessment/' + selectedTab);
  }

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