//
//  TimePeriod.swift
//  WOKA
//
//  Created by MacBook Pro on 23/05/24.
//

import UIKit

// Enumeration representing different periods of the day, each with a start time, end time, and two colors.
enum TimePeriod: Equatable {
    // Enumeration cases for different periods of the day with associated values.
    case morning(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor, dayCycle: DayCycle)
    case afternoon(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor, dayCycle: DayCycle)
    case evening(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor, dayCycle: DayCycle)
    case night(startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor, dayCycle: DayCycle)
    
    // Computed property to get the details of each time period as a tuple.
    var details: (startTime: String, endTime: String, color1: UIColor, color2: UIColor, grass: String, textColor: UIColor, dayCycle: DayCycle) {
        switch self {
        case .morning(let startTime, let endTime, let color1, let color2, let grass, let textColor, let dayCycle):
            return (startTime, endTime, color1, color2, grass, textColor, dayCycle)
        case .afternoon(let startTime, let endTime, let color1, let color2, let grass, let textColor, let dayCycle):
            return (startTime, endTime, color1, color2, grass, textColor, dayCycle)
        case .evening(let startTime, let endTime, let color1, let color2, let grass, let textColor, let dayCycle):
            return (startTime, endTime, color1, color2, grass, textColor, dayCycle)
        case .night(let startTime, let endTime, let color1, let color2, let grass, let textColor, let dayCycle):
            return (startTime, endTime, color1, color2, grass, textColor, dayCycle)
        }
    }
    
    // Function to check if a given current time falls within the time period.
    func contains(currentTime: Date) -> Bool {
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm"
        guard let start = formatter.date(from: details.startTime),
              let end = formatter.date(from: details.endTime) else { return false }
        
        return isTime(currentTime, between: start, and: end)
    }
    
    // Equatable protocol conformance to compare two TimePeriod instances.
    static func ==(lhs: TimePeriod, rhs: TimePeriod) -> Bool {
        switch (lhs, rhs) {
        case let (.morning(startTime1, endTime1, color11, color12, grass1, textColor1, dayCycle1), .morning(startTime2, endTime2, color21, color22, grass2, textColor2, dayCycle2)),
            let (.afternoon(startTime1, endTime1, color11, color12, grass1, textColor1, dayCycle1), .afternoon(startTime2, endTime2, color21, color22, grass2, textColor2, dayCycle2)),
            let (.evening(startTime1, endTime1, color11, color12, grass1, textColor1, dayCycle1), .evening(startTime2, endTime2, color21, color22, grass2, textColor2, dayCycle2)),
            let (.night(startTime1, endTime1, color11, color12, grass1, textColor1, dayCycle1), .night(startTime2, endTime2, color21, color22, grass2, textColor2, dayCycle2)):
            return startTime1 == startTime2 && endTime1 == endTime2 && color11 == color21 && color12 == color22 && grass1 == grass2 && textColor1 == textColor2 && dayCycle1 == dayCycle2
        default:
            return false
        }
    }
    
    // Helper function to check if a given time falls between a start and end time.
    func isTime(_ time: Date, between startTime: Date, and endTime: Date) -> Bool {
        let calendar = Calendar.current
        let currentComponents = calendar.dateComponents([.hour, .minute], from: time)
        let startComponents = calendar.dateComponents([.hour, .minute], from: startTime)
        let endComponents = calendar.dateComponents([.hour, .minute], from: endTime)
        
        guard let currentHour = currentComponents.hour, let currentMinute = currentComponents.minute,
              let startHour = startComponents.hour, let startMinute = startComponents.minute,
              let endHour = endComponents.hour, let endMinute = endComponents.minute else {
            return false
        }
        
        let currentTotalMinutes = currentHour * 60 + currentMinute
        let startTotalMinutes = startHour * 60 + startMinute
        let endTotalMinutes = endHour * 60 + endMinute
        
        if startTotalMinutes <= endTotalMinutes {
            // Regular case where the time period does not cross midnight.
            return currentTotalMinutes >= startTotalMinutes && currentTotalMinutes <= endTotalMinutes
        } else {
            // Special case where the time period crosses midnight.
            return currentTotalMinutes >= startTotalMinutes || currentTotalMinutes <= endTotalMinutes
        }
    }
}


enum DayCycle{
    case morning
    case afternoon
    case evening
    case night
}
