import { Component, EventEmitter, Input, Output, ViewContainerRef } from '@angular/core';
import { ListViewEventData } from 'nativescript-ui-listview';
import { ModalDialogOptions, ModalDialogService, registerElement } from '@nativescript/angular';
import { EduEventCourseEnrollReminderPageComponent } from '~/pages/event/containers/edu/edu-event-notification-page/edu-event-course-enroll-reminder-page.component';
import { EduEventOutput } from '~/pages/event/entities/edu-event-search.types';
import { ActionBarAction } from '../../common/disappearing-scroll-action-bar/disappearing-scroll-action-bar.component';
import { SortSearchTag } from '~/pages/event/types/common.types';
import * as utils from "@nativescript/core/utils/utils";
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("EduEventListView", () => { return ContentView });


interface ListType extends EduEventOutput {
    extended: boolean;
}
@Component({
    selector: 'EduEventListView',
    moduleId: module.id,
    templateUrl: './edu-event-list-view.component.html',
    styleUrls: ['./_edu-event-list-view.component.scss'],
    providers: [ModalDialogService],

})
export class EduEventListViewComponent extends ScrollTopBarListComponent<ListType> {

    @Input()
    actionList: ActionBarAction[];
    @Input()
    isSearchSortDirty: boolean
    @Input()
    tags: SortSearchTag[];
    @Input()
    title: string

    @Output()
    onAction = new EventEmitter()
    @Output()
    resetSortSearchAction = new EventEmitter()
    @Output()
    showEvent = new EventEmitter<EduEventOutput>();
    @Output()
    enrollEvent = new EventEmitter<EduEventOutput>();
    @Output()
    refreshData = new EventEmitter();

    currentDate = new Date();
    icons = icons;

    constructor(
        private _vcRef: ViewContainerRef,
        private _modalService: ModalDialogService,
    ) {
        super();
    }

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

    onEnroll(item: EduEventOutput) {
        this.enrollEvent.emit(item);
    }
    onEnrollNotification(item: EduEventOutput) {
        const options: ModalDialogOptions = {
            viewContainerRef: this._vcRef,
            context: { event: item },
            fullscreen: false,
        };

        this._modalService.showModal(EduEventCourseEnrollReminderPageComponent, options)

    }

    isDateGreaterThanDate(a: string, b: Date) {
        const d = new Date(a)
        return d > b;
    }

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

    onUrl(url: string) {
        if (url) {
            utils.openUrl(url.split(',')[0]);
        }
    }

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

    isEnrollable(item: ListType) {
        return (item.LoginSkautis || item.LoginLocation)
            && (!item.LoginTo || this.isDateGreaterThanDate(item.LoginTo, this.currentDate))
            && (!item.LoginFrom || !this.isDateGreaterThanDate(item.LoginFrom, this.currentDate))
    }

}
