//
//  MPPositive_LibraryListViewModel.swift
//  relax.offline.mp3.music
//
//  Created by Mr.Zhou on 2024/7/24.
//

import UIKit

class MPPositive_LibraryListViewModel: NSObject {
    ///对应模块标题
    var title:String = "Library"
    ///曲库View组
    var libraryViewModels:[MPPositive_LibraryViewModel] = []
    
    override init() {
        super.init()
    }
    ///生成曲库View组
    func reloadLibrarys(_ complehandler:(() -> Void)?) {
        var array:[MPPositive_LibraryViewModel] = []
        //获取数据
        let songs = MPPositive_LoadCoreModel.shared.songViewModels
        if songs.isEmpty == false, let lastUrl = songs.first?.coverURL {
            //获取收藏歌曲
            let item = MPPositive_LibraryViewModel(.init(libraryType: .love_songs, coverUrl: lastUrl, title: "Love Songs"))
            array.append(item)
        }else {
            let item = MPPositive_LibraryViewModel(.init(libraryType: .love_songs, coverUrl: nil, title: "Love Songs"))
            array.append(item)
        }
        let artists = MPPositive_LoadCoreModel.shared.artistViewModels
        if artists.isEmpty == false, let lastUrl = artists.first?.coverURL {
            let item = MPPositive_LibraryViewModel(.init(libraryType: .love_aritists, coverUrl: lastUrl, title: "Love Artists"))
            array.append(item)
        }else {
            let item = MPPositive_LibraryViewModel(.init(libraryType: .love_aritists, coverUrl: nil, title: "Love Artists"))
            array.append(item)
        }
        let offlines = MPPositive_LoadCoreModel.shared.loadViewModels
        if offlines.isEmpty == false, let lastUrl = offlines.first?.reviewURL {
            let item = MPPositive_LibraryViewModel(.init(libraryType: .offline_songs, coverUrl: lastUrl, title: "Offline Songs"))
            array.append(item)
        }else {
            let item = MPPositive_LibraryViewModel(.init(libraryType: .offline_songs, coverUrl: nil, title: "Offline Songs"))
            array.append(item)
        }
        //对自定义列表进行添加
        MPPositive_LoadCoreModel.shared.playLists.forEach { list in
            if list.playList.videosArray.isEmpty == false, let lastUrl = list.coverImageURL, let title = list.title, let identifier = list.playList.playListId {
                let item = MPPositive_LibraryViewModel(.init(libraryType: .custom_playlist, coverUrl: lastUrl, title: title, identifier: identifier))
                array.append(item)
            }
        }
        libraryViewModels = array
        if let block = complehandler {
            block()
        }
    }
}
