//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
//Copyright (C) 2024 HTWKalender support@htwkalender.de

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU Affero General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Affero General Public License for more details.

//You should have received a copy of the GNU Affero General Public License
//along with this program.  If not, see <https://www.gnu.org/licenses/>.

package room

import (
	"github.com/pocketbase/pocketbase"
	"htwkalender/data-manager/model"
	"htwkalender/data-manager/service/db"
	"htwkalender/data-manager/service/functions"
	"time"
)

func GetRooms(app *pocketbase.PocketBase) ([]string, error) {
	rooms, err := db.GetRooms(app)
	if err != nil {
		return nil, err
	} else {
		return rooms, nil
	}
}

func GetRoomScheduleForDay(app *pocketbase.PocketBase, room string, date string) ([]model.AnonymizedEventDTO, error) {
	roomSchedule, err := db.GetRoomScheduleForDay(app, room, date)
	if err != nil {
		return nil, err
	}
	anonymizedRoomSchedule := anonymizeRooms(roomSchedule)
	return anonymizedRoomSchedule, nil
}

func GetRoomSchedule(app *pocketbase.PocketBase, room string, from string, to string, mapped string) ([]model.AnonymizedEventDTO, error) {

	if mapped == "true" {
		room = functions.MapRoom(room, false)
	}

	roomSchedule, err := db.GetRoomScheduleInTimeSpan(app, room, from, to)
	if err != nil {
		return nil, err
	}
	anonymizedRoomSchedule := anonymizeRooms(roomSchedule)

	// If mapped is "true", map the rooms again after anonymization
	if mapped == "true" {
		for i := range anonymizedRoomSchedule {
			anonymizedRoomSchedule[i].Rooms = functions.MapRoom(anonymizedRoomSchedule[i].Rooms, true)
		}
	}

	return anonymizedRoomSchedule, nil
}

// Transform the events to anonymized events throwing away all unnecessary information
func anonymizeRooms(events []model.Event) []model.AnonymizedEventDTO {
	var anonymizedEvents []model.AnonymizedEventDTO
	for _, event := range events {
		anonymizedEvents = append(anonymizedEvents, event.AnonymizeEvent())
	}
	return anonymizedEvents
}

func GetFreeRooms(app *pocketbase.PocketBase, from time.Time, to time.Time) ([]string, error) {
	rooms, err := db.GetRooms(app)
	if err != nil {
		return nil, err
	}
	var events model.Events
	events, err = db.GetEventsThatCollideWithTimeRange(app, from, to)
	if err != nil {
		return nil, err
	}
	freeRooms := removeRoomsThatHaveEvents(rooms, events)
	return freeRooms, nil
}

// Remove all rooms from the list that have events in the given time range
func removeRoomsThatHaveEvents(rooms []string, schedule []model.Event) []string {
	var freeRooms []string
	for _, room := range rooms {
		if !isRoomInSchedule(room, schedule) {
			freeRooms = append(freeRooms, room)
		}
	}
	return freeRooms
}

// Check if a room is in the schedule
func isRoomInSchedule(room string, schedule []model.Event) bool {
	for _, event := range schedule {
		if event.Course != "Sport" {
			rooms := functions.SeperateRoomString(event.Rooms)
			// check if room is in rooms
			for _, r := range rooms {
				if r == room {
					return true
				}
			}
		} else {
			if event.Rooms == room {
				return true
			}
		}
	}
	return false
}
