import { sortItems } from '.';
import { CampEventOutput } from '../entities/camp-event-search.types';
import { CongEventOutput } from '../entities/cong-event-search.types';
import { EduEventSortFilter } from '../types/EduEventSearch';
import { EduEventOutput, EduEventType } from '../entities/edu-event-search.types';
import { CampEventSortKey, CongEventSortKey, EduEventSortKey, GenEventSortKey, SortParams } from '../types/EventSorting';
import { GenEventOutput } from '../entities/gen-event-search.types';
import { EduEventMyActionsOutput } from '~/pages/event/entities/edu-event-search.types';
import { SERVER_URL } from '~/constants/api.constants';


export const SKAUTIS_EDU_EVENT_ENROLL_URL = `${SERVER_URL}Junak/Events/Education/EventEducationEnroll.aspx`

export function sortFilterEduEvents(evs: EduEventOutput[],
    sortParams: EduEventSortFilter, types: EduEventType[]): EduEventOutput[] {
    const { onlyQualification, sortByKey } = sortParams;

    if (onlyQualification) {
        evs = evs.filter(e => {
            const type = types.find(t => t.ID === e.ID_EventEducationType);
            return type && type.IsQualifyingExam;
        })
    }

    const isDate = [EduEventSortKey.REGISTRATION, EduEventSortKey.START]
        .map(a => a.toString()).indexOf(sortByKey)

    return sortItems(evs, sortParams, isDate >= 0)
}
export function sortFilterMyEduEvents(evs: EduEventMyActionsOutput[],
    sortParams: SortParams, searchParams: { unit: string }): EduEventMyActionsOutput[] {
    const { sortByKey } = sortParams;

    if (searchParams.unit) {
        evs = evs.filter(e => e.UnitFullName.toLowerCase().includes(searchParams.unit.toLowerCase()))
    }

    const isDate = [EduEventSortKey.REGISTRATION, EduEventSortKey.START]
        .map(a => a.toString()).indexOf(sortByKey)

    return sortItems(evs, sortParams, isDate >= 0)
}


export function sortGenEvents(evs: GenEventOutput[], sortParams: SortParams) {
    const { sortByKey: key } = sortParams;
    return sortItems(evs, sortParams, [GenEventSortKey.START, GenEventSortKey.END]
        .map(a => a.toString()).indexOf(key) >= 0)

}

export function sortCampEvents(evs: CampEventOutput[], sortParams: SortParams) {
    const { sortByKey: key } = sortParams;
    return sortItems(evs, sortParams, [CampEventSortKey.START, CampEventSortKey.END].map(a => a.toString()).indexOf(key) >= 0)

}
export function sortCongEvents(evs: CongEventOutput[], sortParams: SortParams) {
    const { sortByKey: key } = sortParams;

    return sortItems(evs, sortParams, [CongEventSortKey.START, CongEventSortKey.END].map(a => a.toString()).indexOf(key) >= 0)
}

export enum EventTimeState {
    PAST, FUTURE, CURRENT,
}

export function eventTimeState(start: string, end: string) {
    const startDate = new Date(start);
    const endDate = new Date(end);
    const now = new Date();
    if (now < startDate) {
        return EventTimeState.FUTURE
    } else if (now > startDate && (now < endDate || !endDate)) {
        return EventTimeState.CURRENT
    } else if (now > endDate) {
        return EventTimeState.PAST
    }
    return undefined;
}