import { Component, ViewChild, Injector, Output, EventEmitter} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { AddressesServiceProxy, CreateOrEditAddressDto, UpdateAddressInput } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import * as moment from 'moment';

import { AddressFormComponent } from '@app/shared/layout/form/address-form.component';


@Component({
    selector: 'createOrEditAddressModal',
    templateUrl: './create-or-edit-address-modal.component.html'
})
export class CreateOrEditAddressModalComponent extends AppComponentBase {

    @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective;
    @ViewChild(AddressFormComponent, {static:false}) addressForm;


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

    active = false;
    saving = false;

    address: CreateOrEditAddressDto = new CreateOrEditAddressDto();
    address2: UpdateAddressInput = new UpdateAddressInput();
    
    postalCodeValue = '';
    geocodeLatitude = '';
    addressTypeName : any;
    postalCodeId : number;
    postalCodeCountry : string;


    constructor(
        injector: Injector,
        private _addressesServiceProxy: AddressesServiceProxy
    ) {
        super(injector);
    }

    show(addressId?: number): void {

        if (!addressId) {
            this.address = new CreateOrEditAddressDto();
            this.address.id = addressId;
            this.postalCodeValue = '';
            this.geocodeLatitude = '';
            this.addressTypeName = '';

            this.active = true;
            this.modal.show();
        } else {
            this._addressesServiceProxy.getAddressForEdit(addressId).subscribe(result => {
                
                this.address = result.address;

                this.postalCodeId = this.address.postalCodeId;
                this.postalCodeValue = result.postalCodeValue;
                this.geocodeLatitude = result.geocodeLatitude;
                this.addressTypeName = result.addressTypeName;

                this.active = true;
                this.modal.show();

            });
        }
    }

    save(): void {
            this.saving = true;
            this.address = this.addressForm.address
            this.address.postalCodeId = Number((<HTMLInputElement>document.getElementById('postalSelectInput')).value);
            this.address.addressTypeId = Number((<HTMLInputElement>document.getElementById('addressTypeSelectInput')).value);
            this._addressesServiceProxy.createOrEdit(this.address)
             .pipe(finalize(() => { this.saving = false;}))
             .subscribe((
             ) => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
             });
    }

        setPostalCodeIdNull() {
        this.address.postalCodeId = null;
        this.postalCodeValue = '';
    }
        setGeocodeIdNull() {
        this.address.geocodeId = null;
        this.geocodeLatitude = '';
    }
        setAddressTypeIdNull() {
        this.address.addressTypeId = null;
        this.addressTypeName = '';
    }


    close(): void {

        this.active = false;
        this.modal.hide();
    }
}
