//
//  MPPositive_JsonListAlbum.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/4/28.
//

import Foundation
///列表/专辑结构体
struct JsonListOrAlbum: Codable {
    ///抓取的列表数据
    let contents:Contents?
    ///抓取的头部展示（列表/专辑才有）内容
    let header:Header?
    
    enum CodingKeys: String, CodingKey {
        case contents = "contents"
        case header = "header"

    }
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        contents = try values.decodeIfPresent(Contents.self, forKey: .contents)
        header = try values.decodeIfPresent(Header.self, forKey: .header)
    }
    //MARK: - 内容结构
    ///头部结构
    struct Header: Codable {
        let musicDetailHeaderRenderer:MusicDetailHeaderRenderer?
        let musicImmersiveHeaderRenderer:MusicImmersiveHeaderRenderer?
        enum CodingKeys: String, CodingKey {
            case musicDetailHeaderRenderer = "musicDetailHeaderRenderer"
            case musicImmersiveHeaderRenderer = "musicImmersiveHeaderRenderer"
        }
        
        init(from decoder: Decoder) throws {
            let values = try decoder.container(keyedBy: CodingKeys.self)
            musicDetailHeaderRenderer = try values.decodeIfPresent(MusicDetailHeaderRenderer.self, forKey: .musicDetailHeaderRenderer)
            musicImmersiveHeaderRenderer = try values.decodeIfPresent(MusicImmersiveHeaderRenderer.self, forKey: .musicImmersiveHeaderRenderer)
        }
        struct MusicDetailHeaderRenderer:Codable {
            ///主标题
            let title:Title?
            ///副标题
            let subtitle:Subtitle?
            ///三级标题
            let secondSubtitle:SecondSubtitle?
            ///说明/介绍
            let description:Description?
            ///配图
            let thumbnail:Thumbnail?
            enum CodingKeys: String, CodingKey {
                case title = "title"
                case subtitle = "subtitle"
                case secondSubtitle = "secondSubtitle"
                case description = "description"
                case thumbnail = "thumbnail"
            }
            
            init(from decoder: Decoder) throws {
                let values = try decoder.container(keyedBy: CodingKeys.self)
                title = try values.decodeIfPresent(Title.self, forKey: .title)
                subtitle = try values.decodeIfPresent(Subtitle.self, forKey: .subtitle)
                secondSubtitle = try values.decodeIfPresent(SecondSubtitle.self, forKey: .secondSubtitle)
                description = try values.decodeIfPresent(Description.self, forKey: .description)
                thumbnail = try values.decodeIfPresent(Thumbnail.self, forKey: .thumbnail)
            }
            struct Title: Codable {
                let runs:[Run]?
                enum CodingKeys: String, CodingKey {
                    case runs = "runs"
                }
                
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    runs = try values.decodeIfPresent([Run].self, forKey: .runs)
                }
            }
            struct Subtitle: Codable {
                let runs:[Run]?
                enum CodingKeys: String, CodingKey {
                    case runs = "runs"
                }
                
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    runs = try values.decodeIfPresent([Run].self, forKey: .runs)
                }
            }
            struct SecondSubtitle: Codable {
                let runs:[Run]?
                enum CodingKeys: String, CodingKey {
                    case runs = "runs"
                }
                
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    runs = try values.decodeIfPresent([Run].self, forKey: .runs)
                }
            }
            struct Description: Codable {
                let runs:[Run]?
                enum CodingKeys: String, CodingKey {
                    case runs = "runs"
                }
                
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    runs = try values.decodeIfPresent([Run].self, forKey: .runs)
                }
            }
            struct Thumbnail: Codable {
                let croppedSquareThumbnailRenderer:CroppedSquareThumbnailRenderer?
                enum CodingKeys: String, CodingKey {
                    case croppedSquareThumbnailRenderer = "croppedSquareThumbnailRenderer"
                }
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    croppedSquareThumbnailRenderer = try values.decodeIfPresent(CroppedSquareThumbnailRenderer.self, forKey: .croppedSquareThumbnailRenderer)
                }
                struct CroppedSquareThumbnailRenderer: Codable {
                    let thumbnail:Thumbnail?
                    enum CodingKeys: String, CodingKey {
                        case thumbnail = "thumbnail"
                    }
                    init(from decoder: Decoder) throws {
                        let values = try decoder.container(keyedBy: CodingKeys.self)
                        thumbnail = try values.decodeIfPresent(Thumbnail.self, forKey: .thumbnail)
                    }
                    struct Thumbnail: Codable {
                        ///封面图片组（默认取最后一位，图像内容最大最高清）
                        let thumbnails:[Thumbnails]?
                        enum CodingKeys: String, CodingKey {
                            case thumbnails = "thumbnails"
                        }
                        init(from decoder: Decoder) throws {
                            let values = try decoder.container(keyedBy: CodingKeys.self)
                            self.thumbnails = try values.decodeIfPresent([Thumbnails].self, forKey: .thumbnails)
                        }
                        //MARK: - 封面图片
                        ///封面图片
                        struct Thumbnails: Codable {
                            ///封面图片路径
                            let url:String?
                            let width:CGFloat?
                            let height:CGFloat?
                            enum CodingKeys: String, CodingKey {
                                case url = "url"
                                case width = "width"
                                case height = "height"
                            }
                            init(from decoder: Decoder) throws {
                                let values = try decoder.container(keyedBy: CodingKeys.self)
                                url = try values.decodeIfPresent(String.self, forKey: .url)
                                width = try values.decodeIfPresent(CGFloat.self, forKey: .width)
                                height = try values.decodeIfPresent(CGFloat.self, forKey: .height)
                            }
                        }
                    }
                }
            }
            //MARK: - 文本内容，标题/作者/专辑
            struct Run: Codable {
                ///关键文本内容（代表各级标题）
                let text:String?
                ///指向预览ID
                let navigationEndpoint: NavigationEndpoint?
                enum CodingKeys: String, CodingKey {
                    case text = "text"
                    case navigationEndpoint = "navigationEndpoint"
                }
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    text = try values.decodeIfPresent(String.self, forKey: .text)
                    navigationEndpoint = try values.decodeIfPresent(NavigationEndpoint.self, forKey: .navigationEndpoint)
                }
                struct NavigationEndpoint: Codable {
                    let browseEndpoint:BrowseEndpoint?
                    
                    enum CodingKeys: String, CodingKey {
                        case browseEndpoint = "browseEndpoint"
                    }
                    init(from decoder: Decoder) throws {
                        let values = try decoder.container(keyedBy: CodingKeys.self)
                        browseEndpoint = try values.decodeIfPresent(BrowseEndpoint.self, forKey: .browseEndpoint)
                        
                    }
                    //MARK: - 展示内容块的预览内容
                    struct BrowseEndpoint: Codable {
                        ///预览ID（作者/歌单/专辑都有，关键内容）
                        let browseId:String?
                        ///预览参数（作者/歌单/专辑具备，关键内容）
                        let params:String?
                        ///指向预览类型
                        let browseEndpointContextSupportedConfigs:BrowseEndpointContextSupportedConfigs?
                        enum CodingKeys: String, CodingKey {
                            case browseId = "browseId"
                            case params = "params"
                            case browseEndpointContextSupportedConfigs = "browseEndpointContextSupportedConfigs"
                        }
                        init(from decoder: Decoder) throws {
                            let values = try decoder.container(keyedBy: CodingKeys.self)
                            browseId = try values.decodeIfPresent(String.self, forKey: .browseId)
                            params = try values.decodeIfPresent(String.self, forKey: .params)
                            browseEndpointContextSupportedConfigs = try values.decodeIfPresent(BrowseEndpointContextSupportedConfigs.self, forKey: .browseEndpointContextSupportedConfigs)
                        }
                        struct BrowseEndpointContextSupportedConfigs: Codable {
                            let browseEndpointContextMusicConfig:BrowseEndpointContextMusicConfig?
                            enum CodingKeys: String, CodingKey {
                                case browseEndpointContextMusicConfig = "browseEndpointContextMusicConfig"
                            }
                            init(from decoder: Decoder) throws {
                                let values = try decoder.container(keyedBy: CodingKeys.self)
                                browseEndpointContextMusicConfig = try values.decodeIfPresent(BrowseEndpointContextMusicConfig.self, forKey: .browseEndpointContextMusicConfig)
                            }
                            //MARK: - Browse类型 作者/歌单/专辑
                            struct BrowseEndpointContextMusicConfig: Codable {
                                let pageType:String?
                                enum CodingKeys: String, CodingKey {
                                    case pageType = "pageType"
                                }
                                init(from decoder: Decoder) throws {
                                    let values = try decoder.container(keyedBy: CodingKeys.self)
                                    pageType = try values.decodeIfPresent(String.self, forKey: .pageType)
                                }
                            }
                        }
                    }
                }
            }
        }
        struct MusicImmersiveHeaderRenderer: Codable {
            ///主标题
            let title:Title?
            ///说明/介绍
            let description:Description?
            ///配图
            let thumbnail:Thumbnail?
            enum CodingKeys: String, CodingKey {
                case title = "title"
                case description = "description"
                case thumbnail = "thumbnail"
            }
            
            init(from decoder: Decoder) throws {
                let values = try decoder.container(keyedBy: CodingKeys.self)
                title = try values.decodeIfPresent(Title.self, forKey: .title)
                description = try values.decodeIfPresent(Description.self, forKey: .description)
                thumbnail = try values.decodeIfPresent(Thumbnail.self, forKey: .thumbnail)
            }
            struct Title: Codable {
                let runs:[Run]?
                enum CodingKeys: String, CodingKey {
                    case runs = "runs"
                }
                
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    runs = try values.decodeIfPresent([Run].self, forKey: .runs)
                }
            }
            struct Subtitle: Codable {
                let runs:[Run]?
                enum CodingKeys: String, CodingKey {
                    case runs = "runs"
                }
                
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    runs = try values.decodeIfPresent([Run].self, forKey: .runs)
                }
            }
            struct SecondSubtitle: Codable {
                let runs:[Run]?
                enum CodingKeys: String, CodingKey {
                    case runs = "runs"
                }
                
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    runs = try values.decodeIfPresent([Run].self, forKey: .runs)
                }
            }
            struct Description: Codable {
                let runs:[Run]?
                enum CodingKeys: String, CodingKey {
                    case runs = "runs"
                }
                
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    runs = try values.decodeIfPresent([Run].self, forKey: .runs)
                }
            }
            struct Thumbnail: Codable {
                let musicThumbnailRenderer:MusicThumbnailRenderer?
                enum CodingKeys: String, CodingKey {
                    case musicThumbnailRenderer = "musicThumbnailRenderer"
                }
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    musicThumbnailRenderer = try values.decodeIfPresent(MusicThumbnailRenderer.self, forKey: .musicThumbnailRenderer)
                }
                struct MusicThumbnailRenderer: Codable {
                    let thumbnail:Thumbnail?
                    enum CodingKeys: String, CodingKey {
                        case thumbnail = "thumbnail"
                    }
                    init(from decoder: Decoder) throws {
                        let values = try decoder.container(keyedBy: CodingKeys.self)
                        thumbnail = try values.decodeIfPresent(Thumbnail.self, forKey: .thumbnail)
                    }
                    struct Thumbnail: Codable {
                        ///封面图片组（默认取最后一位，图像内容最大最高清）
                        let thumbnails:[Thumbnails]?
                        enum CodingKeys: String, CodingKey {
                            case thumbnails = "thumbnails"
                        }
                        init(from decoder: Decoder) throws {
                            let values = try decoder.container(keyedBy: CodingKeys.self)
                            self.thumbnails = try values.decodeIfPresent([Thumbnails].self, forKey: .thumbnails)
                        }
                        //MARK: - 封面图片
                        ///封面图片
                        struct Thumbnails: Codable {
                            ///封面图片路径
                            let url:String?
                            let width:CGFloat?
                            let height:CGFloat?
                            enum CodingKeys: String, CodingKey {
                                case url = "url"
                                case width = "width"
                                case height = "height"
                            }
                            init(from decoder: Decoder) throws {
                                let values = try decoder.container(keyedBy: CodingKeys.self)
                                url = try values.decodeIfPresent(String.self, forKey: .url)
                                width = try values.decodeIfPresent(CGFloat.self, forKey: .width)
                                height = try values.decodeIfPresent(CGFloat.self, forKey: .height)
                            }
                        }
                    }
                }
            }
            //MARK: - 文本内容，标题/作者/专辑
            struct Run: Codable {
                ///关键文本内容（代表各级标题）
                let text:String?
                ///指向预览ID
                let navigationEndpoint: NavigationEndpoint?
                enum CodingKeys: String, CodingKey {
                    case text = "text"
                    case navigationEndpoint = "navigationEndpoint"
                }
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    text = try values.decodeIfPresent(String.self, forKey: .text)
                    navigationEndpoint = try values.decodeIfPresent(NavigationEndpoint.self, forKey: .navigationEndpoint)
                }
                struct NavigationEndpoint: Codable {
                    let browseEndpoint:BrowseEndpoint?
                    
                    enum CodingKeys: String, CodingKey {
                        case browseEndpoint = "browseEndpoint"
                    }
                    init(from decoder: Decoder) throws {
                        let values = try decoder.container(keyedBy: CodingKeys.self)
                        browseEndpoint = try values.decodeIfPresent(BrowseEndpoint.self, forKey: .browseEndpoint)
                        
                    }
                    //MARK: - 展示内容块的预览内容
                    struct BrowseEndpoint: Codable {
                        ///预览ID（作者/歌单/专辑都有，关键内容）
                        let browseId:String?
                        ///预览参数（作者/歌单/专辑具备，关键内容）
                        let params:String?
                        ///指向预览类型
                        let browseEndpointContextSupportedConfigs:BrowseEndpointContextSupportedConfigs?
                        enum CodingKeys: String, CodingKey {
                            case browseId = "browseId"
                            case params = "params"
                            case browseEndpointContextSupportedConfigs = "browseEndpointContextSupportedConfigs"
                        }
                        init(from decoder: Decoder) throws {
                            let values = try decoder.container(keyedBy: CodingKeys.self)
                            browseId = try values.decodeIfPresent(String.self, forKey: .browseId)
                            params = try values.decodeIfPresent(String.self, forKey: .params)
                            browseEndpointContextSupportedConfigs = try values.decodeIfPresent(BrowseEndpointContextSupportedConfigs.self, forKey: .browseEndpointContextSupportedConfigs)
                        }
                        struct BrowseEndpointContextSupportedConfigs: Codable {
                            let browseEndpointContextMusicConfig:BrowseEndpointContextMusicConfig?
                            enum CodingKeys: String, CodingKey {
                                case browseEndpointContextMusicConfig = "browseEndpointContextMusicConfig"
                            }
                            init(from decoder: Decoder) throws {
                                let values = try decoder.container(keyedBy: CodingKeys.self)
                                browseEndpointContextMusicConfig = try values.decodeIfPresent(BrowseEndpointContextMusicConfig.self, forKey: .browseEndpointContextMusicConfig)
                            }
                            //MARK: - Browse类型 作者/歌单/专辑
                            struct BrowseEndpointContextMusicConfig: Codable {
                                let pageType:String?
                                enum CodingKeys: String, CodingKey {
                                    case pageType = "pageType"
                                }
                                init(from decoder: Decoder) throws {
                                    let values = try decoder.container(keyedBy: CodingKeys.self)
                                    pageType = try values.decodeIfPresent(String.self, forKey: .pageType)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    ///列表内容结构
    struct Contents: Codable {
        let singleColumnBrowseResultsRenderer : SingleColumnBrowseResultsRenderer?
        enum CodingKeys: String, CodingKey {

            case singleColumnBrowseResultsRenderer = "singleColumnBrowseResultsRenderer"
        }
        init(from decoder: Decoder) throws {
            let values = try decoder.container(keyedBy: CodingKeys.self)
            singleColumnBrowseResultsRenderer = try values.decodeIfPresent(SingleColumnBrowseResultsRenderer.self, forKey: .singleColumnBrowseResultsRenderer)
        }
        struct SingleColumnBrowseResultsRenderer: Codable {
            ///通常只有1位数据
            let tabs : [Tab]?
            enum CodingKeys: String, CodingKey {
                case tabs = "tabs"
            }
            init(from decoder: Decoder) throws {
                let values = try decoder.container(keyedBy: CodingKeys.self)
                tabs = try values.decodeIfPresent([Tab].self, forKey: .tabs)
            }
            struct Tab : Codable {
                let tabRenderer : TabRenderer?
                enum CodingKeys: String, CodingKey {
                    case tabRenderer = "tabRenderer"
                }
                init(from decoder: Decoder) throws {
                    let values = try decoder.container(keyedBy: CodingKeys.self)
                    tabRenderer = try values.decodeIfPresent(TabRenderer.self, forKey: .tabRenderer)
                }
                struct TabRenderer : Codable {
                    ///内容总汇（包含content以及第一阶段ctoke吗）
                    let content : Content?
                    let trackingParams : String?
                    enum CodingKeys: String, CodingKey {
                        case content = "content"
                        case trackingParams = "trackingParams"
                    }
                    init(from decoder: Decoder) throws {
                        let values = try decoder.container(keyedBy: CodingKeys.self)
                        content = try values.decodeIfPresent(Content.self, forKey: .content)
                        trackingParams = try values.decodeIfPresent(String.self, forKey: .trackingParams)
                    }
                    struct Content : Codable {
                        let sectionListRenderer : SectionListRenderer?
                        enum CodingKeys: String, CodingKey {
                            case sectionListRenderer = "sectionListRenderer"
                        }
                        init(from decoder: Decoder) throws {
                            let values = try decoder.container(keyedBy: CodingKeys.self)
                            sectionListRenderer = try values.decodeIfPresent(SectionListRenderer.self, forKey: .sectionListRenderer)
                        }
                        //MARK: - 核心内容，从此往上代码无意义
                        struct SectionListRenderer : Codable {
                            ///指向图像数据内容
                            let contents : [Content]?

                            enum CodingKeys: String, CodingKey {
                                case contents = "contents"
                            }

                            init(from decoder: Decoder) throws {
                                let values = try decoder.container(keyedBy: CodingKeys.self)
                                contents = try values.decodeIfPresent([Content].self, forKey: .contents)
                            }
                            ///图像数据内容
                            struct Content: Codable {
                                ///歌单调用
                                let musicPlaylistShelfRenderer:MusicPlaylistShelfRenderer?
                                ///专辑调用
                                let musicShelfRenderer:MusicShelfRenderer?
                                enum CodingKeys: String, CodingKey {
                                    case musicPlaylistShelfRenderer = "musicPlaylistShelfRenderer"
                                    case musicShelfRenderer = "musicShelfRenderer"
                                }

                                init(from decoder: Decoder) throws {
                                    let values = try decoder.container(keyedBy: CodingKeys.self)
                                    musicPlaylistShelfRenderer = try values.decodeIfPresent(MusicPlaylistShelfRenderer.self, forKey: .musicPlaylistShelfRenderer)
                                    musicShelfRenderer = try values.decodeIfPresent(MusicShelfRenderer.self, forKey: .musicShelfRenderer)
                                }
                                struct MusicPlaylistShelfRenderer: Codable {
                                    ///列表ID
                                    let playlistId:String?
                                    ///指向音乐内容组
                                    let contents:[Content]?
                                    enum CodingKeys: String, CodingKey {
                                        case playlistId = "playlistId"
                                        case contents = "contents"
                                    }
                                    init(from decoder: Decoder) throws {
                                        let values = try decoder.container(keyedBy: CodingKeys.self)
                                        playlistId = try values.decodeIfPresent(String.self, forKey: .playlistId)
                                        contents = try values.decodeIfPresent([Content].self, forKey: .contents)
                                    }
                                    ///音乐内容
                                    struct Content: Codable {
                                        ///常规列表(通常是四组件构成，封面，一级标题，二级标题，三级标题，额外包含ID内容，通常是单曲)
                                        let musicResponsiveListItemRenderer:RootMusicResponsiveListItemRenderer?
                                        ///两行式列表(通常是三组件构成，封面，一级标题，二级标题，额外包含ID内容，通常是歌单)
                                        let musicTwoRowItemRenderer:RootMusicTwoRowItemRenderer?
                                        enum CodingKeys: String, CodingKey {
                                            case musicResponsiveListItemRenderer = "musicResponsiveListItemRenderer"
                                            case musicTwoRowItemRenderer = "musicTwoRowItemRenderer"
                                        }
                                        init(from decoder: Decoder) throws {
                                            let values = try decoder.container(keyedBy: CodingKeys.self)
                                            musicResponsiveListItemRenderer = try values.decodeIfPresent(RootMusicResponsiveListItemRenderer.self, forKey: .musicResponsiveListItemRenderer)
                                            musicTwoRowItemRenderer = try values.decodeIfPresent(RootMusicTwoRowItemRenderer.self, forKey: .musicTwoRowItemRenderer)
                                        }
                                    }
                                }
                                struct MusicShelfRenderer: Codable {
                                    ///指向音乐内容组
                                    let contents:[Content]?
                                    enum CodingKeys: String, CodingKey {
                                        case contents = "contents"
                                    }
                                    init(from decoder: Decoder) throws {
                                        let values = try decoder.container(keyedBy: CodingKeys.self)
                                        contents = try values.decodeIfPresent([Content].self, forKey: .contents)
                                    }
                                    ///音乐内容
                                    struct Content: Codable {
                                        ///常规列表(通常是四组件构成，封面，一级标题，二级标题，三级标题，额外包含ID内容，通常是单曲)
                                        let musicResponsiveListItemRenderer:RootMusicResponsiveListItemRenderer?
                                        ///两行式列表(通常是三组件构成，封面，一级标题，二级标题，额外包含ID内容，通常是歌单)
                                        let musicTwoRowItemRenderer:RootMusicTwoRowItemRenderer?
                                        enum CodingKeys: String, CodingKey {
                                            case musicResponsiveListItemRenderer = "musicResponsiveListItemRenderer"
                                            case musicTwoRowItemRenderer = "musicTwoRowItemRenderer"
                                        }
                                        init(from decoder: Decoder) throws {
                                            let values = try decoder.container(keyedBy: CodingKeys.self)
                                            musicResponsiveListItemRenderer = try values.decodeIfPresent(RootMusicResponsiveListItemRenderer.self, forKey: .musicResponsiveListItemRenderer)
                                            musicTwoRowItemRenderer = try values.decodeIfPresent(RootMusicTwoRowItemRenderer.self, forKey: .musicTwoRowItemRenderer)
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
