import { Component, EventEmitter, Injector, ViewChild, Output } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { UpdateAddressInput, PostalCodeServiceProxy, AddressServiceProxy,AddressTypeServiceProxy,AddressTypeListDto} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';
import { finalize } from 'rxjs/operators';

@Component({
    templateUrl: './edit-address-modal.component.html',
    selector: 'updateAddressModal',
})
export class UpdateAddressModalComponent extends AppComponentBase {

    @ViewChild('editModal', {static: false}) modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    address:UpdateAddressInput = new UpdateAddressInput();
    selectedCountry: any;
    filteredCountries: any;
    selectedCity: any;
    filteredCities: any;
    selectedState: any;
    filteredStates: any;
    postal: any;
    filteredPostal: any;
    addressType:any;
    addressTypes:AddressTypeListDto[];
    filteredAddressTypes:any;
    

    constructor(
        injector: Injector,
        private _addressService: AddressServiceProxy,

        // private _countryService: CountryServiceProxy,
        // private _cityService: CityServiceProxy,
        // private _stateService: StateServiceProxy,

        private _postalCodeService: PostalCodeServiceProxy,
        private _addressTypeService: AddressTypeServiceProxy
    ) {
        super(injector);
    }

    show(addressId: any): void {
        this.active = true;
        // this.addressType.name = null;

            this._addressService.getAddress(
            addressId,undefined,undefined,undefined,undefined,undefined,undefined,undefined,undefined).subscribe(result => {
            this.address.postalCodeId = result.items[0].postalCode.id;
            this.address.geocodeId = null;
            this.address.id = result.items[0].id;

            this.selectedCity = result.items[0].postalCode.city;
            this.selectedState = result.items[0].postalCode.state;
            this.selectedCountry = result.items[0].postalCode.country;

            this.address.addressLine1 = result.items[0].addressLine1;
            this.address.addressLine2 = result.items[0].addressLine2;

            // for discussion
            this.address.geocodeId = null;
            
            this.address.addressTypeId = result.items[0].addressType === undefined ? null : result.items[0].addressType.id;
            // result.items[0]['addressType']  === undefined? this.addressType = '' : this.addressType = result.items[0]['addressType'];
            // result.items[0]['addressType'] === undefined? this.addressType = 'null' : this.addressType = result.items[0]['addressType'];
            this.modal.show();
        });
        
        
    }

    onShown(): void {
        document.getElementById('address1Input').focus();
        // let maxcount = 1000;
        // this.filterAddressTypes(maxcount);
        // this.filterCities(maxcount);
        // this.filterPostal(maxcount);
        // this.filterCountries(maxcount);
        // this.filterStates(maxcount);
        //  $('.kt-select2').select2();


         
    }

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

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

    // filterPostal(maxcount): void {
    //     this._postalCodeService.getPostalCode(
    //         undefined,
    //         undefined,
    //         undefined,
    //         maxcount,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredPostal = result.items;
    //     });
    //   }
  
    //   filterCountries(maxcount): void {
    //     this._countryService.getCountry(
    //         undefined,
    //         undefined,
    //         undefined,
    //         maxcount,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredCountries = result.items;
    //     });
    //   }
  
    //   filterCities(maxcount): void {
    //     this._cityService.getCity(
    //         undefined,
    //         undefined,
    //         undefined,
    //         maxcount,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredCities = result.items;
    //     });
    //   }
  
    //   filterStates(maxcount): void {
    //     this._stateService.getState(
    //         undefined,
    //         undefined,
    //         undefined,
    //         maxcount,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredStates = result.items;
    //     });
    //   }

    //   filterAddressTypes(maxcount): void {
    //     this._addressTypeService.getAddressType(
    //         undefined,
    //         undefined,
    //         undefined,
    //         maxcount,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredAddressTypes = result.items;
    //     });
    //   }
}
