import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { EmployeeHttpService } from 'app/services/employee/employee-http.service';
import { LocalStorageService } from 'angular-2-local-storage';
import { AuthService } from '../../../services/auth.service';
import { Router, ActivatedRoute } from '@angular/router';
import { Subject } from 'rxjs/Subject';
import * as Moment from 'moment';
import { EmployeeReview } from '../../../models/employee.model';
import {EmployeeNavBarService} from "../../employee-dashboard/employee-nav-bar.service";
import { environment } from '../../../../environments/environment.dev';
@Component({
  selector: 'app-employee-review-details',
  templateUrl: './employee-review-details.component.html',
  styleUrls: ['./employee-review-details.component.scss']
})
export class EmployeeReviewDetailsComponent implements OnInit {

  imagelink: string;
  reviewStatus: string;
  reviewerId: string;
  private empAdId: string;
  empData: EmployeeReview;
  private reviewDataList: EmployeeReview[];
  reviewData: EmployeeReview[];
  private image: any;
  loader = false;
  eventDetailsId: number;
  private defaultStatus = 'all';
  private $destroyed = new Subject();

  constructor(
    private employeeHttpService: EmployeeHttpService,
    private localStorageService: LocalStorageService,
    private employeeNavBarService: EmployeeNavBarService,
    private AuthService: AuthService,
    private router: Router,
    private activatedRoute: ActivatedRoute,
  ) {
      this.reviewerId = this.activatedRoute.snapshot.paramMap.get('id');
      this.empAdId = this.activatedRoute.snapshot.paramMap.get('adId');
      // this.getImageDetails();
    this.employeeNavBarService.setCurrentPage(11);
  }

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

  getReviewDetails() {
    this.loader = true;
    this.employeeHttpService.getEmployeeReviewDetails(this.reviewerId, this.defaultStatus).takeUntil(this.$destroyed)
      .subscribe(data => {
        this.reviewDataList = data;
        this.reviewData = this.reviewDataList.filter(item => item.id === this.empAdId);
        this.empData = this.reviewData[0];
        this.eventDetailsId = this.reviewData[0].eventDetailsId;
        this.reviewStatus = this.getStatus(this.reviewData[0]);
        this.loader = false;
      });
  }

  getImageDetails() {
    this.employeeHttpService.getEmployeeImage(this.reviewerId).takeUntil(this.$destroyed)
      .subscribe(data => {
        if (data === undefined || data.length === 0) {
          this.image = '';
        } else {
          this.image = 'data:image/jpg;base64,' + data[0].fileContent;
        }
      });
  }

  getClassByValue(value: string) {
    switch (value) {
      case 'Overdue': return 'icon overdue';
      case 'To Do': return 'icon to-do';
      case 'Done': return 'icon done';
    }
  }

  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: EmployeeReview) {
    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'];
  }

  submit(status, reviewer) {
    this.empData = reviewer;
    this.reviewStatus = status;
    this.eventDetailsId = reviewer.eventDetailsId;
  }

  updateStatus(value) {
    this.reviewStatus = value;
  }

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

  isOverdue(review: EmployeeReview) {
    const calculatedDate = Moment(review.updatedDate, 'YYYY-MM-DD').add(review.overdueDays, 'days');
    const currentDate = Moment(Moment.now()).format('YYYY-MM-DD');
    return Moment(calculatedDate).isBefore(currentDate);
   }

}
