import { Component, ViewChild, Injector, ElementRef, Output, EventEmitter } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    TimeZoneServiceProxy, UpdateTimeZoneInput
} from '@shared/service-proxies/service-proxies';
import * as _ from 'lodash';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';


@Component({
    selector: 'editTimeZoneModal',
    templateUrl: './edit-timezones-modal.component.html'
})
export class EditTimeZoneModalComponent extends AppComponentBase {

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  @ViewChild('modal', {static: false})modal: ModalDirective;
 @ViewChild('nameInput', {static: false}) nameInput: ElementRef;

  timezone: UpdateTimeZoneInput = new UpdateTimeZoneInput();

  active: boolean = false;
  saving: boolean = false;

  constructor(
    injector: Injector,
    private _timeZoneService: TimeZoneServiceProxy
  ) {
    super(injector);
  }

  show(timeZoneId): void {
    this.active = true;
    this._timeZoneService.getTimeZone(timeZoneId, undefined, undefined, undefined, undefined).subscribe((result)=> {
      this.timezone.value = result.items[0]['value'];
      this.timezone.id = result.items[0]['id'];
      //console.log(result.items[0]['name']);
      this.modal.show();
    });

  }

  onShown(): void {
   // this.nameInput.nativeElement.focus();
  }

  save(): void {
    this.saving = true;
    this._timeZoneService.updateTimeZone(this.timezone)
      .subscribe(() => {
        this.notify.info(this.l('SavedSuccessfully'));
        this.close();
        this.modalSave.emit(this.timezone);
      });
    this.saving = false;
  }

  close(): void {
    this.modal.hide();
    this.active = false;
  }
}
