import { Component, Input } from '@angular/core';
import { JhiEventManager } from 'ng-jhipster';

import { I<%= entityAngularName %> } from 'app/shared/model/<%= entityModelFileName %>.model';
import { <%= entityAngularName %>Service } from './<%= entityFileName %>.service';
import { ActivatedRoute, Router } from '@angular/router';
import { PROGRESS_STATE } from 'app/shared/constants/load.constants';

@Component({
    selector: '<%= jhiPrefixDashed %>-<%= entityFileName %>-delete-dialog',
    templateUrl: './<%= entityFileName %>-delete-dialog.component.html'
})
export class <%= entityAngularName %>DeleteDialogComponent {
    @Input() item: I<%= entityAngularName %>;
    @Input() list: Array<I<%= entityAngularName %>>;
    @Input() callback: Function;
    @Input() close: Function;
    private progressIndex: number;
    private progressValue: number;
    private progressState: string;
    private _opened: any;

    constructor(
        private <%= entityInstance %>Service: <%= entityAngularName %>Service,
        private eventManager: JhiEventManager,
        private route: ActivatedRoute,
        private router: Router
    ) {
        this.progressIndex = 0;
        this.progressValue = 0;
        this.progressState = PROGRESS_STATE.READY;
        this.opened = true;
    }

    confirmDelete(id: <% if (pkType === 'String') { %>string<% } else { %>number<% } %>) {
        this.<%= entityInstance %>Service.delete(id).subscribe(response => {
            this.eventManager.broadcast({
                name: '<%= entityInstance %>ListModification',
                content: 'Deleted an <%= entityInstance %>'
            });
            this.callback();
            this.opened = false;
        });
    }

    get opened() {
        return this._opened;
    }

    set opened(value: any) {
        this._opened = value;
        if (!value) {
            this.close();
        }
    }

    confirmBatchDelete() {
        const loop = () => {
            this.<%= entityInstance %>Service.delete(this.list[this.progressIndex].id).subscribe(
                response => {
                    this.progressValue++;
                    if (this.progressIndex + 1 < this.list.length) {
                        this.progressIndex++;
                        loop();
                    } else {
                        this.progressState = PROGRESS_STATE.SUCCESS;
                        this.eventManager.broadcast({
                            name: '<%= entityInstance %>ListModification',
                            content: 'Deleted an <%= entityInstance %>'
                        });
                        this.callback();
                    }
                },
                error => {
                    this.progressState = PROGRESS_STATE.ERROR;
                    this.eventManager.broadcast({
                        name: '<%= entityInstance %>ListModification',
                        content: 'Deleted an <%= entityInstance %>'
                    });
                }
            );
        };
        this.progressState = PROGRESS_STATE.IN_PROGRESS;
        loop();
    }
}
