import { Component, Injector, OnInit, ViewChild, ViewEncapsulation, Input, ViewChildren } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppConsts } from '@shared/AppConsts';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ControllerDriverListDto, ControllerDriverListServiceProxy} from '@shared/service-proxies/service-proxies';

import * as moment from 'moment';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { HttpClient } from '@angular/common/http';
import { finalize } from 'rxjs/operators';

@Component({
    templateUrl: './driver-details.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()],
    providers: [ControllerDriverListServiceProxy],
    styles: ['.table-title {margin: 0 !important;min-height: auto !important;}']
})

export class DriverDetailComponent extends AppComponentBase  implements OnInit{
    @ViewChild('dataTable', {static: true}) dataTable: Table;
    @ViewChild('paginator', {static: true}) paginator: Paginator;
  
    locationFilter : number;
    userId: number;
    driverName: string;
    routeIds: any;

	constructor(
        injector: Injector,
        private _driverDetailService: ControllerDriverListServiceProxy,
        private activatedRoute: ActivatedRoute,
        
    ) {
        super(injector);
    }

    ngOnInit(): void {
    
        this.activatedRoute.paramMap.subscribe(params => {
            this.locationFilter = +params.get("location");
            this.userId = +params.get("userid");
        });

        this._driverDetailService.getDriverEntity(
            undefined,
            undefined,
            undefined,
            undefined,
            this.userId,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ).pipe(finalize(() => this.spinnerService.hide())
        ).subscribe(result => {
            this.driverName  = result.fullName
        });
    }
  
    getDriverDetails(event?: LazyLoadEvent): void
    {
        var key;
        this.routeIds = [];
       
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
    
        this.spinnerService.show();
       
        this._driverDetailService.getDriverDetails(
            undefined,
            undefined,
            undefined,
            undefined,
            this.userId,
            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 => {
            for (key in result.items)
            {
                this.routeIds.push(result.items[key].routeId);
            }
      
            this._driverDetailService
                .getStopCount(this.routeIds)
                .subscribe(stopResult =>
                {   
                
                    if(stopResult !== null)
                    {
                        if(Object.keys(stopResult).length > 0)
                        {
                            for (key in stopResult)
                            {   
                                
                                $('#route' + stopResult[key].routeId).html(stopResult[key].stops.toString());
                            
                                var time = $('#tt'+ stopResult[key].routeId).html();

                                if(time != null)
                                {
                                    var a = time.split(':');
                                    var minutes = (+a[0]) * 60 + (+a[1]);
                                    
                                    $('#sph' + stopResult[key].routeId).html((((60/minutes)*stopResult[key].stops).toFixed(2).toString()));
                                }
                                
                            }
                        }
                    }
                });
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.spinnerService.hide();
        });
    }

    
}