//
//  MPPositive_LoadCoreModel.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/5/28.
//

import UIKit
///数据库单例类
class MPPositive_LoadCoreModel: NSObject {
    static let shared = MPPositive_LoadCoreModel()
    //CoreData专用更新线程
    private var reloadQueue:DispatchQueue = .init(label: "relax.offline.mp3.CoreDataReload")
    ///收藏列表ViewModel组
    var listViewModels:[MPPositive_CollectionListViewModel] = []
    ///收藏艺术家ViewModel组
    var artistViewModels:[MPPositive_CollectionArtistViewModel] = []
    ///收藏歌曲ViewModel组
    var songViewModels:[MPPositive_CollectionSongViewModel] = []
    ///下载歌曲ViewModel组
    var loadViewModels:[MPPositive_DownloadViewModel] = []
    ///搜索结果
    var searchTags:[MPPositive_SearchTagModel] = []
    ///自定义歌单组
    var playLists:[MPPositive_CustomPlayListViewModel] = []
    
    private override init() {
        super.init()
//        reloadAll()
    }
    func reloadAll() {
        reloadCollectionListViewModels(nil)
        reloadCollectionArtistViewModels(nil)
        reloadCollectionSongViewModel(nil)
        reloadLoadSongViewModel(nil)
        reloadSearchTags(nil)
        reloadCustomPlayLists(nil)
    }
    
    ///更新收藏列表组
    func reloadCollectionListViewModels(_ complection:(() -> Void)?) {
        reloadQueue.async {
            var viewModels = [MPPositive_CollectionListViewModel]()
            MPPositive_CollectionListModel.fetchAll { array in
                array.forEach { item in
                    viewModels.append(.init(item))
                }
                //倒叙一下
                viewModels = viewModels.reversed()
                DispatchQueue.main.async {
                    self.listViewModels = viewModels
                    if complection != nil {
                        complection!()
                    }
                }
            }
        }
    }
    ///更新收藏艺术家组
    func reloadCollectionArtistViewModels(_ complection:(() -> Void)?) {
        reloadQueue.async {
            var viewModels = [MPPositive_CollectionArtistViewModel]()
            MPPositive_CollectionArtistModel.fetchAll{ array in
                array.forEach { item in
                    viewModels.append(.init(item))
                }
                //倒叙一下
                viewModels = viewModels.reversed()
                DispatchQueue.main.async {
                    self.artistViewModels = viewModels
                    if complection != nil {
                        complection!()
                    }
                }
            }
        }
    }
    ///更新收藏歌曲组
    func reloadCollectionSongViewModel(_ complection:(() -> Void)?) {
        reloadQueue.async {
            var viewModels = [MPPositive_CollectionSongViewModel]()
            MPPositive_CollectionSongModel.fetchAll{ array in
                array.forEach { item in
                    viewModels.append(.init(item))
                }
                //倒叙一下
                viewModels = viewModels.reversed()
                DispatchQueue.main.async {
                    self.songViewModels = viewModels
                    if complection != nil {
                        complection!()
                    }
                }
            }
        }
    }
    ///更新下载歌曲库
    func reloadLoadSongViewModel(_ complection:(() -> Void)?) {
        reloadQueue.async {
            var viewModels = [MPPositive_DownloadViewModel]()
            MPPositive_DownloadItemModel.fetchAll{ array in
                array.forEach { item in
                    viewModels.append(.init(item))
                }
                //倒叙一下
                viewModels = viewModels.reversed()
                DispatchQueue.main.async {
                    self.loadViewModels = viewModels
                    if complection != nil {
                        complection!()
                    }
                }
            }
        }
    }
    ///更新搜索历史
    func reloadSearchTags(_ complection:(() -> Void)?) {
        reloadQueue.async {
            MPPositive_SearchTagModel.fetchAll{ array in
                let s = array.sorted { test1, test2 in
                    if let date1 = test1.date, let date2 = test2.date {
                        return date1 > date2
                    }else {
                        return false
                    }
                }
                DispatchQueue.main.async {
                    self.searchTags = s
                    if complection != nil {
                        complection!()
                    }
                }
            }
        }
    }
    ///更新自定义歌单组
    func reloadCustomPlayLists(_ complection:(() -> Void)?) {
        reloadQueue.async {
            var viewModels = [MPPositive_CustomPlayListViewModel]()
            //获取数据库自定义歌单数据，并排序
            MPPositive_CustomPlayListModel.fetchAll{ array in
                let items = array.sorted(by: {$0.createTime > $1.createTime})
                //生成ViewModel
                for (index, item) in items.enumerated() {
                    let listViewModel = MPPositive_CustomPlayListViewModel(item)
                    listViewModel.index = index
                    viewModels.append(listViewModel)
                }
                DispatchQueue.main.async {
                    self.playLists = viewModels
                    if complection != nil {
                        complection!()
                    }
                }
            }
        }
    }
}
