import { Component, Injector, ViewEncapsulation, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { RouteSchedulesServiceProxy, RouteScheduleDto  } from '@shared/service-proxies/service-proxies';
import { NotifyService } from '@abp/notify/notify.service';
import { AppComponentBase } from '@shared/common/app-component-base';
import { TokenAuthServiceProxy } from '@shared/service-proxies/service-proxies';
import { CreateOrEditRouteScheduleModalComponent } from './create-or-edit-routeSchedule-modal.component';
import { ViewRouteScheduleModalComponent } from './view-routeSchedule-modal.component';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { EntityTypeHistoryModalComponent } from '@app/shared/common/entityHistory/entity-type-history-modal.component';
import * as _ from 'lodash';
import * as moment from 'moment';

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

    @ViewChild('createOrEditRouteScheduleModal', { static: true }) createOrEditRouteScheduleModal: CreateOrEditRouteScheduleModalComponent;
    @ViewChild('viewRouteScheduleModalComponent', { static: true }) viewRouteScheduleModal: ViewRouteScheduleModalComponent;
    @ViewChild('entityTypeHistoryModal', { static: true }) entityTypeHistoryModal: EntityTypeHistoryModalComponent;
    @ViewChild('dataTable', { static: true }) dataTable: Table;
    @ViewChild('paginator', { static: true }) paginator: Paginator;

    advancedFiltersAreShown = false;
    filterText = '';
    maxClinicIdFilter : number;
		maxClinicIdFilterEmpty : number;
		minClinicIdFilter : number;
		minClinicIdFilterEmpty : number;


    _entityTypeFullName = 'SprintTek.Laboratory.RouteSchedule';
    entityHistoryEnabled = false;

    constructor(
        injector: Injector,
        private _routeSchedulesServiceProxy: RouteSchedulesServiceProxy,
        private _notifyService: NotifyService,
        private _tokenAuth: TokenAuthServiceProxy,
        private _activatedRoute: ActivatedRoute,
        private _fileDownloadService: FileDownloadService
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this.entityHistoryEnabled = this.setIsEntityHistoryEnabled();
    }

    private setIsEntityHistoryEnabled(): boolean {
        let customSettings = (abp as any).custom;
        return customSettings.EntityHistory && customSettings.EntityHistory.isEnabled && _.filter(customSettings.EntityHistory.enabledEntities, entityType => entityType === this._entityTypeFullName).length === 1;
    }

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

        this.primengTableHelper.showLoadingIndicator();

        this._routeSchedulesServiceProxy.getAll(
            parseInt(this.filterText),
            this.maxClinicIdFilter == null ? this.maxClinicIdFilterEmpty: this.maxClinicIdFilter,
            this.minClinicIdFilter == null ? this.minClinicIdFilterEmpty: this.minClinicIdFilter,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getSkipCount(this.paginator, event),
            this.primengTableHelper.getMaxResultCount(this.paginator, event)
        ).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }

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

    createRouteSchedule(): void {
        this.createOrEditRouteScheduleModal.show();
    }

    showHistory(routeSchedule: RouteScheduleDto): void {
        this.entityTypeHistoryModal.show({
            entityId: routeSchedule.id.toString(),
            entityTypeFullName: this._entityTypeFullName,
            entityTypeDescription: ''
        });
    }

    deleteRouteSchedule(routeSchedule: RouteScheduleDto): void {
        this.message.confirm(
            '',
            (isConfirmed) => {
                if (isConfirmed) {
                    this._routeSchedulesServiceProxy.delete(routeSchedule.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }

    exportToExcel(): void {
        this._routeSchedulesServiceProxy.getRouteSchedulesToExcel(
        this.filterText,
            this.maxClinicIdFilter == null ? this.maxClinicIdFilterEmpty: this.maxClinicIdFilter,
            this.minClinicIdFilter == null ? this.minClinicIdFilterEmpty: this.minClinicIdFilter,
        )
        .subscribe(result => {
            this._fileDownloadService.downloadTempFile(result);
         });
    }
}
