//
//  UserResponseModel.swift
//  SpeakEasyLearnEnglish
//
//  Created by edy on 2025/7/21.
//

import UIKit

struct UserMode:Codable {
    var id:Int?
    /// 用户名
    var userName:String?
    var appleUserId:String?
    var googleUserId:String?
    
    var email:String?
    var status:Int?
    var avatar:String?
    /// 母语
    var nativeLanguage:String?
    var subscribed:Int?
    /// 等级
    var targetLevel:String?
    var type:Int?
    var token:String?
    /// 是否用母语学习
    var learnedNative:Int?
    
    static let keyPathMap: [String: PartialKeyPath<UserMode>] = [
            "id": \UserMode.id,
            "userName": \UserMode.userName,
            "appleUserId": \UserMode.appleUserId,
            "googleUserId": \UserMode.googleUserId,
            "email": \UserMode.email,
            "status": \UserMode.status,
            "avatar": \UserMode.avatar,
            "nativeLanguage": \UserMode.nativeLanguage,
            "subscribed": \UserMode.subscribed,
            "targetLevel": \UserMode.targetLevel,
            "type": \UserMode.type,
            "token": \UserMode.token,
            "learnedNative": \UserMode.learnedNative
        ]
    
    mutating func assign(from source: UserMode?) {
        for (_, keyPath) in UserMode.keyPathMap {
            switch keyPath {
            case let keyPath as WritableKeyPath<UserMode, Int?>:
                if self[keyPath: keyPath] == nil, let value = source?[keyPath: keyPath] {
                    self[keyPath: keyPath] = value
                }
            case let keyPath as WritableKeyPath<UserMode, String?>:
                if self[keyPath: keyPath] == nil, let value = source?[keyPath: keyPath] {
                    self[keyPath: keyPath] = value
                }
            default:
                break
            }
        }
        
//        let mirror = Mirror(reflecting: source)
//        for child in mirror.children {
//            if let key = child.label {
//                // 使用键路径动态设置值
//                if let value = child.value as? Int, let intKey = UserMode.keyPathMap[key] as? WritableKeyPath<UserMode, Int?> {
//                    self[keyPath: intKey] = value
//                } else if let value = child.value as? String , let intKey = UserMode.keyPathMap[key] as? WritableKeyPath<UserMode, String?>{
//                    self[keyPath: intKey] = value
//                }
//            }
//        }
    }
}

