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

import UIKit
///更多页面（两种款式，单曲/视频一种，列表/专辑一种）
class MPPositive_MoreContentViewController: MPPositive_BaseViewController {
    //约束类型设置
    enum MoreShowType: Int {
        case Single = 0
        case List = 1
        case Artist = 2
        case Video = 3
        //布局Layout
        var layout:UICollectionViewFlowLayout{
            let layout = UICollectionViewFlowLayout()
            layout.sectionInset = .init(top: 0, left: 18*width, bottom: 50*width, right: 16*width)
            switch self {
            case .Single:
                layout.itemSize = .init(width: 168*width, height: 134*width)
                layout.minimumInteritemSpacing = 2*width
                layout.minimumLineSpacing = 32*width
            case .List:
                layout.itemSize = .init(width: 162*width, height: 210*width)
                layout.minimumLineSpacing = 32*width
                layout.minimumInteritemSpacing = 15*width
            case .Artist:
                layout.itemSize = .init(width: 94*width, height: 116*width)
                layout.minimumInteritemSpacing = 2*width
                layout.minimumLineSpacing = 32*width
            case .Video:
                layout.itemSize = .init(width: 339*width, height: 237*width)
                layout.minimumLineSpacing = 32*width
            }
            return layout
        }
    }
    ///列表展示类型
    private var showType:MoreShowType = .Single{
        didSet{
            collectionView.collectionViewLayout = showType.layout
            collectionView.reloadData()
        }
    }
    private lazy var collectionView:UICollectionView = {
        let collectionView:UICollectionView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), collectionViewLayout: showType.layout)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.backgroundColor = .clear
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(MPPositive_HomeListFirstCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListFirstCollectionViewCellID)
        collectionView.register(MPPositive_MoreListContentCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_MoreListContentCollectionViewCellID)
        collectionView.register(MPPositive_HomeListFourthCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListFourthCollectionViewCellID)
        collectionView.register(MPPositive_HomeListFifthCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListFifthCollectionViewCellID)
        collectionView.contentInset = .init(top: 0, left: 0, bottom: 70*width, right: 0)
        return collectionView
    }()
    //列表cell
    private let MPPositive_HomeListFirstCollectionViewCellID = "MPPositive_HomeListFirstCollectionViewCell"
    private let MPPositive_MoreListContentCollectionViewCellID = "MPPositive_MoreListContentCollectionViewCell"
    private let MPPositive_HomeListFourthCollectionViewCellID = "MPPositive_HomeListFourthCollectionViewCell"
    //单曲cell
    private let MPPositive_HomeListFifthCollectionViewCellID = "MPPositive_HomeListFifthCollectionViewCell"
    //传值
    var browseModuleList:MPPositive_BrowseModuleListViewModel!
    init(_ list:MPPositive_BrowseModuleListViewModel) {
        super.init(nibName: nil, bundle: nil)
        browseModuleList = list
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setTitle("More")
        setPopBtn()
        confirgue()
    }
    private func confirgue() {
        view.backgroundColor = .init(hex: "#121212")
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(32*width)
            make.left.right.bottom.equalToSuperview()
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        reload()
    }
    //传递数据检测
    private func reload() {
        //根据传递的数据类型进行检测，判断是什么类型的
        let type:MoreShowType = .init(rawValue: browseModuleList.items.first?.browseItem.itemType?.rawValue ?? 0)!
        if browseModuleList.items.first?.browseItem.pageType == "MUSIC_VIDEO_TYPE_OMV" {
            showType = .Video
        }else {
            showType = type
        }
    }
}
//MARK: - collectionView
extension MPPositive_MoreContentViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return browseModuleList.items.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        switch showType {
        case .Single:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListFirstCollectionViewCellID, for: indexPath) as! MPPositive_HomeListFirstCollectionViewCell
            cell.itemViewModel = browseModuleList.items[indexPath.row]
            return cell
        case .List:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_MoreListContentCollectionViewCellID, for: indexPath) as! MPPositive_MoreListContentCollectionViewCell
            cell.itemViewModel = browseModuleList.items[indexPath.row]
            return cell
        case .Artist:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListFourthCollectionViewCellID, for: indexPath) as! MPPositive_HomeListFourthCollectionViewCell
            cell.itemViewModel = browseModuleList.items[indexPath.row]
            return cell
        case .Video:
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListFifthCollectionViewCellID, for: indexPath) as! MPPositive_HomeListFifthCollectionViewCell
            cell.itemViewModel = browseModuleList.items[indexPath.row]
            return cell
        }
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        MP_AnalyticsManager.shared.home_b_module_clickAction(browseModuleList.title ?? "")
        switch showType {
        case .Single, .Video:
            //单曲/视频跳转
            MPPositive_Debouncer.shared.call {
                [weak self] in
                guard let self = self else {return}
                guard MP_NetWorkManager.shared.netWorkStatu == .reachable else {
                    MP_HUD.text("Weak connection.", delay: 2.0, completion: nil)
                    return
                }
                MP_AnalyticsManager.shared.song_clickAction("MoreContent")
                //优先清除数据
                MP_PlayerManager.shared.loadPlayer = nil
                //弹出播放器
                NotificationCenter.notificationKey.post(notificationName: .pup_player_vc)
                MP_AnalyticsManager.shared.player_b_impAction()
                let item = browseModuleList.items[indexPath.row]
                //触发next请求，优先获取列表全部单曲基础数据（不完善）
                MP_NetWorkManager.shared.requestNextList(item.browseItem.playListId ?? "", videoId: item.browseItem.videoId ?? "", clickTrackingParams: item.browseItem.clickTrackingParams){ [weak self] listSongs in
                    guard let self = self else {return}
                    //回掉的数据并不完善，生成一个playerloadViewModel
                    let lodaViewModel = MPPositive_PlayerLoadViewModel(listSongs, currentVideoId: browseModuleList.items[indexPath.row].browseItem.videoId ?? "")
                    lodaViewModel.improveData(browseModuleList.items[indexPath.row].browseItem.videoId ?? "")
                    //更改播放器播放类型
                    MP_PlayerManager.shared.setPlayType(.normal)
                    MP_PlayerManager.shared.loadPlayer = lodaViewModel
                    MP_AnalyticsManager.shared.player_b_listAction()
                }
            }
        case .Artist:
            let item = browseModuleList.items[indexPath.row]
            //前往艺术家页面
            let artistVC = MPPositive_ArtistShowViewController(item.browseItem.artistId ?? "", clickTrackingParams: item.browseItem.clickTrackingParams ?? "")
            navigationController?.pushViewController(artistVC, animated: true)
        default:
            let item = browseModuleList.items[indexPath.row]
            //列表专辑
            let listVC = MPPositive_ListShowViewController(item.browseItem.browseId ?? "", params: "", title: item.title ?? "", subtitle: item.subtitle ?? "", clickTrackingParams: item.browseItem.clickTrackingParams ?? "")
            navigationController?.pushViewController(listVC, animated: true)
        }
    }
}
