//
//  Date+Extension.swift
//  Lanu
//
//  Created by OneeChan on 2025/12/14.
//

import Foundation


extension Date {
    // 辅助方法：格式化 "时:分"（补零，如 09:05）
    func formattedTime() -> String {
        let formatter = DateFormatter()
        formatter.locale = curLocal
        formatter.dateFormat = "HH:mm"
        return formatter.string(from: self)
    }
    
    // 辅助方法：格式化 "星期一"
    func formattedWeek() -> String {
        let formatter = DateFormatter()
        formatter.locale = curLocal
        formatter.dateFormat = "EEEE"
        return formatter.string(from: self)
    }
    
    // 辅助方法：格式化 "星期 时:分"（如 星期一 09:30）
    func formattedWeekWithTime() -> String {
        let formatter = DateFormatter()
        formatter.locale = curLocal
        formatter.dateFormat = "EEEE HH:mm"
        return formatter.string(from: self)
    }
    
    // 辅助方法：格式化 "月/日"（如 10/23）
    func formattedDate(_ separator: String = "/") -> String {
        let formatter = DateFormatter()
        formatter.locale = curLocal
        formatter.dateFormat = "MM\(separator)dd"
        return formatter.string(from: self)
    }
    
    // 辅助方法：格式化 "月/日 时:分"（如 10/23 14:30）
    func formattedDateWithTime(_ separator: String = "/") -> String {
        let formatter = DateFormatter()
        formatter.locale = curLocal
        formatter.dateFormat = "MM\(separator)dd HH:mm"
        return formatter.string(from: self)
    }
    
    // 辅助方法：格式化 "日/月/年"（如 23/10/2025）
    func formattedFullDate(_ separator: String = "/", normal: Bool = false) -> String {
        let formatter = DateFormatter()
        formatter.locale = curLocal
        if normal {
            formatter.dateFormat = "yyy\(separator)MM\(separator)dd"
        } else {
            formatter.dateFormat = "dd\(separator)MM\(separator)yyyy"
        }
        return formatter.string(from: self)
    }
    
    // 辅助方法：格式化 "年/月/日 时:分"（如 2025/10/23 14:30）
    func formattedFullDateWithTime(_ separator: String = "/") -> String {
        let formatter = DateFormatter()
        formatter.locale = curLocal
        formatter.dateFormat = "dd\(separator)MM\(separator)yyyy HH:mm"
        return formatter.string(from: self)
    }
    
    var tencentIMTimeDesc: String {
        if self == Date.distantPast {
            return ""
        }
        
        let dateFmt = DateFormatter()
        dateFmt.locale = curLocal
        
        var calendar = Calendar.current
        calendar.firstWeekday = 7
        
        let nowComponent: DateComponents = calendar.dateComponents([.day, .month, .year, .weekOfMonth], from: Date())
        let dateComponent: DateComponents = calendar.dateComponents([.day, .month, .year, .weekOfMonth], from: self)
        
        if nowComponent.year == dateComponent.year {
            if nowComponent.month == dateComponent.month {
                if nowComponent.weekOfMonth == dateComponent.weekOfMonth {
                    if nowComponent.day == dateComponent.day {
                        dateFmt.dateFormat = "HH:mm"
                    } else {
                        dateFmt.dateFormat = "EEEE"
                    }
                } else {
                    dateFmt.dateFormat = "MM/dd"
                }
            } else {
                dateFmt.dateFormat = "MM/dd"
            }
        } else {
            dateFmt.dateFormat = "dd/MM/yyyy"
        }
        
        return dateFmt.string(from: self)
    }
    
    func yearAgo(_ year: Int) -> Date {
        let calendar = Calendar.current
        let yearsAgoComponents = DateComponents(year: year, day: -1)
        guard let yearsAgo = calendar.date(byAdding: yearsAgoComponents, to: Date()) else {
            return Date()
        }
        
        return yearsAgo
    }
}

extension Date {
    var inMinutes: Bool {
        timeIntervalSince1970.inMinutes
    }
    
    var inHour: Bool {
        timeIntervalSince1970.inHour
    }
    
    var inDay: Bool {
        timeIntervalSince1970.inDay
    }
    
    var isSameDay: Bool {
        Calendar.current.isDate(Date(), inSameDayAs: self)
    }
}
