import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { TagTypeServiceProxy, TagTypeListDto } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { finalize } from 'rxjs/operators';
import { CreateTagTypeModalComponent } from './create-tag-types-modal.component';

@Component({
    templateUrl: './tag-type.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class TagTypeComponent extends AppComponentBase {

    @ViewChild('dataTable', {static: true}) dataTable: Table;
    @ViewChild('paginator', {static: true}) paginator: Paginator;
    // @ViewChild('ExcelFileUpload') excelFileUpload: FileUpload;
    @ViewChild('createTagTypeModal', {static: false}) createTagTypeModal: CreateTagTypeModalComponent;

    uploadUrl: string;

    //Filters
    advancedFiltersAreShown = false;
    filterText = '';

    constructor(
        injector: Injector,
        private _tagTypeAppService: TagTypeServiceProxy,
        private _activatedRoute: ActivatedRoute
    ) {
        super(injector);
        // this.filterText = this._activatedRoute.snapshot.queryParams['filterText'] || ''; 
    }

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

        this.spinnerService.show();

        this._tagTypeAppService.getTagType(
            undefined,
            this.filterText,
            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;
        });
    }

    createTagType(): void {
        this.createTagTypeModal.show();
    }


    deleteTagType(tagType: TagTypeListDto): void {
        this.message.confirm(
            this.l('AreYouSure'),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._tagTypeAppService.deleteTagType(tagType.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }

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

}
