import { Component, Input, ViewContainerRef } from '@angular/core';
import { ModalDialogOptions, ModalDialogService, RouterExtensions } from '@nativescript/angular';
import { PhoneContactService } from '~/core/services/phone-contact.service';
import { openUrl } from "@nativescript/core/utils/utils";
import { EduEventDetailOutput, EduEventCourseOutput, EduEventCoursesTermsOutput, EduEventLocationDetailOutput } from '../../../entities/edu-event-detail.types';
import { EduEventLogo } from '../../../state-mgmt/edu/edu-event-detail.state';
import { PhonePipe } from '~/shared/pipes/phone.pipe';
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 { SKAUTIS_EDU_EVENT_ENROLL_URL } from '~/pages/event/util/event';
import { transitionOptions } from '~/constants/shared.constants';
import { bellSlashIcon, enrollIcon, bellIcon } from '~/constants/icons';

@Component({
    selector: 'EduEventBaseDetail',
    moduleId: module.id,
    templateUrl: './edu-event-base-detail.component.html',
    styleUrls: ['./_edu-event-base-detail.component.scss']
})
export class EduEventBaseDetailComponent {

    @Input()
    isLoading: boolean;
    @Input()
    eventDetail: EduEventDetailOutput
    @Input()
    courses: EduEventCourseOutput[];
    @Input()
    coursesTerms: EduEventCoursesTermsOutput[];
    @Input()
    locationDetails: EduEventLocationDetailOutput[];
    @Input()
    logo: EduEventLogo;
    @Input()
    eventListData: EduEventOutput;
    currentDate = new Date();

    constructor(
        private routerExtensions: RouterExtensions,
        private phoneContactSrv: PhoneContactService,
        private phonePipe: PhonePipe,
        private _vcRef: ViewContainerRef,
        private _modalService: ModalDialogService,
    ) {
    }

    get urls() {
        return this.eventDetail.Web.split(',').map(u => u.trim())
    }

    onBackButton(): void {
        this.routerExtensions.back();
    }

    onCreateCall(phoneNumber: string) {
        this.phoneContactSrv.createCall(this.phonePipe.transform(phoneNumber));
    }

    async onEmailTap(email: string) {
        await this.phoneContactSrv.createEmail(email);
    }
    openUnit() {
        setTimeout(() => {
            this.routerExtensions.navigate(['unit', this.eventDetail.ID_Unit],
                { transition: transitionOptions });
        }, 10);
    }

    onUrl(url: string) {
        openUrl(url)
    }
    get enrollIcon() {
        return enrollIcon;
    }
    get notificationIcon() {
        return bellIcon;
    }
    get enabledNotificationIcon() {
        return bellSlashIcon;
    }

    getLocation(id: number) {
        return this.locationDetails.find(l => l.ID === id)
    }

    onEnrollNotification() {
        const options: ModalDialogOptions = {
            viewContainerRef: this._vcRef,
            context: { event: this.eventDetail },
            fullscreen: false,
        };

        this._modalService.showModal(EduEventCourseEnrollReminderPageComponent, options)
    }

    onEnroll() {
        if (this.eventListData) {
            const skuatIsUrl = `${SKAUTIS_EDU_EVENT_ENROLL_URL}?ID_EventEducationCourse=${this.eventListData.ID_EventEducationCourse}&ID_EventEducation=${this.eventListData.ID}`;
            let url = this.eventListData.LoginSkautis ? skuatIsUrl : this.eventListData.LoginLocation;
            if (!url.startsWith('http://') && !url.startsWith('https://')) {
                url = 'http://' + url
            }
            openUrl(url)
        }
    }
    isDateGreaterThanDate(a: string | Date, b: Date) {
        const d = new Date(a)
        return d > b;
    }
    canEnroll() {
        return (this.eventDetail.LoginSkautis || this.eventDetail.LoginLocation) &&
            (!this.eventDetail.LoginTo || this.isDateGreaterThanDate(this.eventDetail.LoginTo, this.currentDate))
            && (!this.eventDetail.LoginFrom || !this.isDateGreaterThanDate(this.eventDetail.LoginFrom, this.currentDate))
    }

    canSetNotification() {
        return this.isDateGreaterThanDate(this.eventListData.LoginFrom, this.currentDate) &&
            !this.eventListData.ID_EventEducationCourseEnrollReminder
    }

}
