import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';

export enum MODAL_STATE {
  OPEN = 'open',
  CLOSED = 'closed'
}

export enum MODAL_CONTENT {
  SELECT = 'select',
  REJECT = 'reject'
}

@Injectable()
export class TaModalServiceService {

  private _candidate: BehaviorSubject<any> = new BehaviorSubject({});
  private _modal_state: BehaviorSubject<MODAL_STATE> = new BehaviorSubject(null);
  private _modal_content: BehaviorSubject<MODAL_CONTENT> = new BehaviorSubject(null);
  
  private _modal_accepted: BehaviorSubject<boolean> = new BehaviorSubject(null);

  public readonly candidate: Observable<any> = this._candidate.asObservable();
  public readonly modal_state: Observable<MODAL_STATE> = this._modal_state.asObservable();
  public readonly modal_content: Observable<MODAL_CONTENT> = this._modal_content.asObservable();
  public readonly modal_accepted: Observable<boolean> = this._modal_accepted.asObservable();

  constructor() { }

  selectCandidate(candidate) {
    console.log('selecting candidate', candidate);
    this._candidate.next(candidate);
  }

  showModal(modal: MODAL_CONTENT) {
    this._modal_accepted.next(null);
    this._modal_content.next(modal);
    this._modal_state.next(MODAL_STATE.OPEN);
  }

  confirm() {
    this._modal_state.next(MODAL_STATE.CLOSED);
    this._modal_accepted.next(true);
  }

  cancel() {
    this._modal_state.next(MODAL_STATE.CLOSED);
    this._modal_accepted.next(false);
  }
}