import { Component, OnInit, Input } from '@angular/core';
import { Router } from '@angular/router';
import { EmployeeHttpService } from '../../../../services/employee/employee-http.service';
import { Subject } from 'rxjs/Subject';
import * as Moment from 'moment';
import { EmployeeReview } from '../../../../models/employee.model';
import { environment } from '../../../../../environments/environment.dev';
@Component({
  selector: 'app-employee-card',
  templateUrl: './employee-card.component.html',
  styleUrls: ['./employee-card.component.scss']
})
export class EmployeeCardComponent implements OnInit {
  imagelink: string;
  @Input() id: string;
  @Input() title: string;
  @Input() count?: number;
  @Input() reviewData: any;
  image = '';
  private $destroyed = new Subject();

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

  ngOnInit() {
    this.imagelink = environment.imageUrl+'/assets/img/employee/newdashboard/';
    // this.getImageDetails();
  }

  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';
    }
  }

  getStatus(review) {
    const status = {
      'PENDING': 'To Do',
      'PASSED': 'Done',
      'REJECTED': 'Done',
      'DONE': 'Done',
      'OVERDUE': 'Overdue',
      'default': 'Invalid'
    };
    if (review.status === 'PENDING') {
      return this.isOverdue(review) ? status['OVERDUE'] : status['PENDING'];
    }
    return status[review.status] || 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(reviewer: any) {
    this.router.navigate(['/employee/dashboard/list', this.id, reviewer.id]);
  }

  isOverdue(review) {
    const calculatedDate = Moment(review.updatedDate, 'YYYY-MM-DD').add(review.overdueDays, 'days').format('YYYY-MM-DD');
    const currentDate = Moment(Moment.now()).format('YYYY-MM-DD');
    // console.log('assessment name ' + review.assessmentName + 'calculated Date ' + calculatedDate + ' currentDate ' +  currentDate + ' result ' + Moment(calculatedDate).isBefore(currentDate));
    return Moment(calculatedDate).isBefore(currentDate);
  }

  getAssessmentCount() {
    if (this.count === undefined || this.count === null) {
      return '00';
    } else if (this.count > 0 && this.count < 10) {
      return '0' + this.count;
    } else if (this.count > 10) {
      return this.count;
    }
  }

}
