//
//  Colors.swift
//  Tusker
//
//  Created by Shadowfacts on 1/31/23.
//  Copyright © 2023 Shadowfacts. All rights reserved.
//

import UIKit
import SwiftUI

extension UIColor {
    static let appBackground = UIColor { traitCollection in
        if case .dark = traitCollection.userInterfaceStyle,
           !traitCollection.pureBlackDarkMode {
            return UIColor(hue: 230/360, saturation: 23/100, brightness: 10/100, alpha: 1)
        } else {
            return .systemBackground
        }
    }
    
    static let appSecondaryBackground = UIColor { traitCollection in
        if case .dark = traitCollection.userInterfaceStyle,
           !traitCollection.pureBlackDarkMode {
            if traitCollection.userInterfaceLevel == .elevated {
                return UIColor(hue: 230/360, saturation: 23/100, brightness: 10/100, alpha: 1)
            } else {
                return UIColor(hue: 230/360, saturation: 23/100, brightness: 5/100, alpha: 1)
            }
        } else {
            return .secondarySystemBackground
        }
    }
    
    static let appGroupedBackground = UIColor { traitCollection in
        if case .dark = traitCollection.userInterfaceStyle,
           !traitCollection.pureBlackDarkMode {
            return .appSecondaryBackground
        } else {
            return .systemGroupedBackground
        }
    }
    
    static let appSelectedCellBackground = UIColor { traitCollection in
        if case .dark = traitCollection.userInterfaceStyle,
           !traitCollection.pureBlackDarkMode {
            return UIColor(hue: 230/360, saturation: 20/100, brightness: 27/100, alpha: 1)
        } else {
            return .systemFill
        }
    }
    
    static let appGroupedCellBackground = UIColor { traitCollection in
        if case .dark = traitCollection.userInterfaceStyle {
            if traitCollection.pureBlackDarkMode {
                return .secondarySystemBackground
            } else {
                return .appFill
            }
        } else {
            return .systemBackground
        }
    }
    
    static let appFill = UIColor { traitCollection in
        if case .dark = traitCollection.userInterfaceStyle,
           !traitCollection.pureBlackDarkMode {
            return UIColor(hue: 230/360, saturation: 20/100, brightness: 17/100, alpha: 1)
        } else {
            return .systemFill
        }
    }
}

extension Color {
    static let appBackground = Color(uiColor: .appBackground)
    static let appGroupedBackground = Color(uiColor: .appGroupedBackground)
    static let appSecondaryBackground = Color(uiColor: .appSecondaryBackground)
    static let appSelectedCellBackground = Color(uiColor: .appGroupedCellBackground)
    static let appGroupedCellBackground = Color(uiColor: .appGroupedCellBackground)
    static let appFill = Color(uiColor: .appFill)
}

@available(iOS, obsoleted: 17.0)
private let traitsKey: String = ["Traits", "Defined", "client", "_"].reversed().joined()
@available(iOS, obsoleted: 17.0)
private let key = "tusker_usePureBlackDarkMode"

@available(iOS 17.0, *)
private struct PureBlackDarkModeTrait: UITraitDefinition {
    static let defaultValue = true
    static let affectsColorAppearance = true
}

extension UITraitCollection {
    var pureBlackDarkMode: Bool {
        if #available(iOS 17.0, *) {
            return self[PureBlackDarkModeTrait.self]
        } else {
            return obsoletePureBlackDarkMode
        }
    }
    
    @available(iOS, obsoleted: 17.0)
    var obsoletePureBlackDarkMode: Bool {
        get {
            // default to true to match OS behavior
            (value(forKey: traitsKey) as? [String: Any])?[key] as? Bool ?? true
        }
        set {
            var dict = value(forKey: traitsKey) as? [String: Any] ?? [:]
            dict[key] = newValue
            setValue(dict, forKey: traitsKey)
        }
    }
    
    convenience init(pureBlackDarkMode: Bool) {
        if #available(iOS 17.0, *) {
            self.init(PureBlackDarkModeTrait.self, value: pureBlackDarkMode)
        } else {
            self.init()
            self.obsoletePureBlackDarkMode = pureBlackDarkMode
        }
    }
}

@available(iOS 17.0, *)
extension UIMutableTraits {
    var pureBlackDarkMode: Bool {
        get { self[PureBlackDarkModeTrait.self] }
        set { self[PureBlackDarkModeTrait.self] = newValue }
    }
}
