import { InnovationStatusNames, POCStatus, InnovationType, InnovationStatus, ReviewerDetails } from 'app/models/InnovationIdea.model';
import { Router } from '@angular/router';
import { Component, Input, Output, EventEmitter } from '@angular/core';
import { ToastsManager } from 'ng2-toastr';
import { InnovationHttpService } from 'app/services/innovation/innovation-http.service';

enum ReviewOptions {
  approve = 'approve',
  reject = 'reject',
  rework = 'rework'
}

@Component({
  selector: 'app-my-innovation-idea-reviewer',
  templateUrl: './my-innovation-idea-reviewer.component.html',
  styleUrls: ['./my-innovation-idea-reviewer.component.scss']
})
export class MyInnovationIdeaReviewerComponent {
  @Input() reviewer: ReviewerDetails | null = { creatorId: 0, reviewerId: 0, innovationId: 0 };
  @Input() innovationType: InnovationType | null;
  @Output() actionButton = new EventEmitter<boolean>();

  enumReviewOptions = ReviewOptions;

  constructor(private innovationHttpService: InnovationHttpService, private toaster: ToastsManager, private router: Router) { }

  updateState(review, comment) {
    let state: InnovationStatusNames;
    switch (review) {
      case ReviewOptions.approve:
        state = this.innovationType === InnovationType.idea ? InnovationStatus.TakeOff : POCStatus.TakeOff;
        break;
      case ReviewOptions.reject:
        state = this.innovationType === InnovationType.idea ? InnovationStatus.Rejected : POCStatus.Rejected;
        break;
      case ReviewOptions.rework:
        state = this.innovationType === InnovationType.idea ? InnovationStatus.Rework : POCStatus.Rework;
        break;
    }
    this.innovationHttpService.updateInnovationIdea({
      submitterId: this.reviewer.reviewerId,
      userInfoMstrId: this.reviewer.creatorId,
      innovationId: this.reviewer.innovationId,
      status: state,
      comments: comment
    })
      .subscribe(r => {
        this.toaster.success('Innovation successfully reviewed');
        this.actionButton.emit(true);
        this.router.navigate(['/employee/dashboard/innovation']);
      }, e => this.toaster.error('There was an error sending the review, please try again.'));
  }
}
