import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
import { AppConsts } from '@shared/AppConsts';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { EntityDtoOfInt64, ContactServiceProxy, ContactListDto } from '@shared/service-proxies/service-proxies';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { CreateContactModalComponent } from './create-contact-modal.component';
import { HttpClient } from '@angular/common/http';
import { FileUpload } from 'primeng/fileupload';
import { finalize } from 'rxjs/operators';

@Component({
    templateUrl: './contacts.component.html',
    encapsulation: ViewEncapsulation.None,
   // styleUrls: ['./contacts.component.less'],
    animations: [appModuleAnimation()]
})
export class ContactComponent extends AppComponentBase {

    @ViewChild('createOrEditUserModal', {static: false}) createContactModal: CreateContactModalComponent;
    @ViewChild('dataTable', {static: true}) dataTable: Table;
    @ViewChild('paginator', {static: true}) paginator: Paginator;

    advancedFiltersAreShown = false;

    //Filters
    filters: {
        filter: string
        id: number;
        firstName: string;
        lastName: string;
        addressLine1Filter: string;
        addressLine2Filter: string;
        cityFilter: string;
        stateFilter: string;
        countryFilter: string;
        postalCodeValueFilter: string;
    } = <any>{};

    postalCodeId: number;
    nameFilter: string;

    constructor(
        injector: Injector,
        private _contactService: ContactServiceProxy,
        private _fileDownloadService: FileDownloadService,
        private _activatedRoute: ActivatedRoute,
        private _httpClient: HttpClient,
        private _router: Router,
    ) {
        super(injector);
    }

    exportToExcel(): void {
        this._contactService.getContactsToExcel(
            this.filters.filter,
            this.filters.firstName,
            this.filters.lastName,
            this.filters.addressLine1Filter,
            this.filters.addressLine2Filter,
            this.filters.postalCodeValueFilter,
            this.filters.countryFilter,
            this.filters.stateFilter,
            this.filters.cityFilter
        ) .subscribe(result => {
            this._fileDownloadService.downloadTempFile(result);
         });
    }

    getContact(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }

        this.spinnerService.show();

        this._contactService.getContact(
            this.filters.id,
            undefined,
            undefined,
            this.postalCodeId,
            this.filters.postalCodeValueFilter,
            this.filters.stateFilter,
            this.filters.cityFilter,
            this.filters.countryFilter,
            this.filters.addressLine1Filter,
            this.filters.addressLine2Filter,
            this.filters.filter,
            undefined,
            this.filters.firstName,
            this.filters.lastName,
            undefined,
            undefined,
            undefined,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.spinnerService.hide())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.spinnerService.hide();
            console.log(result.items[0].address.postalCode.state);
            
        });


    }

    reloadPage(): void {
        this.paginator.changePage(this.paginator.getPage());
    }

    deleteContact(contact: ContactListDto): void {

        this.message.confirm(
            this.l('DeleteWarningMessage', contact.firstName+" "+contact.lastName),
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._contactService.deleteContact(contact.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.info(this.l('SuccessfullyDeleted'));

                        });
                }
            }
        );
    }

    createOrEditContact(url: any, id?: number){
        if(id != null){
            var myurl = `${url}/${id}`;
        }else{
            var myurl = `${url}`;
        }
        console.log(myurl);
        this._router.navigateByUrl(myurl);
    }
}
