import Combine
import SwiftUI

class ThemeManager: ObservableObject {
    @Published var accentColor: Color = .blue {
        didSet {
            saveColor()
        }
    }
    
    private let userDefaultsKey = "accentColorData"
    
    init() {
        loadColor()
    }
    
    private func loadColor() {
        guard let data = UserDefaults.standard.data(forKey: userDefaultsKey) else {
            self.accentColor = .blue
            return
        }
        
        do {
            if let uiColor = try NSKeyedUnarchiver.unarchivedObject(ofClass: UIColor.self, from: data) {
                self.accentColor = Color(uiColor)
            }
        } catch {
            print("Failed to load accent color: \(error)")
            self.accentColor = .blue
        }
    }
    
    private func saveColor() {
        do {
            let uiColor = UIColor(accentColor)
            let data = try NSKeyedArchiver.archivedData(withRootObject: uiColor, requiringSecureCoding: false)
            UserDefaults.standard.set(data, forKey: userDefaultsKey)
        } catch {
            print("Failed to save accent color: \(error)")
        }
    }
    
    func resetToDefault() {
        accentColor = .blue
    }
    
    // Curated list of preset colors that maintain good contrast
    static let presetColors: [Color] = [
        .blue,      // Default Blue
        .purple,    // Purple
        .pink,      // Pink
        .red,       // Red
        .orange,    // Orange
        .green,     // Green
        .teal,      // Teal
        .indigo,    // Indigo
        .mint,      // Mint
        Color(uiColor: .systemBrown), // Brown
        Color(uiColor: .systemCyan)   // Cyan
    ]
    
    var contrastingTextColor: Color {
        let uiColor = UIColor(accentColor)
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
        
        uiColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
        
        // Calculate relative luminance
        let luminance = 0.299 * red + 0.587 * green + 0.114 * blue
        
        // Return black for light colors, white for dark colors
        return luminance > 0.5 ? .black : .white
    }
}
