import {EventEmitter, Injectable} from '@angular/core';
import { Router } from '@angular/router';
import { SecurityService } from '../../logic/services/security.service';
import { Observable, Subject } from 'rxjs';
import { LookupSourceService } from './lookup-source.service';
import { Type } from '@angular/core/src/type';
import { ModalParams, ModalSize } from './app-navigation.service.models';
import { MetadataService } from './metadata.service';
import { AlertService } from '../../ui/infrastructure/alert.service';

@Injectable()
export class AppNavigationService {

  public customModalParams: any = {};
  public customModalOpened = false;
  public customModalTitle = 'Действие';
  public customModalAcceptButtonText = 'ОК';
  public customModalAcceptPressed = false;
  public customModalSize = 'sm';
  public customModalComponent = undefined;
  public customModalAcceptExternalPromise: Subject<any>;

  constructor(private router: Router,
              private lookupSourceService: LookupSourceService,
              private metadataService: MetadataService,
              private securityService: SecurityService,
              private alertService: AlertService) {
  }

  public showModal<TComponent, TParams, TResult>(component: Type<TComponent>,
                                                 params: ModalParams<TParams>): Observable<TResult> {
    if (params.size) {
      switch (params.size) {
        case ModalSize.sm:
          this.customModalSize = 'sm';
          break;
        case ModalSize.medium:
          this.customModalSize = '';
          break;
        case ModalSize.lg:
          this.customModalSize = 'lg';
          break;
        case ModalSize.xl:
          this.customModalSize = 'xl';
          break;
        default:
          this.customModalSize = '';
          break;
      }
    } else {
      this.customModalSize = '';
    }

    this.customModalComponent = component;
    this.customModalTitle = params.title ? params.title : '';
    this.customModalAcceptButtonText = params.acceptText ? params.acceptText : 'OK';
    this.customModalParams = params.initBodyParams;

    this.customModalAcceptPressed = false;
    this.customModalOpened = true;

    this.customModalAcceptExternalPromise = new Subject<TResult>();
    return this.customModalAcceptExternalPromise.asObservable();
  }
}
