import { Component, Injector, Input, Output, EventEmitter, OnInit, AfterViewInit } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateAddressInput, AddressTypeServiceProxy, PostalCodeServiceProxy, UpdateAddressInput } from '@shared/service-proxies/service-proxies';



@Component({
    selector: 'addressForm',
    templateUrl: './address-form.component.html',
    styleUrls: ['address-form.component.less']
})
export class AddressFormComponent extends AppComponentBase {

    @Input() address: UpdateAddressInput = new UpdateAddressInput();
    @Input('selectedCountry') selectedCountry: string;
    @Input('selectedCity') selectedCity: string;
    @Input('selectedState') selectedState: string;
    @Input('postalCodeId') selectedPostalCodeId: number; 
    @Input('postalCodeValue') selectedPostalCodeValue: string;
    // @Input('address1Input') address1InputValue:string;
    // @Input('address2Input') address2InputValue:string;
    // @Input('addressType') addressTypeValue: string;

    

    constructor(
        injector: Injector,

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

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

    filteredCountries: any;
    filteredCities: any;
    filteredStates: any;
    filteredPostal: any;
    filteredAddressType: any;
    selectedPostalCode: string;

    filterChange = false

    ngOnInit() {
        var that = this;
        $('.kt-select2').select2();
        let maxcount = 1000;
        this.filterAddressTypes(maxcount);
        this.filterPostal();
        this.selectedPostalCode=this.selectedPostalCodeValue;
        $("#postalSelectInput").change(function () {
            that.selectedPostalCode = $("#postalSelectInput option:selected").text();
            that.selectedPostalCode = $.trim(that.selectedPostalCode)
            that.filterChange = true;
            that.filterCountries(that.selectedPostalCode);
            that.filterStates(that.selectedPostalCode);
            that.filterCities(that.selectedPostalCode)
        })
    }

    ngAfterViewInit(){
        // this.filterCountries(this.selectedPostalCodeValue)
        // console.log(this.selectedPostalCodeValue)
    }

    ngOnChanges(){
        // this.filterPostal();
        if(this.selectedPostalCodeValue){
            this.filterCountries(this.selectedPostalCodeValue)
            this.filterStates(this.selectedPostalCodeValue)
            this.filterCities(this.selectedPostalCodeValue) 
        }
    }

    filterPostal(): void {
        this._postalCodeService.getDistinctPostalCode(
            undefined
        ).subscribe(result => {
            this.filteredPostal = result.items;
        });
    }

    filterCountries(postalCode): void {
        this._postalCodeService.getDistinctCountry(
            postalCode,
            undefined
        ).subscribe(result => {
            this.filteredCountries = result.items;
            $("#selectedCountry").text(this.filteredCountries[0]['country']);
        });
    }

    filterStates(postalCode): void {
        this._postalCodeService.getDistinctState(
            postalCode,
            undefined
        ).subscribe(result => {
            this.filteredStates = result.items;
            $("#selectedState").text(this.filteredStates[0]['state']);
        });
    }

    filterCities(postalCode): void {
        this._postalCodeService.getDistinctCity(
            postalCode,
            undefined
        ).subscribe(result => {
            this.filteredCities = result.items;
            $("#selectedCity").text(this.filteredCities[0]['city']);
        });
    }

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