import { UserInfo } from 'app/models/userInfo.model';
import { Component, OnInit, OnDestroy, OnChanges } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import {ActivatedRoute, Router} from '@angular/router';
import { Location } from '@angular/common';
import { EmployeeHttpService } from 'app/services/employee/employee-http.service';
import { EmployeeNavBarService } from '../employee-nav-bar.service';
import { EmployeeCoursesSliderComponent } from '../../shared/employee-course-card/employee-course-card.component';
import { EmployeeDashboardLeaderBoardComponent } from '../../shared/employee-dashboard-leader-board/employee-dashboard-leader-board.component';
import { EmployeeDetailsResponse, StatusCount, EmployeeDashboardTabNames } from 'app/models/employee.model';
import { LocalStorageService } from 'angular-2-local-storage';
import { PlatformHttpService } from 'app/services/platform/platform-http.service';
import { ComponentPlatformResponse } from 'app/models/conponent-platform.model';
import { Observable } from 'rxjs/Observable';
import { AuthService } from 'app/services/auth.service';
import { DirectoryBundleResponse } from 'app/models/directory-bundle-model';
import { SkillsResponse } from 'app/models/skills.model';
import { ProgressStateSetupWorkspaceService } from 'app/employee/employee-dashboard/employee-sandbox/progress-state-setup-workspace.service';
import { state, ResponseSetupWorkSpace } from 'app/models/sandbox-progress.model';
import { environment } from '../../../../environments/environment.dev';
@Component({
  selector: 'app-employee-old-dashboard',
  templateUrl: './employee-old-dashboard.component.html',
  styleUrls: ['./employee-old-dashboard.component.scss']
})

export class EmployeeOldDashboardComponent implements OnInit {
  imagelink: string;
  mytaskview: Boolean;
  showArow: boolean;
  myfeedbackview: boolean;
  newDashBoard = false;
  sandboxButton: { show: boolean, data?: ResponseSetupWorkSpace };
  platform_data: ComponentPlatformResponse[];
  private $destroyed = new Subject();
  currentUser: UserInfo;
  public view_employeeDetails: EmployeeDetailsResponse;
  public coursesCountStatus: StatusCount;

  public view_tabSections = [
    //{ id: EmployeeDashboardTabNames.capability, name: 'Capability Transformation', details: 'Courses: Upskill your Competency' },
    //{ id: EmployeeDashboardTabNames.platform, name: 'Platform', details: 'Digital Assets' },
    { id: EmployeeDashboardTabNames.innovation, name: 'Innovation', details: 'Ideas to MVP' }
  ];

  public view_selectedTabSection = this.view_tabSections[0];
  public togglePanelStatus = false;

  public enumEmployeeDashboardTabNames = EmployeeDashboardTabNames;

  img_path_prefix = environment.imageUrl+'/assets/img/employee/';
  img_path_suffix = '.png';
  img_shared_path_prefix = this.img_path_prefix + 'shared/';
  img_home_path_prefix = this.img_path_prefix + 'dashboard/home/';

  gridOptions = { cols: 3 };
  courses: any = [];
  imageUrl: string = environment.imageUrl;
  
  constructor( private employeeHttpService: EmployeeHttpService,
               private employeeNavBarService: EmployeeNavBarService,
               private localStorageService: LocalStorageService,
               private platformService: PlatformHttpService,
               private authService: AuthService,
               private progressSandboxService: ProgressStateSetupWorkspaceService,
               private route: ActivatedRoute,
               private location: Location
  ) {
    this.employeeNavBarService.setCurrentPage(0);
  }
  ngOnInit() {
    this.mytaskview = true;
    this.myfeedbackview = false;
    this.imagelink = environment.imageUrl+'/assets/img/employee/newdashboard/';
    this.currentUser = this.localStorageService.get('userInfo');
    const selectedTabName: EmployeeDashboardTabNames = this.route.snapshot.url.length > 0 ? this.route.snapshot.url[0].path as EmployeeDashboardTabNames : undefined;
    const selectedTab = selectedTabName !== EmployeeDashboardTabNames.capability ? this.view_tabSections.find(tab => tab.id === selectedTabName) : undefined;
    if (selectedTab) {
      this.setActiveTab(selectedTab);
    }

    this.employeeHttpService.getUserCoursesCountByStatus(this.currentUser.userInfoMstrId).takeUntil(this.$destroyed)
      .subscribe(coursesCount => {
        if (coursesCount.error == null) {
          this.coursesCountStatus = coursesCount;
          for (const tempKey in this.coursesCountStatus.statusCount) {
            if (this.coursesCountStatus.statusCount[tempKey] === null) {
              this.coursesCountStatus.statusCount[tempKey] = 0;
            }
          }
        }
      });

    this.employeeHttpService.getEmployeeDetails(this.currentUser.userInfoMstrId).takeUntil(this.$destroyed)
      .subscribe(details => {
        if (details != null) {
          this.view_employeeDetails = details;
        }
      });

    this.platformService.getComponentCatalogFromPlatform()
      .takeUntil(this.$destroyed)
      .subscribe(plat => this.platform_data = plat);

    Observable.combineLatest(
      this.progressSandboxService.ProgressIconComponent,
      this.progressSandboxService.setupProcessWorked)
      .subscribe(a => {
        this.sandboxButton = a[1].state === state.Completed && a[0].state === state.Completed ? { show: true, data: a[1].data } : { show: false };
      });
  }

  goPrevPage(event) {
    // alert('here');
    this.showArow = event;
  }
  viewchange(view) {
    if (view === 'myfeedbackview') {
      this.mytaskview = false;
      this.myfeedbackview = true;
    } else {
      this.mytaskview = true;
      this.myfeedbackview = false;
    }
  }

  /*OLD DASHBOARD CONTENT*/


  ngOnChanges() {
    this.ngOnInit();
  }

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

  /**
   * Set active tab content
   *
   * @public
   * @param {*} tab
   * @memberof EmployeeDashboardContentComponent
   */
  public setActiveTab(selectedTab: any): void {
    this.view_selectedTabSection = selectedTab;
    this.location.go('/employee/dashboard/old/' + selectedTab.id);
  }

  public manageLeaderBoard() { }

  goto() {
    window.open('https://3-platform.altimetrik.com/componentCatalog', '_blank');
  }

}
