import { Component, EventEmitter, Input, Output } from '@angular/core';
import { ListViewEventData } from 'nativescript-ui-listview';
import { PhoneContactService } from '~/core/services/phone-contact.service';
import { CampEventOutput } from '~/pages/event/entities/camp-event-search.types';
import { CampEventSearch } from '~/pages/event/state-mgmt/camp/camp-event-search-list.state';
import { ActionBarAction } from '../../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 canEditEvent, { AuthEventType } from '~/pages/event/util/authorization';
import { ScrollTopBarListComponent } from '../../common/scroll-top-bar-list/scroll-top-bar-list.component';
import { registerElement, ModalDialogService } from '@nativescript/angular';
import icons from '~/constants/icons';


interface ListType extends CampEventOutput {
    extended: boolean;
}
registerElement('CampEventListView', () => { return ContentView });
@Component({
    selector: 'CampEventListView',
    moduleId: module.id,
    templateUrl: './camp-event-list.component.html',
    styleUrls: ['./_camp-event-list.component.scss'],
    providers: [ModalDialogService],

})
export class CampEventListComponent extends ScrollTopBarListComponent<ListType> {

    @Input()
    isSearchSortDirty: boolean
    @Input()
    tags: SortSearchTag[];

    @Input()
    actionList: ActionBarAction[];
    @Input()
    searchParams: CampEventSearch;

    @Output()
    onAction = new EventEmitter()

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

    currentDate = new Date();

    icons = icons;

    constructor(
        private phoneContactSrv: PhoneContactService,
    ) {
        super()
    }

    onCallTap(phone: string) {
        this.phoneContactSrv.createCall(phone);
    }

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

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

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

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

    isEditable(item: CampEventOutput) {
        return canEditEvent(item.ID_Unit, AuthEventType.CAMP) &&
            ['approvedLeader', 'approvedParent'].indexOf(item.ID_EventCampState) === -1
    }
}
