import { MyTeamService } from './../../my-team/service/my-team.service';
import { MySpecialistService } from './../service/my-specialist.service';
import { SpecialistResponse } from './../models/specialist.response';
import { Component, OnInit } from '@angular/core';
import { MenuResponse } from 'src/app/hmg-common/services/menu/models/menu-response';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { LoginRequest } from 'src/app/hmg-common/services/authentication/models/login.request';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss'],
})
export class HomeComponent implements OnInit {

  isSearch: boolean = false;
  mySpecList: any;
  searchKey: any;
  searchKeySelect: any;
  P_USER_NAME: string;
  headerTitle: string = "";
  selMenu: MenuResponse;
  specialistEmpList: any;
  IsReachEnd: boolean = false;
  P_PAGE_NUM: number;
  P_PAGE_LIMIT: number;
  request: any;
  specialistItem: any;

  constructor(public cs: CommonService, private ts: TranslatorService, public mySpecialistService: MySpecialistService) { }

  ngOnInit() {
    let item = this.cs.sharedService.getSharedData('mySpecList', false);
    this.specialistItem = item;
    this.P_USER_NAME = this.cs.sharedService.getSharedData(LoginRequest.SHARED_DATA, false);
    this.selMenu = new MenuResponse();
    this.selMenu = this.cs.sharedService.getSharedData(MenuResponse.SHARED_DATA, false);
    this.headerTitle = this.selMenu.List_Menu.SUB_MENU_NAME;
    this.P_PAGE_LIMIT = 50;
    this.P_PAGE_NUM = 1;
    this.getEmployeeProfile();
  }

  getEmployeeProfile() {
    let searchEmpNum: string = "";
    let searchEmpName: string = "";
    let searchEmpEmail: string = "";
    if (this.searchKey) {
      switch (this.searchKeySelect) {
        case '1': {

          searchEmpName = this.searchKey;
          searchEmpNum = "";
          searchEmpEmail = "";

          break;
        }
        case '2': {
          searchEmpNum = this.searchKey;
          searchEmpName = "";
          searchEmpEmail = "";
          break;
        }
        case '3': {
          searchEmpEmail = this.searchKey;
          searchEmpNum = "";
          searchEmpName = "";
          break;
        }
        default: {
          searchEmpNum = "";
          searchEmpName = "";
          searchEmpEmail = "";
          break;
        }
      }
    } else {
      searchEmpNum = "";
      searchEmpName = "";
      searchEmpEmail = "";
    }
    let item: any = this.specialistItem;
    this.P_PAGE_NUM = 1;
    let selEmpNo: string = null;
    let respID: number;
    this.IsReachEnd = false;
    this.cs.sharedService.setSharedData(item.RESP_ID, MenuResponse.SHARED_SEL_RESP_ID);
    if (item.MENU_TYPE == 'S') {
      selEmpNo = null;
      respID = item.RESP_ID;
    }
    const request = {
      P_SELECTED_EMPLOYEE_NUMBER: selEmpNo,
      P_SELECTED_RESP_ID: respID,
      P_PAGE_NUM: this.P_PAGE_NUM,
      P_PAGE_LIMIT: this.P_PAGE_LIMIT,
      P_SEARCH_EMPLOYEE_NUMBER: searchEmpNum,
      P_SEARCH_EMPLOYEE_DISPLAY_NAME: searchEmpName,
      P_SEARCH_EMAIL_ADDRESS: searchEmpEmail
    }

    this.request = request;
    this.mySpecialistService.getUserInformation(
      request).
      subscribe((result: SpecialistResponse) => {
        this.handleSpecialistResult(result);
      });


  }
  private handleSpecialistResult(result) {
    if (this.cs.validResponse(result)) {
      //this.isSearch=false;
      if (this.cs.hasData(result.MemberInformationList)) {
        this.request.P_PAGE_NUM++;
        this.specialistEmpList = result.MemberInformationList;
        let lastItemIndex = result.MemberInformationList.length - 1;
        if (result.MemberInformationList[lastItemIndex]) {
          let lastitem = result.MemberInformationList[lastItemIndex];
          if (lastitem.NO_OF_ROWS == lastitem.ROW_NUM) {
            this.IsReachEnd = true;
          } else {
            this.IsReachEnd = false;
          }
          // this.navCtrl.push('SmsAuthenticatePage');
        }
      } else {
        this.specialistEmpList = [];
      }
      //  this.navCtrl.push("MyTeamPage",{employee:result.MemberInformationList[0],empSubordinate:{},isSecondLevel:true});

    }

  }
  public getSpecialistInfo(i) {
    // this.navCtrl.push("MyTeamPage", { employee: this.specialistEmpList[i], isSpecialist: true });
    this.cs.sharedService.setSharedData(this.specialistEmpList[i], MyTeamService.EMPLOYEE_SHARED_DATA);
    this.cs.sharedService.setSharedData(true, 'isSpecialist');
    this.cs.openMyTeamDetails();
  }
  doInfinite(infiniteScroll) {
    if (!this.IsReachEnd) {
      this.mySpecialistService.getUserInformation(
        this.request).
        subscribe((result: SpecialistResponse) => {
          if (this.cs.validResponse(result)) {
            if (this.cs.hasData(result.MemberInformationList)) {
              this.request.P_PAGE_NUM++;
              result.MemberInformationList.forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.specialistEmpList.push(element);
                // this.pro.GetVacationRulesList.push(vr);   
              });
            } else {
              this.IsReachEnd = true;
            }
          }
          //this.P_PAGE_NUM++;
          if (infiniteScroll)
            infiniteScroll.target.complete();


          // console.log(resFlag);
        }, (Error) => console.log(Error), () => infiniteScroll.target.complete());
    } else {
      if (infiniteScroll)
        infiniteScroll.target.complete();
    }
  }


  toggleSearch() {
    this.isSearch = !this.isSearch;
  }

}
