import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { WorklistSettingComponent } from 'src/app/notification/worklist-setting/worklist-setting.component';
import { CommonService } from '../../services/common/common.service';
import { DashboredService } from '../../services/dashbored/dashbored.service';
import { PerformanceAppraisalResponse } from '../../services/dashbored/performance-appraisal.response';
import { SharedDataService } from '../../services/shared-data-service/shared-data.service';
import { TranslatorService } from '../../services/translator/translator.service';

@Component({
  selector: 'app-generic-header',
  templateUrl: './generic-header.component.html',
  styleUrls: ['./generic-header.component.scss'],
})
export class GenericHeaderComponent implements OnInit {
  @Input() userImage = '';
  @Input() showImage = false;
  @Input() showMenu = false;
  @Input() showBack = false;
  @Input() navigate = true;
  @Input() showClose = false;
  @Input() updateDirection = false;
  @Input() headerText = '';
  @Input() backLink: string;
  @Input() worklistSetting = false;
  @Input() performanceEvaluation = false;
  @Input() subordinateLeave = false;
  @Output() trigger = new EventEmitter();

  public appraisalArr: any = [];
  public performanceData: any = [];

  public close = 'assets/imgs/cancel.png';
  constructor(
    public common: CommonService,
    public DS: DashboredService,
    public sharedData: SharedDataService,
    public ts: TranslatorService,
    public modalController: ModalController) { }

  ngOnInit() {
    if (this.updateDirection) {
        this.showBack = false;
      }
  }

  openProfilePage() {
    this.common.openProfile('sideMenu');
  }

  closeModal() {
    this.trigger.emit();
  }

  async openNotifictionSetting() {
    // const modal = await this.modalController.create({
    //   component: WorklistSettingComponent,
    //   cssClass: 'app-update-modal-css',
    // });
    // return await modal.present();
    this.common.openWorklistSettingsPage();
  }


  public openPerormance() {
    this.showPerformanceAppraisal();
  }

  public showPerformanceAppraisal() {
    this.DS.getPerformanceAppraisal(() => {
      this.showPerformanceAppraisal();
    }).subscribe((result: PerformanceAppraisalResponse) => {
      this.handlePerformanceAppraisalResult(result);
    });
  }

  private handlePerformanceAppraisalResult(result) {
    if (this.common.validResponse(result)) {
      if (this.common.hasData(result.GetPerformanceAppraisalList)) {
        this.appraisalArr = result.GetPerformanceAppraisalList;
        this.performanceData = [];
        for (let i = 0; i < this.appraisalArr.length; i++) {
          this.performanceData.push({ name: this.appraisalArr[i].APPRAISAL_YEAR, value: parseInt(this.appraisalArr[i].APPRAISAL_SCORE).toFixed() });
        }
        console.log('PerformanceAppraisalResponse');
        this.sharedData.setSharedData(this.performanceData, PerformanceAppraisalResponse.PERFORMANCE_DATA);
        this.common.openPerformanceevaluation();
      } else {
        let msg: string = this.ts.trPK("userProfile", "no-appraisal");
        this.common.presentAlert(msg);
      }
    }
  }

  public openSubordinateLeavePage() {
    this.common.openSubordinateLeavePage();
  }
}
