import { Component, Injector, OnInit, ViewChild, ViewEncapsulation } 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 { IncidentListDto, IncidentServiceProxy } from '@shared/service-proxies/service-proxies';
import { IncidentImageListDto, IncidentImageServiceProxy } from '@shared/service-proxies/service-proxies';
import { ImageListDto, ImageServiceProxy } from '@shared/service-proxies/service-proxies';

import { FileDownloadService } from '@shared/utils/file-download.service';
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 { FileUpload } from 'primeng/fileupload';
import { finalize } from 'rxjs/operators';
import { CreateIncidentModalComponent } from './create-incident-modal.component';
import * as _ from 'lodash';
@Component({
    templateUrl: 'incident.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})

export class IncidentComponent extends AppComponentBase implements OnInit {

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

    
    images: IncidentImageListDto[] = [];
    
    
    imageId: any;
    incidentImageId: any;

    //images: {
        //id: number;
        //path: string;
        //name: string;
        
    //} = <any>{};

    filters: {
        id: number;
        content: string;
        CurrentPage: number;
    } = <any>{};

    incidentId: any;
    constructor(
        injector: Injector,
        private _incidentService: IncidentServiceProxy,
        private _incidentImageService: IncidentImageServiceProxy,
        private _imageService: ImageServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(): void {

    }

    getIncidents(event?: LazyLoadEvent) {
          
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this.spinnerService.show();
        this._incidentService.getIncident(
            this.filters.id,
            undefined,
            this.filters.content,
            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;
            this.spinnerService.hide();
            
            this.incidentId =  this.primengTableHelper.records;           
                        

            //console.log(this.incidentId.id);
            
        });

    }

 

    deleteIncidentImage(incidentId): void {

            this._incidentImageService.getIncidentImage(
            undefined,
            undefined,
            incidentId,
            undefined,
            undefined,
            undefined

            ).subscribe(result => {

                if(result.totalCount == 0) {
                    console.log('nothing to delete');
                }
                else {


                                    this.incidentImageId = result.items;

                                    for(let item of this.incidentImageId) {
                                        //console.log(item.id);

                                        this._incidentImageService.deleteIncidentImage(item.id).subscribe(() => {
                                                                   
                                        });

                                        this._imageService.deleteImage(item.imageId).subscribe(() => {
                                                                   
                                        });
                                    }

                }
                
                
                
            });
            
             
        
    }
    
    createIncident(): void {
        this.createIncidentModal.show();
    }

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

    deleteIncident(incident: IncidentListDto): void {
            
        this.message.confirm(
            this.l('AreYouSure?'),
            isConfirmed => {
                if (isConfirmed) {
                    this._incidentService.deleteIncident(incident.id).subscribe(() => {
                        this.deleteIncidentImage(incident.id);
                        this.reloadPage();
                        this.notify.info(this.l('SuccessfullyDeleted'));
                        
                    });
                    
                }
            }
        );
    } 

 

    
   

}
