//
//  Prankmodel.swift
//  Funny_sounds
//
//  Created by 忆海16 on 2024/8/15.
//

import Foundation


// 音频文件的模型
class AudioFile:NSObject, Codable {
    let title: String
    let mp3Url: String
    let preUrl: String
    // 自定义初始化方法
     init(title: String, mp3Url: String, preUrl: String) {
         self.title = title
         self.mp3Url = mp3Url
         self.preUrl = preUrl
     }
     
     // 必须实现这些方法，以避免与Codable冲突
     required init(from decoder: Decoder) throws {
         let container = try decoder.container(keyedBy: CodingKeys.self)
         self.title = try container.decode(String.self, forKey: .title)
         self.mp3Url = try container.decode(String.self, forKey: .mp3Url)
         self.preUrl = try container.decode(String.self, forKey: .preUrl)
     }
     
     func encode(to encoder: Encoder) throws {
         var container = encoder.container(keyedBy: CodingKeys.self)
         try container.encode(title, forKey: .title)
         try container.encode(mp3Url, forKey: .mp3Url)
         try container.encode(preUrl, forKey: .preUrl)
     }
     
     private enum CodingKeys: String, CodingKey {
         case title
         case mp3Url
         case preUrl
     }
}

// 分类模型
class SoundCategory:NSObject,Codable {
    let categoryId: String
    let categoryName: String
    let categoryUrl: String
    let list: [AudioFile]
}


func parseJSONData(jsonData: Data) -> [SoundCategory]? {
    let decoder = JSONDecoder()
    do {
        // 解析数据
        let soundCategories = try decoder.decode([SoundCategory].self, from: jsonData)
        return soundCategories
    } catch {
        print("解析JSON数据时出错: \(error)")
        return nil
    }
}
