import Combine
import SwiftUI
import Foundation
import AppKit

enum AutoCollapseDelay: Int, CaseIterable, Identifiable {
    case three = 3
    case five = 5
    case ten = 10

    var id: Int { rawValue }
    var label: String { "\(rawValue) 秒" }
}

enum FloatingAppearance: String, CaseIterable, Identifiable {
    case system = "跟随主题"
    case light = "亮色"
    case dark = "暗色"

    var id: String { rawValue }

    var colorScheme: ColorScheme? {
        switch self {
        case .system:
            return nil
        case .light:
            return .light
        case .dark:
            return .dark
        }
    }

    var nsAppearance: NSAppearance? {
        switch self {
        case .system:
            return nil
        case .light:
            return NSAppearance(named: .aqua)
        case .dark:
            return NSAppearance(named: .darkAqua)
        }
    }
}

@MainActor
final class AppSettings: ObservableObject {
    static let shared = AppSettings()

    @Published var showFloatingWindow: Bool {
        didSet { UserDefaults.standard.set(showFloatingWindow, forKey: Self.showFloatingWindowKey) }
    }

    @Published var showStatusBarIndicator: Bool {
        didSet { UserDefaults.standard.set(showStatusBarIndicator, forKey: Self.showStatusBarIndicatorKey) }
    }

    @Published var autoCollapseSeconds: Int {
        didSet { UserDefaults.standard.set(autoCollapseSeconds, forKey: Self.autoCollapseSecondsKey) }
    }

    @Published var floatingAppearance: FloatingAppearance {
        didSet { UserDefaults.standard.set(floatingAppearance.rawValue, forKey: Self.floatingAppearanceKey) }
    }

    private static let showFloatingWindowKey = "showFloatingWindow"
    private static let showStatusBarIndicatorKey = "showStatusBarIndicator"
    private static let autoCollapseSecondsKey = "autoCollapseSeconds"
    private static let floatingAppearanceKey = "floatingAppearance"

    private init() {
        let defaults = UserDefaults.standard
        if defaults.object(forKey: Self.showFloatingWindowKey) == nil {
            defaults.set(true, forKey: Self.showFloatingWindowKey)
        }
        if defaults.object(forKey: Self.showStatusBarIndicatorKey) == nil {
            defaults.set(true, forKey: Self.showStatusBarIndicatorKey)
        }
        if defaults.object(forKey: Self.autoCollapseSecondsKey) == nil {
            defaults.set(AutoCollapseDelay.five.rawValue, forKey: Self.autoCollapseSecondsKey)
        }
        if defaults.object(forKey: Self.floatingAppearanceKey) == nil {
            defaults.set(FloatingAppearance.system.rawValue, forKey: Self.floatingAppearanceKey)
        }
        showFloatingWindow = defaults.bool(forKey: Self.showFloatingWindowKey)
        showStatusBarIndicator = defaults.bool(forKey: Self.showStatusBarIndicatorKey)
        let collapseSeconds = defaults.integer(forKey: Self.autoCollapseSecondsKey)
        autoCollapseSeconds = AutoCollapseDelay.allCases.map(\.rawValue).contains(collapseSeconds)
            ? collapseSeconds
            : AutoCollapseDelay.five.rawValue
        let appearanceRawValue = defaults.string(forKey: Self.floatingAppearanceKey) ?? FloatingAppearance.system.rawValue
        floatingAppearance = FloatingAppearance(rawValue: appearanceRawValue) ?? .system
    }
}
