import { Component, Injector, ViewEncapsulation, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { RouteScheduleDetailsServiceProxy, RouteScheduleDetailDto  } 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 { CreateOrEditRouteScheduleDetailModalComponent } from './create-or-edit-routeScheduleDetail-modal.component';
import { ViewRouteScheduleDetailModalComponent } from './view-routeScheduleDetail-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: './routeScheduleDetails.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class RouteScheduleDetailsComponent extends AppComponentBase {

    @ViewChild('createOrEditRouteScheduleDetailModal', { static: true }) createOrEditRouteScheduleDetailModal: CreateOrEditRouteScheduleDetailModalComponent;
    @ViewChild('viewRouteScheduleDetailModalComponent', { static: true }) viewRouteScheduleDetailModal: ViewRouteScheduleDetailModalComponent;
    @ViewChild('entityTypeHistoryModal', { static: true }) entityTypeHistoryModal: EntityTypeHistoryModalComponent;
    @ViewChild('dataTable', { static: true }) dataTable: Table;
    @ViewChild('paginator', { static: true }) paginator: Paginator;

    advancedFiltersAreShown = false;
    filterText = '';
    routeCodeFilter = '';
    maxEstimatedTimeOfArrivalFilter : moment.Moment;
		minEstimatedTimeOfArrivalFilter : moment.Moment;
    maxCutOffFilter : moment.Moment;
		minCutOffFilter : moment.Moment;
    automaticFilter = -1;
    willCallFilter = -1;
    maxOorRateFilter : number;
		maxOorRateFilterEmpty : number;
		minOorRateFilter : number;
		minOorRateFilterEmpty : number;
    maxEffectivityDateFilter : moment.Moment;
		minEffectivityDateFilter : moment.Moment;
    maxRouteScheduleIdFilter : number;
		maxRouteScheduleIdFilterEmpty : number;
		minRouteScheduleIdFilter : number;
		minRouteScheduleIdFilterEmpty : number;
    maxRouteTypeIdFilter : number;
		maxRouteTypeIdFilterEmpty : number;
		minRouteTypeIdFilter : number;
		minRouteTypeIdFilterEmpty : number;


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

    constructor(
        injector: Injector,
        private _routeScheduleDetailsServiceProxy: RouteScheduleDetailsServiceProxy,
        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;
    }

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

        this.primengTableHelper.showLoadingIndicator();

        this._routeScheduleDetailsServiceProxy.getAll(
            this.filterText,
            this.routeCodeFilter,
            this.maxEstimatedTimeOfArrivalFilter,
            this.minEstimatedTimeOfArrivalFilter,
            this.maxCutOffFilter,
            this.minCutOffFilter,
            this.automaticFilter,
            this.willCallFilter,
            this.maxOorRateFilter == null ? this.maxOorRateFilterEmpty: this.maxOorRateFilter,
            this.minOorRateFilter == null ? this.minOorRateFilterEmpty: this.minOorRateFilter,
            this.maxEffectivityDateFilter,
            this.minEffectivityDateFilter,
            this.maxRouteScheduleIdFilter == null ? this.maxRouteScheduleIdFilterEmpty: this.maxRouteScheduleIdFilter,
            this.minRouteScheduleIdFilter == null ? this.minRouteScheduleIdFilterEmpty: this.minRouteScheduleIdFilter,
            this.maxRouteTypeIdFilter == null ? this.maxRouteTypeIdFilterEmpty: this.maxRouteTypeIdFilter,
            this.minRouteTypeIdFilter == null ? this.minRouteTypeIdFilterEmpty: this.minRouteTypeIdFilter,
            undefined,
            undefined,
            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());
    }

    createRouteScheduleDetail(): void {
        this.createOrEditRouteScheduleDetailModal.show();
    }

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

    deleteRouteScheduleDetail(routeScheduleDetail: RouteScheduleDetailDto): void {
        this.message.confirm(
            '',
            (isConfirmed) => {
                if (isConfirmed) {
                    this._routeScheduleDetailsServiceProxy.delete(routeScheduleDetail.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }

    exportToExcel(): void {
        this._routeScheduleDetailsServiceProxy.getRouteScheduleDetailsToExcel(
        this.filterText,
            this.routeCodeFilter,
            this.maxEstimatedTimeOfArrivalFilter,
            this.minEstimatedTimeOfArrivalFilter,
            this.maxCutOffFilter,
            this.minCutOffFilter,
            this.automaticFilter,
            this.willCallFilter,
            this.maxOorRateFilter == null ? this.maxOorRateFilterEmpty: this.maxOorRateFilter,
            this.minOorRateFilter == null ? this.minOorRateFilterEmpty: this.minOorRateFilter,
            this.maxEffectivityDateFilter,
            this.minEffectivityDateFilter,
            this.maxRouteScheduleIdFilter == null ? this.maxRouteScheduleIdFilterEmpty: this.maxRouteScheduleIdFilter,
            this.minRouteScheduleIdFilter == null ? this.minRouteScheduleIdFilterEmpty: this.minRouteScheduleIdFilter,
            this.maxRouteTypeIdFilter == null ? this.maxRouteTypeIdFilterEmpty: this.maxRouteTypeIdFilter,
            this.minRouteTypeIdFilter == null ? this.minRouteTypeIdFilterEmpty: this.minRouteTypeIdFilter,
            undefined,
        )
        .subscribe(result => {
            this._fileDownloadService.downloadTempFile(result);
         });
    }
}
