import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ListViewEventData } from 'nativescript-ui-listview';
import { ModalDialogService, registerElement } from '@nativescript/angular';
import * as dialogs from "@nativescript/core/ui/dialogs";
import { GenEventOutput } from '~/pages/event/entities/gen-event-search.types';
import { GenEventSearch } from '~/pages/event/state-mgmt/gen/gen-event-search-list.state';
import { ActionBarAction } from '~/pages/event/components/common/disappearing-scroll-action-bar/disappearing-scroll-action-bar.component';
import { SortSearchTag } from '~/pages/event/types/common.types';
import { ContentView } from '@nativescript/core/ui/page';
import { ScrollTopBarListComponent } from './../../common/scroll-top-bar-list/scroll-top-bar-list.component';
import icons from '~/constants/icons';

registerElement("GenEventListView", () => { return ContentView });
interface ListType extends GenEventOutput {
    extended: boolean;
}
@Component({
    selector: 'GenEventListView',
    moduleId: module.id,
    templateUrl: './gen-event-list-view.component.html',
    styleUrls: ['./_gen-event-list-view.component.scss'],
    providers: [ModalDialogService],

})
export class GenEventListViewComponent extends ScrollTopBarListComponent<ListType> {
    @Input()
    searchParams: GenEventSearch;
    @Input()
    isSearchSortDirty: boolean
    @Input()
    tags: SortSearchTag[];
    @Input()
    actionList: ActionBarAction[];

    @Output()
    resetSortSearchAction = new EventEmitter()
    @Output()
    showEvent = new EventEmitter<GenEventOutput>();
    @Output()
    refreshData = new EventEmitter();
    @Output()
    showUnit = new EventEmitter<number>();
    @Output()
    deleteItem = new EventEmitter<number>();
    @Output()
    updateItem = new EventEmitter<number>();
    @Output()
    onAction = new EventEmitter()

    currentDate = new Date();
    icons = icons;

    onDetailTap(id: number) {
        this.showEvent.emit(this.items.find(e => e.ID === id));
    }

    onUnitDetail(unitId: number) {
        this.showUnit.emit(unitId);
    }

    onItemDelete(eventId: number) {
        const options = {
            title: "Smazáni akce",
            message: "Opravdu chcete tuto akci smazat?",
            okButtonText: "Ano",
            cancelButtonText: "Ne"
        };
        dialogs.confirm(options).then((result: boolean) => {
            if (result) {
                this.deleteItem.emit(eventId);
            };
        });
    }

    onItemUpdate(eventId: number) {

        this.updateItem.emit(eventId);
    }

    onPullToRefreshInitiated(args: ListViewEventData) {
        this.refreshData.emit(true);
        setTimeout(() => {
            const listView = args.object;
            listView.notifyPullToRefreshFinished();
        }, 100);
    }


    onResetSortSearchAction() {
        this.resetSortSearchAction.emit()
    }
}
