import { Component, OnInit, OnDestroy } from '@angular/core';
import { HttpErrorResponse, <% if (pagination !== 'no') { %> HttpHeaders, <% } %>HttpResponse } from '@angular/common/http';
<%_ if (pagination === 'pagination' || pagination === 'pager') { _%>
import { ActivatedRoute, Router } from '@angular/router';
<%_ } else if (searchEngine === 'elasticsearch') { _%>
import { ActivatedRoute, Router } from '@angular/router';
<%_ } _%>
import { Subscription } from 'rxjs/Subscription';
import { JhiEventManager, <% if (pagination !== 'no') { %>JhiParseLinks, <% } %>JhiAlertService<% if (fieldsContainBlob) { %>, JhiDataUtils<% } %> } from 'ng-jhipster';

import {
    I<%= entityAngularName %>,
    <%_ for (idx in fields) { 
        if (searchEngine === 'elasticsearch' && fields[idx].fieldIsEnum) { _%>
    <%=fields[idx].fieldType%>,
    <%_ }} _%>
} from 'app/shared/model/<%= entityModelFileName %>.model';
import { Principal } from 'app/core';
import { ITEMS_PER_PAGE } from 'app/shared';
import { Subject } from 'rxjs/Subject';
import { ClrDatagridSortOrder, ClrDatagridStateInterface } from '@clr/angular';
import { SEARCH_DEBOUNCE_TIME } from 'app/shared/constants/load.constants';
import { <%= entityAngularName %>Service } from './<%= entityFileName %>.service';
import { <%= entityAngularName %>DeleteDialogService } from './<%= entityFileName %>-delete-dialog.service';
import { <%= entityAngularName %>SelectDialogService } from './<%= entityFileName %>-select-dialog.service';

import { decodeFiltersQueryParam, encodeFiltersQueryParam, resolveFilter} from 'app/shared/util/filter-util';

@Component({
    selector: '<%= jhiPrefixDashed %>-<%= entityFileName %>',
    templateUrl: './<%= entityFileName %>.component.html'
})
export class <%= entityAngularName %>Component implements OnInit, OnDestroy {
    <%_ if (pagination === 'pagination' || pagination === 'pager') { _%>
<%- include('pagination-template', {toArrayString: toArrayString}); -%>
    <%_ } else if (pagination === 'no') { _%>
<%- include('no-pagination-template', {toArrayString: toArrayString}); -%>
    <%_ } _%>

    ngOnInit() {
        // modify for Clarity
        <%_ if (pagination === 'no') { _%>
        this.loadAll();
        <%_ } _%>
        this.principal.identity().then(account => {
            this.currentAccount = account;
        });
        this.registerChangeIn<%= entityClassPlural %>();
    }

    ngOnDestroy() {
        // fixed router into error
        if (this.eventSubscriber) {
            this.eventManager.destroy(this.eventSubscriber);
        }
        // this.eventManager.destroy(this.eventSubscriber);
    }

    trackId(index: number, item: I<%= entityAngularName %>) {
        return item.id;
    }
    <%_ if (fieldsContainBlob) { _%>
    byteSize(field) {
        return this.dataUtils.byteSize(field);
    }

    openFile(contentType, field) {
        return this.dataUtils.openFile(contentType, field);
    }
    <%_ } _%>
    <%_
    let eventCallBack = 'this.loadAll()';
    if (pagination === 'infinite-scroll') {
        eventCallBack = 'this.reset()';
    } _%>

    registerChangeIn<%= entityClassPlural %>() {
        this.eventSubscriber = this.eventManager.subscribe('<%= entityInstance %>ListModification', response => <%= eventCallBack %>);
    }

    <%_ if (databaseType !== 'cassandra' && pagination !== 'no') { _%>
    sort() {
        const result = [this.predicate + ',' + (this.reverse ? 'asc' : 'desc')];
        if (this.predicate !== 'id') {
            result.push('id');
        }
        return result;
    }

    <%_ } _%>
    <%_ if (pagination !== 'no') { _%>
        <%_ if (pagination === 'pagination' || pagination === 'pager') { _%>
    private paginate<%= entityClassPlural %>(data: I<%= entityAngularName %>[], headers: HttpHeaders) {
        <%_ if (databaseType !== 'cassandra') { _%>
        this.totalItems = parseInt(headers.get('X-Total-Count'), 10);
        this.queryCount = this.totalItems;
        <%_ } _%>
        this.<%= entityInstancePlural %> = data;
        // added for Clarity
        this.loading = false;
    }
    <%_ } else if (pagination === 'infinite-scroll') { _%>
    private paginate<%= entityClassPlural %>(data: I<%= entityAngularName %>[], headers: HttpHeaders) {
        this.links = this.parseLinks.parse(headers.get('link'));
        this.totalItems = parseInt(headers.get('X-Total-Count'), 10);
        for (let i = 0; i < data.length; i++) {
            this.<%= entityInstancePlural %>.push(data[i]);
        }
    }

    <%_ }} _%>
    delete(item: I<%= entityAngularName %>) {
        this.<%= entityInstance %>DeleteDialogService.open(() => {
            this.loadAll();
        }, [item]);
    }

    // added for Clarity
    batchDelete() {
        if (this.selected.length > 0) {
            this.<%= entityInstance %>DeleteDialogService.open(() => {
                this.loadAll();
            }, this.selected);
        }
    }

    private onError(errorMessage: string) {
        this.jhiAlertService.error(errorMessage, null, null);
    }
}
