import { RequestOptions } from '@angular/http';
import { Component, OnInit, Input, Output } from '@angular/core';
import { AuthService } from '../../services/auth.service';
import { EmployeeHttpService } from 'app/services/employee/employee-http.service';
import { LocalStorageService } from 'angular-2-local-storage';
import {
  DashboardDetailsPayload,
  UserDetailsPayload,
  StepperDetails
} from 'app/models/candidate-dashboard-response/candidate-dashboard-response';
import _ from 'lodash';
import { environment } from '../../../environments/environment.dev';
// import { FeedbackmodalComponent } from 'app/shared/feedbackmodal/feedbackmodal.component';

@Component({
  selector: 'app-candidate-dashboard',
  templateUrl: './candidate-dashboard.component.html',
  styleUrls: ['./candidate-dashboard.component.scss']
})
export class CandidateDashboardComponent implements OnInit {
  showChalangeCard: boolean;
  dashboardDetailspayload: DashboardDetailsPayload;
  userDetailsPayload: UserDetailsPayload;
  dashboardDetails: any;
  userDetails: any;
  testLinkDetails: any;
  mettelLinkDetails: any;
  coadingLinkDetails: any;
  coachDetails: any;
  showCongBox: boolean;
  showBackArrow: boolean;
  congratulationPage: boolean;
  empData: any;
  primaryStatus: string;
  codingStatus: string;
  whiteboardStatus: string;
  leadershipStatus: string;
  initials: string;
  sucessText: string;
  candidateChallengeStatus;
  pcColor: string;
  ccColor: string;
  wdColor: string;
  lcColor: string;

  pcStatusColor: string;
  ccStatusColor: string;
  wdStatusColor: string;
  lcStatusColor: string;

  pcStatusText: string;
  ccStatusText: string;
  wdStatusText: string;
  lcStatusText: string;

  pcBoxBorderColor: string;
  ccBoxBorderColor: string;
  wdBoxBorderColor: string;
  lcBoxBorderColor: string;

  offeredColor: string;
  img_path = environment.imageUrl + '/assets/img/Taflow/';
  stepperObj = [];
  frmCandD: boolean = false;

  constructor(
    public authService: AuthService,
    public employeeHttpService: EmployeeHttpService,
    private localStorageService: LocalStorageService
  ) {
    this.showChalangeCard = true;
    this.showCongBox = false;
    this.primaryStatus = 'task_enabled';
    this.codingStatus = 'task_dissabled';
    this.whiteboardStatus = 'task_dissabled';
    this.leadershipStatus = ' task_dissabled';
    this.pcColor = '';
    this.ccColor = '';
    this.wdColor = '';
    this.lcColor = '';
    this.offeredColor = '';
    const empData = this.localStorageService.get('userInfo');
    this.empData = empData;
    if (this.empData['fullName']) {
      this.initials = this.empData['fullName'].replace(/\W*(\w)\w*/g, '$1').toUpperCase();
    }
    this.dashboardDetailspayload = {
      accessToken: this.empData.accessToken,
      idpType: 'databaseIdentity'
    };
    // { "accessToken": "f81217f0-b13b-4a21-9d68-218b04eee0d7", "idpType": "databaseIdentity" }
    // this.userDetailsPayload = {
    //   accessToken: '2d4e57d5-0fe9-49f2-97b1-661b90350669',
    //   idpType: 'databaseIdentity'
    // };
  }


  ngOnInit() {
    this.getCandidateUserDetails(this.dashboardDetailspayload);
  }
  checkStatus(challengeStatus) {
    if (challengeStatus === 'METTL_INITIATED' || challengeStatus === 'METTL_IN_PROGRESS') {
      this.pcColor = 'pc-color';
      this.pcBoxBorderColor = 'pc-color';
    } else if (challengeStatus === 'CODING_CHALLENGE_INITIATED' || challengeStatus === 'CODING_CHALLENGE_STARTED' || challengeStatus === 'CODING_CHALLENGE_READY' || challengeStatus === 'CODING_CHALLENGE_COMPLETED') {
      this.ccColor = 'cc-color';
      this.ccBoxBorderColor = 'cc-color';
    } else if (challengeStatus === 'WHITEBOARD_READY') {
      this.wdColor = 'wd-color';
      this.wdBoxBorderColor = 'wd-color';
    } else if (challengeStatus === 'LEADERSHIP_CONNECT_READY') {
      this.lcColor = 'lc-color';
      this.lcBoxBorderColor = 'lc-color';
      this.offeredColor = 'all-cleared';
    } else if (challengeStatus === 'METTL_CLEARED') {
      this.pcColor = 'success-color';
    } else if (challengeStatus === 'CODING_CHALLENGE_CLEARED') {
      this.ccColor = 'success-color';
    } else if (challengeStatus === 'WHITEBOARD_CLEARED') {
      this.wdColor = 'success-color';
    } else if (challengeStatus === 'LEADERSHIP_CONNECT_CLEARED') {
      this.lcColor = 'success-color';
      this.offeredColor = 'all-done';
      // this.stepperObj.forEach(element => {
      //   if (element.taskName === 'Offer Status') {
      //     element.taskName = 'Offer Received';
      //   }
      // });
    }
  }
  checkForDashBoardDataStatus(userStatus, challenge) {
    if ((userStatus === 'METTL_INITIATED')) {
      if (challenge === 'primary') {
        this.pcStatusColor = 'task-initiated-heading';
        this.pcStatusText = 'Initiated';
      } else if (challenge === 'coding') {
        this.ccStatusColor = 'task-initiated-heading';
        this.ccStatusText = 'Initiated';
      }
    } else if ((userStatus === 'METTL_IN_PROGRESS') || (userStatus === 'CODING_CHALLENGE_STARTED') ||
      (userStatus === 'WHITEBOARD_READY') || (userStatus === 'LEADERSHIP_CONNECT_READY')) {
      if (challenge === 'primary') {
        this.pcStatusColor = 'task-InProgress-heading';
        this.pcStatusText = 'In Progress';
      } else if (challenge === 'coding') {
        this.ccStatusColor = 'task-InProgress-heading';
        this.ccStatusText = 'In Progress';
      } else if ((challenge === 'whiteboard') && (this.dashboardDetails.whiteboardSession.event.eventStatus === 'ACTIVE')) {
        this.wdStatusColor = 'task-InProgress-heading';
        this.wdStatusText = 'In Progress';
      } else if ((challenge === 'leadership') && (this.dashboardDetails.leadershipConnect.event.eventStatus === 'ACTIVE')) {
        this.lcStatusColor = 'task-InProgress-heading';
        this.lcStatusText = 'In Progress';
      }
    } else if ((userStatus === 'METTL_REVIEW') || (userStatus === 'CODING_CHALLENGE_COMPLETED')) {
      if (challenge === 'primary') {
        this.pcStatusColor = 'task-OnHold-heading';
        this.pcStatusText = 'On Hold';
      } else if (challenge === 'coding') {
        this.ccStatusColor = 'task-OnHold-heading';
        this.ccStatusText = 'On Hold';
      }
    } else if ((userStatus === 'METTL_CLEARED') || (userStatus === 'CODING_CHALLENGE_CLEARED') ||
      (userStatus === 'WHITEBOARD_CLEARED') || (userStatus === 'LEADERSHIP_CONNECT_CLEARED')) {
      if (challenge === 'primary') {
        this.pcStatusColor = 'task-completed-heading';
        this.pcStatusText = 'completed';
      } else if (challenge === 'coding') {
        this.ccStatusColor = 'task-completed-heading';
        this.ccStatusText = 'completed';
      } else if ((challenge === 'whiteboard') && (this.dashboardDetails.whiteboardSession.event.eventStatus === 'PASSED')) {
        this.wdStatusColor = 'task-completed-heading';
        this.wdStatusText = 'completed';
      } else if ((challenge === 'leadership') && (this.dashboardDetails.leadershipConnect.event.eventStatus === 'PASSED')) {
        this.lcStatusColor = 'task-completed-heading';
        this.lcStatusText = 'completed';
      } else if (
        ((challenge === 'whiteboard') && (this.dashboardDetails.whiteboardSession.event.eventStatus === 'ACTIVE'))
        || ((challenge === 'whiteboard') && (this.dashboardDetails.whiteboardSession.event.eventStatus === 'INPROGRESS'))) {
        this.wdStatusColor = 'task-InProgress-heading';
        this.wdColor = 'wd-color';
        this.wdStatusText = 'In Progress';
      } else if (((challenge === 'leadership') && (this.dashboardDetails.whiteboardSession.event.eventStatus === 'ACTIVE'))
        || ((challenge === 'leadership') && (this.dashboardDetails.whiteboardSession.event.eventStatus === 'INPROGRESS'))) {
        this.wdStatusColor = 'task-InProgress-heading';
        this.lcColor = 'lc-color';
        this.wdStatusText = 'In Progress';
      }

    } else if ((userStatus === 'METTL_FAILED') || (userStatus === ' CODING_CHALLENGE_REJECTED') || (userStatus === 'WHITEBOARD_REJECTED') || (userStatus === 'LEADERSHIP_CONNECT_REJECTED')) {
      if (challenge === 'primary') {
        this.pcStatusColor = 'task-Rejected-heading';
        this.pcStatusText = 'Rejected';
      } else if (challenge === 'coding') {
        this.ccStatusColor = 'task-Rejected-heading';
        this.ccStatusText = 'Rejected';
      } else if (challenge === 'whiteboard') {
        this.wdStatusColor = 'task-Rejected-heading';
        this.wdStatusText = 'Rejected';
      } else if (challenge === 'leadership') {
        this.lcStatusColor = 'task-Rejected-heading';
        this.lcStatusText = 'Rejected';
      }
    } else if ((userStatus === 'CODING_CHALLENGE_READY')) {
      if (challenge === 'coding') {
        this.ccStatusColor = 'task-AwaitingInitiation-heading';
        this.ccStatusText = 'Awaiting Initiation';
      }
    } else if ((userStatus === 'CODING_CHALLENGE_INITIATED')) {
      if (challenge === 'coding') {
        this.ccStatusColor = 'task-ChallengeInitiated-heading';
        this.ccStatusText = 'Challenge Initiated';
      }
    }
  }
  dashboardStatus(dashboardStatus) {
    if (this.dashboardDetails.primaryChallenge !== null) {
      if (this.dashboardDetails.primaryChallenge.event.eventStatus === 'PASSED') {
        this.pcStatusColor = 'task-completed-heading';
        this.pcStatusText = 'completed';
      } else {
        this.checkForDashBoardDataStatus(dashboardStatus, 'primary');
      }
    }
    if (this.dashboardDetails.codingChallenge !== null) {
      if (this.dashboardDetails.codingChallenge.event.eventStatus === 'PASSED') {
        this.ccStatusColor = 'task-completed-heading';
        this.ccStatusText = 'completed';
      } else {
        this.checkForDashBoardDataStatus(dashboardStatus, 'coding');
      }
    }
    if (this.dashboardDetails.whiteboardSession !== null) {
      if (this.dashboardDetails.whiteboardSession.event.eventStatus === 'PASSED') {
        this.wdStatusColor = 'task-completed-heading';
        this.wdStatusText = 'completed';
      } else {
        this.checkForDashBoardDataStatus(dashboardStatus, 'whiteboard');
      }
    }
    if (this.dashboardDetails.leadershipConnect !== null) {
      if (this.dashboardDetails.leadershipConnect.event.eventStatus === 'PASSED') {
        this.lcStatusColor = 'task-completed-heading';
        this.lcStatusText = 'completed';
      } else {
        this.checkForDashBoardDataStatus(dashboardStatus, 'leadership');
      }
    }
  }
  checkActiveChallange(dashboardDetails) {
    if (dashboardDetails.primaryChallenge === null) {
      this.primaryStatus = 'task_dissabled';
    } else {
      this.primaryStatus = this.getStatus(dashboardDetails.primaryChallenge.event.eventStatus);
      if (this.primaryStatus === 'task_failed') {
        this.pcBoxBorderColor = 'task-failed-color';
      } else if (this.primaryStatus === 'task_passed') {
        this.pcColor = 'pc-color';
      }
    }
    if (dashboardDetails.codingChallenge === null) {
      this.codingStatus = 'task_dissabled';
    } else {
      this.codingStatus = this.getStatus(dashboardDetails.codingChallenge.event.eventStatus);
      if (this.codingStatus === 'task_failed') {
        this.ccBoxBorderColor = 'task-failed-color';
      } else if (this.codingStatus === 'task_passed') {
        this.ccColor = 'cc-color';
      }
    }
    if (dashboardDetails.whiteboardSession === null) {
      this.whiteboardStatus = 'task_dissabled';
    } else {
      this.whiteboardStatus = this.getStatus(dashboardDetails.whiteboardSession.event.eventStatus);
      if (this.whiteboardStatus === 'task_failed') {
        this.wdBoxBorderColor = 'task-failed-color';
      } else if (this.whiteboardStatus === 'task_passed') {
        this.wdColor = 'wd-color';
      }
    }
    if (dashboardDetails.leadershipConnect === null) {
      this.leadershipStatus = 'task_dissabled';
    } else {
      this.leadershipStatus = this.getStatus(dashboardDetails.leadershipConnect.event.eventStatus);
      if (this.leadershipStatus === 'task_failed') {
        this.lcBoxBorderColor = 'task-failed-color';
      } else if (this.leadershipStatus === 'task_passed') {
        this.lcColor = 'lc-color';
      }
    }
  }

  getStatus(status) {
    if (status === 'ACTIVE') {
      return 'task_enabled';
    } else if (status === 'INPROGRESS' || status === 'COMPLETED') {
      return 'task_enabled';
      // } else if (status === 'COMPLETED') {
      //   return 'task_enabled';
    } else if (status === 'PASSED') {
      if (this.dashboardDetails.userDetails.userStatus === 'METTL_CLEARED'
        || this.dashboardDetails.userDetails.userStatus === 'CODING_CHALLENGE_CLEARED'
        || this.dashboardDetails.userDetails.userStatus === 'WHITEBOARD_CLEARED') {
        this.congratulationPage = true;
      }
      return 'task_passed';
    } else if (status === 'FAILED') {
      return 'task_failed';
    }
  }
  checkForSuccess(event, cardName) {
    if (((this.dashboardDetails.primaryChallenge != null &&
      this.dashboardDetails.primaryChallenge.event.eventStatus === 'PASSED') && (cardName === 'primary'))) {
      this.congratulationPage = true;
      this.showChalangeCard = false;
      this.showBackArrow = true;
      this.sucessText = 'primary';
    } else if ((this.dashboardDetails.codingChallenge != null &&
      this.dashboardDetails.codingChallenge.event.eventStatus === 'PASSED') && (cardName === 'coding')) {
      this.congratulationPage = true;
      this.showChalangeCard = false;
      this.showBackArrow = true;
      this.sucessText = 'coding';
    } else if ((this.dashboardDetails.whiteboardSession != null &&
      this.dashboardDetails.whiteboardSession.event.eventStatus === 'PASSED') && (cardName === 'whiteboard')) {
      this.congratulationPage = true;
      this.showChalangeCard = false;
      this.showBackArrow = true;
      this.sucessText = 'business';
    }
  }

  getCandidateDashboardDetails(reqData) {
    this.authService
      .dashBoardDetails(reqData)
      .then(resp => {
        this.dashboardDetails = JSON.parse(resp);
        // _.forEach(this.dashboardDetails, element => {
        if (this.dashboardDetails.primaryChallenge) {
          this.mettelLinkDetails = this.dashboardDetails.primaryChallenge.testDetails.testLink;
        }
        if (this.dashboardDetails.codingChallenge) {
          this.coadingLinkDetails = this.dashboardDetails.codingChallenge;
          this.getCoachDetails(this.dashboardDetails.codingChallenge.event.coachId);
        }
        // });
        this.checkStatus(this.dashboardDetails.userDetails.userStatus);
        this.dashboardStatus(this.dashboardDetails.userDetails.userStatus);
        this.checkActiveChallange(this.dashboardDetails);
        this.stepperObj = [
          { taskName: 'Primary Challenge', taskStatus: this.primaryStatus, colorStatus: this.pcBoxBorderColor },
          { taskName: 'Coding Challenge', taskStatus: this.codingStatus, colorStatus: this.ccBoxBorderColor },
          { taskName: 'Whiteboard Discussion', taskStatus: this.whiteboardStatus, colorStatus: this.wdBoxBorderColor },
          { taskName: 'Leadership Connect', taskStatus: this.leadershipStatus, colorStatus: this.lcBoxBorderColor },
          { taskName: 'Offer Status', taskStatus: 'got_offer', colorStatus: this.offeredColor }
        ];
      })
      .catch(error => {
        console.log(error);
      });
  }
  getCoachDetails(CoachId) {
    this.employeeHttpService
      .getCoachDetails(CoachId)
      .subscribe(data => {
        const nameInitials = data.userList[0]['fullName'].replace(/\W*(\w)\w*/g, '$1').toUpperCase();
        this.coachDetails = data.userList[0];
        this.coachDetails.nameInitials = nameInitials;
      });
  }
  getCandidateUserDetails(reqData) {
    this.authService
      .userDetails(reqData)
      .then(resp => {
        this.userDetails = JSON.parse(resp);
        this.getCandidateDashboardDetails(this.dashboardDetailspayload);
        // this.getWorkSpaceId();
      })
      .catch(error => {
        console.log(error);
      });
  }
  getCandidateChallengeStatus(reqData) {
    this.authService
      .getCandidateChallangeStatus(reqData)
      .then(resp => {
        this.candidateChallengeStatus = JSON.parse(resp);
        this.getCandidateDashboardDetails(this.dashboardDetailspayload);
      })
      .catch(error => {
        console.log(error);
      });
  }
  sendDescribeWorkspaces(data) {
    this.authService
      .describeWorkspaces(data)
      .then(resp => {
        console.log('updated successfully');
      })
      .catch(error => {
        console.log(error);
      });
  }
  getWorkSpaceId() {
    this.authService.getWorkspaceId(this.userDetails.infoDetails.adId).then(reqData => {
      console.log(reqData);
      this.sendDescribeWorkspaces(reqData['workSpaceId']);
    }).catch(error => {
      console.log(error);
    });
  }
  getEventDetails(event) {
    if (event.id === 1) {
      if (this.dashboardDetails.userDetails.userStatus === 'ONBOARDED' ||
        this.dashboardDetails.userDetails.userStatus === 'METTL_INITIATED' ||
        this.dashboardDetails.userDetails.userStatus === 'METTL_IN_PROGRESS' || this.dashboardDetails.primaryChallenge.testDetails.testStatus === 'Completed') {
        this.getCandidateChallengeStatus({ loggedInUser: this.empData.email, userIds: [this.empData.userInfoMstrId], eventId: event.id });
        this.openInNewTab(this.mettelLinkDetails);
      }
    } else if (event.id === 2) {
      if (this.dashboardDetails.userDetails.userStatus === 'CODING_CHALLENGE_STARTED') {
        if (event.event.srcElement.innerHTML === 'Resume') {
          this.congratulationPage = false;
          this.showChalangeCard = false;
          this.showBackArrow = true;
        } else {
          this.getCandidateChallengeStatus({ loggedInUser: this.empData.email, userIds: [this.empData.userInfoMstrId], eventId: event.id });
        }
      } else {
        this.congratulationPage = false;
        this.showChalangeCard = false;
        this.showBackArrow = true;
        this.getCandidateChallengeStatus({ loggedInUser: this.empData.email, userIds: [this.empData.userInfoMstrId], eventId: event.id });
      }
    } else if (event.id === 3) {
      this.congratulationPage = false;
      // this.showChalangeCard = false;
      // this.showBackArrow = true;
      this.getCandidateChallengeStatus({ loggedInUser: this.empData.email, userIds: [this.empData.userInfoMstrId], eventId: event.id });
    } else if (event.id === 4) {
      this.congratulationPage = false;
      // this.showChalangeCard = false;
      // this.showBackArrow = true;
      this.getCandidateChallengeStatus({ loggedInUser: this.empData.email, userIds: [this.empData.userInfoMstrId], eventId: event.id });
    }
  }
  viewCoadingDetails(event) {
    this.congratulationPage = false;
    this.showChalangeCard = false;
    this.showBackArrow = true;
  }
  openInNewTab(url) {
    const win = window.open(url, '_blank');
    win.focus();
  }
  getBack(event) {
    this.congratulationPage = false;
    this.showBackArrow = false;
    this.showChalangeCard = true;
  }
  backToDashbord(event) {
    this.congratulationPage = false;
    this.showChalangeCard = true;
    this.showBackArrow = false;
  }

  openFeedModal(e) {
    console.log(e);
    if (e == true) {
      this.frmCandD = e
    }
  }

  closeFdModal(e) {
    console.log(e);
    if (e == false) {
      this.frmCandD = e;
      console.log(this.frmCandD);
    }
  }
}
