//
//  AppPaymentStore.swift
//  suixinkan
//

import Foundation

/// 管理当前账号的收款功能偏好。
final class AppPaymentStore {

    private enum Key: String, CaseIterable {
        case isOpenReceiveVoice = "key_is_open_receive_voice"
    }

    private let defaults: UserDefaults
    private let session: AppSessionStore

    /// 使用指定持久化容器和会话创建收款偏好存储。
    init(defaults: UserDefaults, session: AppSessionStore) {
        self.defaults = defaults
        self.session = session
    }

    /// 收款到账是否开启语音播报。
    var isOpenReceiveVoice: Bool {
        get {
            guard let key = scopedKey(.isOpenReceiveVoice) else { return false }
            return defaults.bool(forKey: key)
        }
        set {
            guard let key = scopedKey(.isOpenReceiveVoice) else { return }
            defaults.set(newValue, forKey: key)
        }
    }

    /// 清除当前账号的收款偏好。
    func clear(accountScope: String) {
        guard !accountScope.isEmpty else { return }
        Key.allCases.forEach { defaults.removeObject(forKey: "\(accountScope)_\($0.rawValue)") }
    }

    private func scopedKey(_ key: Key) -> String? {
        session.accountScopedKey(key.rawValue)
    }
}
