import { Component, OnInit, OnChanges, Input, Output, EventEmitter } from '@angular/core';
import { EmployeeUseCase, EmployeeGitResponse, EmployeeReview, EmployeeCodeReviewResponse } from '../../../models/employee.model';
import { AuthService } from '../../../services/auth.service';
import { Router, ActivatedRoute } from '@angular/router';
import { EmployeeHttpService } from '../../../services/employee/employee-http.service';
import { Subject } from 'rxjs/Subject';
import { LocalStorageService } from 'angular-2-local-storage';
import { environment } from '../../../../environments/environment.dev';
@Component({
  selector: 'app-employee-review-info',
  templateUrl: './employee-review-info.component.html',
  styleUrls: ['./employee-review-info.component.scss']
})
export class EmployeeReviewInfoComponent implements OnInit, OnChanges {

  @Input() reviewData: EmployeeReview | null;
  @Output() valueChange = new EventEmitter();
  imagelink: string;
  private $destroyed = new Subject();
  ucDescp = '';
  emailId = '';
  codeReviewUrl: '';
  userInfoMstrId: string;
  gitUserName = '';
  gitPassword = '';
  gitUrl = '';
  sonarReport = '';
  loader = false;
  gitResponse: EmployeeGitResponse = {
    id: 0,
    gitUserId: '',
    name: '',
    username: '',
    password: '',
    email: '',
    createdBy: '',
    createdDate: '',
    updatedBy: '',
    updatedDate: '',
    userInfoMstrId: ''
  };
  useCase: EmployeeUseCase = {
    usecaseDescription: '',
    usecaseTitle: ''
  };
  codeReviewResponse: EmployeeCodeReviewResponse = {
    id: '',
    merge_req_id: '',
    event_dtls_id: '',
    user_info_mstr_id: '',
    merge_request_url: '',
    error: ''
  };

  constructor(
    private employeeHttpService: EmployeeHttpService,
    private authService: AuthService,
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private localStorageService: LocalStorageService,
  ) {
    this.imagelink = environment.imageUrl+'/assets/img/employee/newdashboard/';
    const userData = this.localStorageService.get('userInfo');
    if (userData['userInfoMstrId']) {
      this.userInfoMstrId = userData['userInfoMstrId'];
      this.emailId = userData['email'];
    }
    this.getGitDetails(this.emailId);
  }

  ngOnInit() {

  }

  ngOnChanges() {
    if (this.reviewData != null || this.reviewData !== undefined) {
      this.useCase.usecaseTitle = this.reviewData.assessmentName;
      this.useCase.usecaseDescription = this.reviewData.useCaseDescription;
    }
    this.getCodeReviewUrl(this.reviewData.eventDetailsId);
  }

  onComplete(value) {
    this.valueChange.emit('Feedback');
  }

  getGitDetails(emailId) {
    this.loader = true;
    this.employeeHttpService.getEmployeeGitDetails(emailId).takeUntil(this.$destroyed)
      .subscribe(data => {
        this.loader = false;
        if (data !== undefined || data != null) {
          this.gitResponse = data;
          this.gitUserName = data['username'];
          this.gitPassword = data['password'];
        }
      }
      );

  }

  getCodeReviewUrl(eventDetailsId) {
    this.employeeHttpService.getEmployeeCodeReviewDetails(String(eventDetailsId)).takeUntil(this.$destroyed)
      .subscribe(data => {
        if (data !== undefined || data != null) {
          this.codeReviewResponse = data;
          this.gitUrl = data['merge_request_url'];
          console.log('got codereviewURL ' + this.gitUrl);
        }
      });
  }

  getSonarReport() {
    this.authService.getUserSonarReport(this.reviewData.eventDetailsId).then(resp => {
      console.log(resp, 'response');
      if (resp.url) {
        this.sonarReport = resp;
        window.open(resp.url, '_blank');
      }
    }).catch(err => {
      console.log(err, 'error Responce');
    });
  }

}
