import { Component, Injector, ViewEncapsulation, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ClientPreferencesServiceProxy, ClientPreferenceDto, PreferencesServiceProxy } 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 { CreateOrEditClientPreferenceModalComponent } from './create-or-edit-clientPreference-modal.component';
import { ViewClientPreferenceModalComponent } from './view-clientPreference-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 * as _ from 'lodash';
import * as moment from 'moment';

@Component({
    templateUrl: './clientPreferences.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class ClientPreferencesComponent extends AppComponentBase {

    @ViewChild('createOrEditClientPreferenceModal', { static: true }) createOrEditClientPreferenceModal: CreateOrEditClientPreferenceModalComponent;
    @ViewChild('viewClientPreferenceModalComponent', { static: true }) viewClientPreferenceModal: ViewClientPreferenceModalComponent;
    @ViewChild('dataTable', { static: true }) dataTable: Table;
    @ViewChild('paginator', { static: true }) paginator: Paginator;

    advancedFiltersAreShown = false;
    filterText = '';
    valueFilter = '';
    filteredPreferences:any;
    preferenceId: number;
    constructor(
        injector: Injector,
        private _clientPreferencesServiceProxy: ClientPreferencesServiceProxy,
        private _notifyService: NotifyService,
        private _tokenAuth: TokenAuthServiceProxy,
        private _activatedRoute: ActivatedRoute,
        private _fileDownloadService: FileDownloadService,
        private _preferencesServiceProxy: PreferencesServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(){
        this.filterUserPreferences();
        $('.kt-select2').select2({ 
            placeholder: "Select.."
        });
    }

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

        this.spinnerService.show();
        this.preferenceId =  Number((<HTMLInputElement>document.getElementById('selectedPreferencess')).value) ? Number((<HTMLInputElement>document.getElementById('selectedPreferencess')).value) : undefined;
        this._clientPreferencesServiceProxy.getAll(
            this.filterText,
            this.valueFilter,
            this.preferenceId,
            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.spinnerService.hide();
        });
    }

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

    createClientPreference(): void {
        this.createOrEditClientPreferenceModal.show();
    }

    deleteClientPreference(clientPreference: ClientPreferenceDto): void {
        this.message.confirm(
            '',
            (isConfirmed) => {
                if (isConfirmed) {
                    this._clientPreferencesServiceProxy.delete(clientPreference.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l('SuccessfullyDeleted'));
                        });
                }
            }
        );
    }

    exportToExcel(): void {
        this._clientPreferencesServiceProxy.getClientPreferencesToExcel(
            this.filterText,
            this.valueFilter,
            this.preferenceId,
        )
            .subscribe(result => {
                this._fileDownloadService.downloadTempFile(result);
            });
    }

    filterUserPreferences(): void {
        this._preferencesServiceProxy.getAll(
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined
        ).subscribe(result => {
            this.filteredPreferences = result.items;
        });
    }
}
