import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrderServiceProxy, LocationListDto, LocationServiceProxy, TagTypeServiceProxy, TagTypeListDto, PostalCodeServiceProxy,  ShippingPackageListDto, ShippingPackageServiceProxy, PackageInput, PickupDetailInput, ContactInput, AddressInput, DestinationDetailInput, CreateNoteInput, CreateEmailInput, CreatePhoneInput, PostalCodeListDto, CreateOrderV2Input} from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import * as moment from 'moment';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'createOrderModal',
    templateUrl: './create-order-modal.component.html'
})
export class CreateOrderModalComponent extends AppComponentBase {

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

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

    active = false;
    saving = false;
    
    createInput: CreateOrderV2Input = new CreateOrderV2Input();  

    pickupDetailInput: PickupDetailInput = new PickupDetailInput(); 
    pickupContactInput: ContactInput = new ContactInput();
    pickupContactAddress: AddressInput = new AddressInput;
    
    destinationDetailInput: DestinationDetailInput = new DestinationDetailInput; 
    destinationContactInput: ContactInput = new ContactInput;
    destinationContactAddress: AddressInput = new AddressInput;

    locationFilter: LocationListDto = new LocationListDto();
    filteredLocation: any;

    tagTypeFilter: TagTypeListDto = new TagTypeListDto();
    filteredTagType: any;

    packageFilter: ShippingPackageListDto = new ShippingPackageListDto();
    filteredPackage: any;

    filteredCountries: any;
    filteredCities: any;
    filteredStates: any;
    filteredPostal: any;
    noteInput: string;

    packages: Array<PackageInput> = []; 
    pickUpTo: any;
    pickUpFrom: any;
    deliveryFrom: any;
    deliveryTo: any;
    pickupEta: Date;
    deliveryEta: Date;

    orderPackageInput: Object = new Object();
    addPackage: any;
    packageQuantity: number;

    // pickupcountryId: CountryListDto = new CountryListDto();
    // pickupcityId: CityListDto = new CityListDto();
    // pickupstateId: StateListDto = new StateListDto();
    pickuppostalId: PostalCodeListDto = new PostalCodeListDto();
    // destinationcountryId: CountryListDto = new CountryListDto();
    // destinationcityId: CityListDto = new CityListDto();
    // destinationstateId: StateListDto = new StateListDto();

    destinationpostalId: PostalCodeListDto = new PostalCodeListDto();
    locationInput: LocationListDto = new LocationListDto();
    pickupPhone: string;
    pickupEmail: string;
    destinationPhone: string;
    destinationEmail: string;
    constructor(
        injector: Injector,
        private _orderAppService: OrderServiceProxy,
        private _locationAppService: LocationServiceProxy,
        private _tagTypeAppService: TagTypeServiceProxy,
        private _postalCodeService: PostalCodeServiceProxy,
        private _packageService: ShippingPackageServiceProxy
    ) {
        super(injector);
    }

    show(): void {
        this.createInput = new CreateOrderV2Input();

        this.createInput.packageInput = Object();    
        // this.pickupcountryId = new CountryListDto();
        // this.pickupcityId = new CityListDto();
        // this.pickupstateId = new StateListDto();
        this.pickuppostalId = new PostalCodeListDto();
        // this.destinationcountryId = new CountryListDto();
        // this.destinationcityId = new CityListDto();
        // this.destinationstateId = new StateListDto();

        this.destinationpostalId = new PostalCodeListDto();
        this.locationInput = new LocationListDto();
        this.pickupPhone = "";
        this.pickupEmail = "";
        this.destinationPhone = "";
        this.destinationEmail = "";
        this.createInput.destinationDetailInput = new DestinationDetailInput();
        this.createInput.pickupDetailInput = new PickupDetailInput();
        this.createInput.noteInput = new CreateNoteInput();
        this.createInput.destinationDetailInput.email = new CreateEmailInput();
        this.createInput.destinationDetailInput.phone = new CreatePhoneInput();
        this.createInput.pickupDetailInput.email = new CreateEmailInput();
        this.createInput.pickupDetailInput.phone = new CreatePhoneInput();
        this.orderPackageInput = new Object();
        this.active = true;
        this.modal.show();
    }

    onShown(): void {

    }

    save(): void {
        this.saving = true;

        this.pickupContactInput.addressInput = this.pickupContactAddress;
        // this.pickupContactInput.addressInput.countryId = this.pickupcountryId.id;
        // this.pickupContactInput.addressInput.cityId = this.pickupcityId.id;
        // this.pickupContactInput.addressInput.stateId = this.pickupstateId.id;
        this.pickupContactInput.addressInput.postalCodeId = this.pickuppostalId.id
        this.createInput.pickupDetailInput.contactInput = this.destinationContactInput;
        this.createInput.pickupDetailInput.email.value = this.pickupEmail;
        this.createInput.pickupDetailInput.phone.value = this.pickupPhone;

        this.destinationContactInput.addressInput = this.destinationContactAddress;
        // this.destinationContactInput.addressInput.countryId = this.destinationcountryId.id;
        // this.destinationContactInput.addressInput.cityId = this.destinationcityId.id;
        // this.destinationContactInput.addressInput.stateId = this.destinationstateId.id;
        this.destinationContactInput.addressInput.postalCodeId = this.destinationpostalId.id
        this.createInput.destinationDetailInput.contactInput = this.destinationContactInput;
        this.createInput.destinationDetailInput.email.value = this.destinationEmail;
        this.createInput.destinationDetailInput.phone.value = this.destinationPhone;
        
        this.createInput.locationId = this.locationInput.id;
        this.createInput.pickupDetailInput.etaStart = moment.utc(moment(this.pickupEta).format('YYYY-MM-DD') + ' ' + moment(this.pickUpFrom).format('HH:mm:ss'));
        this.createInput.pickupDetailInput.etaEnd = moment.utc(moment(this.pickupEta).format('YYYY-MM-DD') + ' ' + moment(this.pickUpTo).format('HH:mm:ss'));

        this.createInput.destinationDetailInput.etaStart = moment.utc(moment(this.deliveryEta).format('YYYY-MM-DD') + ' ' + moment(this.deliveryFrom).format('HH:mm:ss'));
        this.createInput.destinationDetailInput.etaEnd = moment.utc(moment(this.deliveryEta).format('YYYY-MM-DD') + ' ' + moment(this.deliveryTo).format('HH:mm:ss'));

        this.createInput.noteInput.content = this.noteInput;

        this.createInput.packageInput = this.packages;


        this._orderAppService.createOrderV2(this.createInput)
            .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();
    }

    filterLocation(event): void {
        this._locationAppService.getLocation(
            undefined,
            event.query,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredLocation = result.items;
        });
      }

    filterTagType(event): void {
        this._tagTypeAppService.getTagType(
            undefined,
            event.query,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredTagType = result.items;
        });
      }



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

    // filterStates(event): void {
    //     this._stateService.getState(
    //         undefined,
    //         event.query,
    //         undefined,
    //         undefined,
    //         undefined
    //     ).subscribe(result => {
    //         this.filteredStates = result.items;
    //     });
    // }



    filterPackage(event): void {
        this._packageService.getShippingPackage(
            undefined,
            event.query,
            undefined,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredPackage = result.items;
        });
    }


    addPackages(): void {
        let toAdd = new PackageInput();
        toAdd.packageName = this.addPackage.name;
        toAdd.packageId = this.addPackage.id;
        toAdd.quantity = this.packageQuantity;
        this.addPackage = '';
        this.packageQuantity = undefined;
        // if(this.packages.c == 0){

        // }
        this.packages.push(toAdd);
    };

    removePackage(toDel): void {
        _.remove(this.packages, toDel);
    };


}
