import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    AddressServiceProxy, LocationServiceProxy, UpdateLocationInput, UpdateContactInput, UpdateAddressInput, TimeZoneServiceProxy, PostalCodeServiceProxy, ContactServiceProxy, UserServiceProxy
} from '@shared/service-proxies/service-proxies';

import * as _ from 'lodash';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'editLocationModal',
    templateUrl: './edit-location-modal.component.html'
})
export class EditLocationModalComponent extends AppComponentBase {

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

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

    active = false;
    saving = false;
    location: UpdateLocationInput = new UpdateLocationInput();
    contact: UpdateContactInput = new UpdateContactInput();
    address: UpdateAddressInput = new UpdateAddressInput();

    country: any;
    filteredCountries: any;
    city: any;
    filteredCities: any;
    state: any;
    filteredStates: any;
    timeZone: any;
    filteredTimeZones: any;
    postal: any;
    filteredPostal: any;
    user: any;
    filteredUsers: any;

    constructor(
        injector: Injector,
        private _locationService: LocationServiceProxy,
        private _timeZoneService: TimeZoneServiceProxy,
        private _postalCodeService: PostalCodeServiceProxy,
        private _userService: UserServiceProxy,
        private _addressService: AddressServiceProxy,
        private _contactService: ContactServiceProxy
    ) {
        super(injector);
    }

    show(locationId: any): void {
        this.active = true;

        this._locationService.getLocation(locationId, undefined, undefined, undefined, undefined)
            .subscribe(result => {
            //this.locations = result.items;

            this.country = result.items[0]['contact']['address']['country'];
            this.state = result.items[0]['contact']['address']['state'];
            this.city = result.items[0]['contact']['address']['city'];
            this.postal = result.items[0]['contact']['address']['postalCode'];
            this.timeZone = result.items[0]['timeZone'];
            
            //location Input
            this.location.id = locationId;
            this.location.name = result.items[0]['name'];
            this.location.minutesPerMile = result.items[0]['minutesPerMile'];
            this.location.minutesPerStop = result.items[0]['minutesPerStop'];
            this.location.timeZoneId = result.items[0]['timeZone']['id'];
            this.location.contactId = result.items[0]['contact']['id'];

            //contact input
            this.contact.firstName = result.items[0]['contact']['firstName'];
            this.contact.lastName = result.items[0]['contact']['lastName'];
            this.contact.addressId = result.items[0]['contact']['address']['id'];
            this.contact.id = result.items[0]['contact']['id'];
            
            //address input
            this.address.addressLine1 = result.items[0]['contact']['address']['addressLine1'];
            this.address.addressLine2 = result.items[0]['contact']['address']['addressLine2'];
            //this.address.postalCodeId = this.postal['id'];
            this.address.id = result.items[0]['contact']['address']['id'];

            this.modal.show();
            //console.log(result.items[0]['contact']['address']['postalCode']);
            

        });
    }

    save(): void {
        this.saving = true;
        this.location.timeZoneId = this.timeZone.id;
        this.address.postalCodeId = this.postal.id;
        this._locationService.updateLocation(
            this.location
        ).pipe(finalize(() => { this.saving = false; }))
        .subscribe(result => {
            this._addressService.updateAddress(this.address).subscribe(result => {
            });
            this._contactService.updateContact(this.contact).subscribe(result => {
            });
            this.notify.info(this.l('SavedSuccessfully'));
            this.close();
            this.modalSave.emit(null);
        });
    }
    onShown(): void {
    }
    close(): void {
        this.active = false;
        this.modal.hide();
    }


    // filterPostal(event): void {
    //   this._postalCodeService.getPostalCode(
    //       undefined,
    //       event.query,
    //       undefined,
    //       undefined,
    //       undefined
    //   ).subscribe(result => {
    //       this.filteredPostal = result.items;
    //   });
    // }


    filterTimeZones(event): void {
      this._timeZoneService.getTimeZone(
          undefined,
          event.query,
          undefined,
          undefined,
          undefined
      ).subscribe(result => {
          this.filteredTimeZones = result.items;
      });
    }

    filterUser(event): void {
      this._userService.getUsers(
          event.query,
          undefined,
          undefined,
          undefined,
          undefined,
          undefined,
          undefined
      ).subscribe(result => {
          this.filteredUsers = result.items;
          console.log(this.filteredUsers);
      });
    }

}
