import { eventSource } from 'src/app/models/agenda/eventSource';
import { setHours, setMinutes } from 'date-fns';

export class eventListBox {
	
	constructor(){}

	EventEndDateTreatment ({startTime, endTime}) {

    const startTimeSamp = new Date(startTime).toLocaleDateString()
    const endTimeSamp = new Date(endTime).toLocaleDateString()

    const endMinutes = new Date(endTime).getMinutes()
    const endHours = new Date(endTime).getHours() 


    if (startTimeSamp < endTimeSamp  && (endMinutes + endHours) == 0) {
      endTime = new Date(endTime);
      endTime.setSeconds(endTime.getSeconds() - 1);
      return new Date(endTime)
    } else {
      return new Date(endTime)
    }

  }
	
	filterProfile(eventSource: eventSource[], profile: 'md' | 'pr' |  'all') {
		return eventSource.filter((e) => e.profile == profile)
	}

	getEventInsideRange(eventSource: eventSource[], rangeStartDate, randEndDate) {
		return eventSource.filter((e)=> {
			if(new Date(rangeStartDate).getTime() <= new Date(e.startTime).getTime() &&
					new Date(randEndDate).getTime() >= new Date(e.endTime).getTime()) { 
				return true
			}
			return false
		})
	}

	daysBetween(){ }

	list(eventSource: eventSource[], profile: 'md' | 'pr' | 'all', rangeStartDate, randEndDate, calendarDate) {

		if(profile != 'all') {
			eventSource = this.filterProfile(eventSource, profile)
		}

		eventSource = this.getEventInsideRange(eventSource, rangeStartDate, randEndDate)

		if(profile == 'md') {
			eventSource = this.encapsulation(eventSource, 'mdgpr');
		} else {
			eventSource = this.encapsulation(eventSource, 'pr');
		}

		return this.display(eventSource)
	}

	display(list) {
		let days = {};

		list.forEach( (event:any, index)=> {

			var startDate: any = new Date(event.start);

			var endDate: any = this.EventEndDateTreatment({
				startTime: startDate, 
				endTime: event.end
			})
			
			const day = (((new Date (event.start)).getDate())).toString().padStart(2,'0')
			
			event.manyDays = false

			event.todayOnly = new Date(event.start).toLocaleDateString() == new Date(event.end).toLocaleDateString()

			if(!days.hasOwnProperty(day)) {
				days[day] = []
			}

			if (new Date(startDate).toLocaleDateString() != new Date(endDate).toLocaleDateString()) {

				const diffTime = Math.abs(endDate - startDate);
				const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 

				if (diffDays <= 150 && !event.event.IsAllDayEvent  ) {

					if (diffDays >= 1) {
						
						const StartEvent =  Object.assign({}, {
							title: event.title,
							start: event.start,
							end: event.end,
							color: event.color,
							id: event.id,
							index: event.index,
							profile: event.profile,
							CalendarName: event.CalendarName,
							event: {
								Subject: event.event.Subject,
								StartDate:  event.event.StartDate,
								EndDate: event.event.EndDate,
								Location:  event.event.Location,
								EventId:  event.event.EventId,
								CalendarName: event.event.CalendarName
							},
							startMany: true,
							endMany: false,
							middle: false
						})
						
						days[day].push(StartEvent)
						
						let i = 1;
						
						while (startDate.getFullYear() !=  endDate.getFullYear() || 
							startDate.getMonth() != endDate.getMonth() ||
							startDate.getDate() != endDate.getDate()) {

							const newDate = startDate.setDate(startDate.getDate()+ i)

							let otherDays =   (((new Date (newDate)).getDate())).toString().padStart(2,'0')
								
							event.other = true
							
							event.start = newDate
							if(!days.hasOwnProperty(otherDays)) {

								days[otherDays] = []
							}
							
							if (!(startDate.getFullYear() !=  endDate.getFullYear() || 
							startDate.getMonth() != endDate.getMonth() ||
							startDate.getDate() != endDate.getDate())) {
								// last push

								const EndEvent =  Object.assign({}, {
									title: event.title,
									start: event.start,
									end: event.end,
									color: event.color,
									id: event.id,
									index: event.index,
									profile: event.profile,
									CalendarName: event.CalendarName,
									event: {
										Subject: event.event.Subject,
										StartDate:  event.event.StartDate,
										EndDate: event.event.EndDate,
										Location:  event.event.Location,
										EventId:  event.event.EventId,
										CalendarName: event.event.CalendarName
									},
									Subject: event.Subject,
									startMany: false,
									endMany: true,
									middle: false
								})

								days[otherDays].push(EndEvent)

							} else {
								const EndEvent =  Object.assign({}, {
									title: event.title,
									start: event.start,
									end: event.end,
									color: event.color,
									id: event.id,
									index: event.index,
									profile: event.profile,
									CalendarName: event.CalendarName,
									event: {
										Subject: event.event.Subject,
										StartDate:  event.event.StartDate,
										EndDate: event.event.EndDate,
										Location:  event.event.Location,
										EventId:  event.event.EventId,
										CalendarName: event.event.CalendarName
									},
									Subject: event.Subject,
									startMany: false,
									endMany: true,
									middle: true
								})
								days[otherDays].push(EndEvent)
							}
							
						}

					} else {
						days[day].push(event)

					}
				} else {
					days[day].push(event)

				}

			} 

			days[day].push(event)

		})

		setTimeout(()=>{
			document.querySelectorAll('.EventListBox-container .EventListBox').forEach((e)=>{
				if(e.childElementCount == 0) {
					e.parentElement.style.display = 'none'
				} else {
					e.parentElement.style.display = 'block'
				}
			})
		}, 10)
		return days
	}

	encapsulation(eventsList, profile) {

		// remove all event
		let events = [];

		eventsList.forEach((element, eventIndex) => {
			
			events.push({
				start: new Date(element.StartDate || element.startTime),
				end: new Date(element.EndDate || element.endTime),
				color: {
					primary: '#0000',
					secondary: '#0000'
				},
				id: element.EventId || element.id,
				index: eventIndex,
				profile: profile,
				CalendarName: element.calendarName,
				event: element.event,
			});

		});

		return events;
	}
	
}