//
//  UserDefaultsExtension.swift
//  SodaLive
//
//  Created by klaus on 2022/05/20.
//

import Foundation

enum UserDefaultsKey: String, CaseIterable {
    case auth
    case role
    case can
    case token
    case email
    case userId
    case nickname
    case pushToken
    case profileImage
    case noChatRoomList
    case devicePushToken
    case isContentPlayLoop
    case isFollowedChannel
    case isViewedOnboardingView
    case notShowingEventPopupId
    case isAdultContentVisible
    case contentPreference
}

extension UserDefaults {
    static func set(_ value: Bool, forKey key: UserDefaultsKey) {
        let key = key.rawValue
        UserDefaults.standard.set(value, forKey: key)
    }
    
    static func bool(forKey key: UserDefaultsKey) -> Bool {
        let key = key.rawValue
        return UserDefaults.standard.bool(forKey: key)
    }
    
    static func set(_ value: String, forKey key: UserDefaultsKey) {
        let key = key.rawValue
        UserDefaults.standard.set(value, forKey: key)
    }
    
    static func string(forKey key: UserDefaultsKey) -> String {
        let key = key.rawValue
        return UserDefaults.standard.string(forKey: key) ?? ""
    }
    
    static func set(_ value: Int, forKey key: UserDefaultsKey) {
        let key = key.rawValue
        UserDefaults.standard.set(value, forKey: key)
    }
    
    static  func int(forKey key: UserDefaultsKey) -> Int {
        let key = key.rawValue
        return UserDefaults.standard.integer(forKey: key)
    }
    
    static func set(_ value: Data, forKey key: UserDefaultsKey) {
        let key = key.rawValue
        UserDefaults.standard.set(value, forKey: key)
    }
    
    static  func data(forKey key: UserDefaultsKey) -> Data? {
        let key = key.rawValue
        return UserDefaults.standard.data(forKey: key)
    }
    
    static func isAdultContentVisible() -> Bool {
        let key = UserDefaultsKey.isAdultContentVisible.rawValue
        return UserDefaults.standard.object(forKey: key) != nil ? bool(forKey: .isAdultContentVisible) : true
    }
    
    static func reset() {
        UserDefaultsKey.allCases.forEach { UserDefaults.standard.removeObject(forKey: $0.rawValue) }
    }
}
