import {Component, OnInit, Input, EventEmitter, Output} from '@angular/core';
import { Router } from '@angular/router';
import { EmployeeHttpService } from './../../../services/employee/employee-http.service';
import { Subject } from 'rxjs/Subject';
import { environment } from '../../../../environments/environment.dev';
@Component({
  selector: 'app-pgm-bny-employee-card',
  templateUrl: './pgm-bny-employee-card.component.html',
  styleUrls: ['./pgm-bny-employee-card.component.scss']
})
export class PgmBnyEmployeeCardComponent implements OnInit {

  imagelink: string;
  @Input() id: string;
  @Input() title: string;
  @Input() count?: number;
  @Input() list: any[];
  @Input() allskills: any[];
  @Output() goToEmployee = new EventEmitter<string>();
  private image = '';
  private $destroyed = new Subject();

  constructor(private router: Router,
              private employeeHttpService: EmployeeHttpService) { }

  ngOnInit() {
    this.imagelink = environment.imageUrl+'/assets/img/employee/newdashboard/';
    // this.getImageDetails();
    console.log("list -- ",this.list);
  }

  eventChange(ID) {
    this.goToEmployee.emit(ID);
  }

  getImageByValue(value: string) {
    switch (value) {
      case 'Overdue': return this.imagelink + 'overdue_icon.png';
      case 'To Do': return this.imagelink + 'completed.png';
      case 'Done': return this.imagelink + 'done_small.png';
      case 'REVIEW_PENDING': return this.imagelink + 'Under Review.png';
      case 'REVIEW_COMPLETED': return this.imagelink + 'Review Completed.png';
      case 'UNDER_REVIEW': return this.imagelink + 'Under Review.png';
      case 'INPROGRESS': return this.imagelink + 'In Progress.png';
      case 'ASSIGNED': return this.imagelink + 'completed.png';
    }
  }

  getStatus(type) {
    const status = {
      'ASSIGNED': 'ASSIGNED',
      'REVIEW_PENDING': 'REVIEW_PENDING',
      'REVIEW_COMPLETED': 'REVIEW_COMPLETED',
      'UNDER_REVIEW': 'UNDER_REVIEW',
      'INPROGRESS': 'INPROGRESS',
      'PENDING': 'To Do',
      'PASSED': 'Done',
      'REJECTED': 'Done',
      'DONE': 'Done',
      'TODO': 'To Do',
      'Open': 'To Do',
      'Overdue': 'Overdue',
      'OVERDUE': 'Overdue',
      'default': 'Invalid'
    };
    return status[type] || status['default'];
  }

  getUserImage(employeeId) {
    // this.getImageDetails(employeeId);
    return this.image;
  }

  getImageDetails(employeeId) {
    this.employeeHttpService.getEmployeeImage(this.id).takeUntil(this.$destroyed)
      .subscribe(data => {
        if(data === undefined || data.length === 0) {
          console.log('image is not present ' + employeeId);
          this.image = '';
        } else {
          console.log('image available ' + employeeId);
          this.image = 'data:image/jpg;base64,' + data[0].fileContent;
        }
      });
  }

  getInitials(fullName: string) {
    if (fullName) {
      return fullName.replace(/\W*(\w)\w*/g, '$1').toUpperCase();
    }
  }

  navigate(adId: string) {
    this.router.navigate(['/employee/dashboard/list', this.id, adId]);
  }

  getCount(count: number) {
    return (count < 9) ? '0' + count : count;
  }

}
