import {
    Component,
    Injector,
    ViewChild,
    ViewEncapsulation
} from '@angular/core';
import {
    ActivatedRoute
} from '@angular/router';
import {
    AppConsts
} from '@shared/AppConsts';
import {
    AppComponentBase
} from '@shared/common/app-component-base';
import {
    EntityDtoOfInt64,
    RouteTemplateListDto,
    RouteServiceProxy,
    LocationServiceProxy,
    RouteTemplateServiceProxy
} from '@shared/service-proxies/service-proxies';
import {
    appModuleAnimation
} from '@shared/animations/routerTransition';
import {
    LazyLoadEvent
} from 'primeng/components/common/lazyloadevent';
import {
    Paginator
} from 'primeng/components/paginator/paginator';
import {
    Table
} from 'primeng/components/table/table';
import {
    CreateOrEditRoutesTemplateModalComponent
} from './create-or-edit-routestemplate-modal.component';
import {
    HttpClient
} from '@angular/common/http';
import {
    FileUpload
} from 'primeng/fileupload';
import {
    finalize
} from 'rxjs/operators';

@Component({
    templateUrl: './routestemplate.component.html',
    animations: [appModuleAnimation()],
    encapsulation: ViewEncapsulation.None
})

export class RoutesTemplateComponent extends AppComponentBase {

    @ViewChild('createOrEditRouteTemplateModal', {static: false}) createOrEditRouteTemplateModal: CreateOrEditRoutesTemplateModalComponent;
    @ViewChild('dataTable', {static: true}) dataTable: Table;
    @ViewChild('paginator', {static: true}) paginator: Paginator;

    filterText: string;
    filterId: number;
    constructor(
        injector: Injector,
        private _routeServiceProxy: RouteServiceProxy,
        private _activatedRoute: ActivatedRoute,
        private _locationServiceProxy: LocationServiceProxy,
        private _routeTemplateServiceProxy: RouteTemplateServiceProxy,
        private _httpClient: HttpClient
    ) {
        super(injector);
        // this.getUsers();    
    }

    // getUsers(event?) {
    //     this._userServiceProxy.getUsers(
    //         undefined,
    //         undefined,
    //         undefined,
    //         true,
    //         this.primengTableHelper.getSorting(this.dataTable),
    //         this.primengTableHelper.getMaxResultCount(this.paginator, event),
    //         this.primengTableHelper.getSkipCount(this.paginator, event)
    //     ).pipe(finalize(() => this.spinnerService.hide())).subscribe(result => {
    //         console.log(result);
    //     });
    // }

    /**** ROUTES MODULE ****/
    getRouteTemplateSearch(event ? : LazyLoadEvent) {
        this.spinnerService.show();
        this._routeTemplateServiceProxy.getRouteTemplatePaged(undefined,undefined,undefined,this.filterText,undefined,undefined,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                console.log(result);
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.spinnerService.hide();
            });
    }
    getRoutesTemplate(event ? : LazyLoadEvent) {
        this.spinnerService.show();

        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.spinnerService.show();
        this._routeTemplateServiceProxy.getRouteTemplatePaged(undefined, undefined, undefined, undefined, undefined, undefined,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                console.log(result);
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.spinnerService.hide();
            });
    }
    createRoutesTemplate(): void {
        // this.spinnerService.show();
        this.createOrEditRouteTemplateModal.create = true;
        this.createOrEditRouteTemplateModal.show(null, this.primengTableHelper);
    }
    updateRouteTemplate(id: number): void {
        // this.spinnerService.show();
        this.createOrEditRouteTemplateModal.create = false;
        this.createOrEditRouteTemplateModal.show(id, this.primengTableHelper);

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

    deleteRouteTemplate(routeTemplate: RouteTemplateListDto): void {
        // if (ro === AppConsts.userManagement.defaultAdminUserName) {
        //     this.message.warn(this.l('{0}UserCannotBeDeleted', AppConsts.userManagement.defaultAdminUserName));
        //     return;
        // }

        this.message.confirm(
            this.l('UserDeleteWarningMessage', routeTemplate.name),
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._routeTemplateServiceProxy.deleteRouteTemplate(routeTemplate.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }
}