import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import Urls from './innovation-urls';
import {
  InnovationIdeaDetailsResponse, Idea, InnovationIdeaPayload, InnovationIdeaResponse, InnovationIdeaUpdatePayload,
  InnovationType, InnovationPOCPayload, InnovationPOCUpdatePayload
} from 'app/models/InnovationIdea.model';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { ResponseCoInnovation } from 'app/models/coInnovation-response';
import { PgUserInfoMstr } from 'app/models/user/user.model';

@Injectable()
export class InnovationHttpService {

  constructor(private http: HttpClient) { }

  /**
   * Get user innovation idea details
   *
   * @param {number} innovationId
   * @returns {Observable<InnovationIdeaDetailsResponse>}
   * @memberof InnovationHttpService
   */
  public getInnovationDetails(id: number): Observable<InnovationIdeaDetailsResponse> {
    return this.http.get<InnovationIdeaDetailsResponse>(Urls.getInnovationDetail + id);
  }

  /**
   * Get user innovation idea feedback details
   *
   * @param {number} innovationId
   * @returns {Observable<InnovationFeedBackDetailsResponse>}
   * @memberof InnovationHttpService
   */
  public getInnovationFeedbackDetails(id: number): Observable<any> {
    return this.http.get<any>(Urls.getInnovationFeedbackDetail + id + '/');
  }

  /**
   * Creates a new innovation
   *
   * @param {*} data
   * @returns {Observable<InnovationIdeaDetailsResponse>}
   * @memberof InnovationHttpService
   */
  public createInnovation(payload: InnovationIdeaPayload, type: InnovationType): Observable<InnovationIdeaDetailsResponse> {
    return type === InnovationType.idea ? this.createInnovationIdea(payload) : this.addExistingPOC(payload);
  }

  /**
   * Creates a new innovation idea
   *
   * @param {InnovationIdeaPayload} payload
   * @returns {Observable<InnovationIdeaDetailsResponse>}
   * @memberof InnovationHttpService
   */
  public createInnovationIdea(payload: InnovationIdeaPayload): Observable<InnovationIdeaDetailsResponse> {
    return this.http.post<InnovationIdeaDetailsResponse>(Urls.addInnovation, payload);
  }

  /**
   * Updates a new innovation idea
   *
   * @param {InnovationIdeaUpdatePayload} payload
   * @returns {Observable<InnovationIdeaDetailsResponse>}
   * @memberof InnovationHttpService
   */
  public updateInnovationIdea(payload: InnovationIdeaUpdatePayload): Observable<InnovationIdeaDetailsResponse> {
    return this.http.put<InnovationIdeaDetailsResponse>(Urls.updateInnovation, payload);
  }

  /**
   * Updates a new innovation poc
   *
   * @param {InnovationPOCUpdatePayload} payload
   * @returns {Observable<InnovationIdeaDetailsResponse>}
   * @memberof InnovationHttpService
   */
  public updateInnovationPOC(payload: InnovationPOCUpdatePayload): Observable<InnovationIdeaDetailsResponse> {
    return this.http.put<InnovationIdeaDetailsResponse>(Urls.updateInnovation, payload);
  }

  /**
   * Creates a new innovation POC
   *
   * @param {InnovationIdeaPayload} payload
   * @returns {Observable<InnovationIdeaDetailsResponse>}
   * @memberof InnovationHttpService
   */
  public addExistingPOC(payload: InnovationIdeaPayload): Observable<InnovationIdeaDetailsResponse> {
    payload.isExistingIdea = true;
    return this.http.post<InnovationIdeaDetailsResponse>(Urls.addExistingPOC, payload);
  }

  /**
   * Add a POC to an existing Idea
   *
   * @param {InnovationPOCPayload} payload
   * @returns {Observable<InnovationIdeaDetailsResponse>}
   * @memberof InnovationHttpService
   */
  public addInnovationPOC(payload: InnovationPOCPayload): Observable<InnovationIdeaDetailsResponse> {
    return this.http.post<InnovationIdeaDetailsResponse>(Urls.addInnovationPOC, payload);
  }

  /**
   * Get all innovations
   *
   * @param {number} employeeId
   * @returns {Observable<any>}
   * @memberof EmployeeHttpService
   */
  public getAllInnovation(): Observable<InnovationIdeaDetailsResponse> {
    return this.http.get<InnovationIdeaDetailsResponse>(Urls.getAllInnovation);
  }

  /**
   * Get user innovation idea details
   *
   * @param {number} employeeId
   * @returns {Observable<any>}
   * @memberof EmployeeHttpService
   */
  public getInnovationDetailsForEmployee(employeeId: number): Observable<InnovationIdeaDetailsResponse> {
    if (!employeeId && employeeId !== 0) {
      return Observable.throw('Could not fetch employee data with undefined id');
    }
    return this.http.get<InnovationIdeaDetailsResponse>(Urls.getInnovationDetails.replace(':userId', employeeId.toString()));
  }

  public getInnovationCategories(): Observable<[{
    categoryId: number;
    category: string;
    createdBy: string;
    createdDate: any;
    updatedBy: string;
    updatedDate: any;
  }]> {
    return this.http.get<any>(Urls.getInnovationCategories);
  }

  public coInnovator(userId: number): Observable<ResponseCoInnovation[]> {
    return this.http.get<ResponseCoInnovation[]>(Urls.coInnovationUrl.replace(':userId', userId.toString()));
  }

  public getAllInnovationFocal(): Observable<{
    focalId: number;
    focalName: string;
    pgUserInfoMstr: PgUserInfoMstr;
    createdBy: string;
    createdDate: number;
    updatedBy: string;
    updatedDate: number;
  }[]> {
    return this.http.get<any>(Urls.getAllInnovationFocalUrl);
  }

}
