//
//  MVMineViewModel.swift
//  MusicVideoPlus
//
//  Created by SanW on 2021/6/22.
//


import UIKit

class MVMineViewModel: NSObject {
    /// 分页获取我的视频
    /// - Parameters:
    ///   - pageNum: 第几页
    ///   - pageSize: 一页多少
    ///   - completeHander: <#completeHander description#>
    class func userVideoListData(pageNum: Int = 1, pageSize: Int = 20, completeHander: @escaping (_ videoLists: [PQVideoListModel]?, _ msg: String?) -> Void) {
        BFNetRequestAdaptor.postRequestData(url: PQENVUtil.shared.longvideoapi + userVideoListUrl, parames: ["pageNum": pageNum, "pageSize": pageSize], commonParams: commonParams()) { response, _, error, _ in
            if response is NSNull || response == nil {
                completeHander(nil, error?.msg)
            } else {
                let temResponseArr: [[String: Any]]? = response as? [[String: Any]]
                if temResponseArr != nil {
                    var videoLists = Array<PQVideoListModel>.init()
                    temResponseArr?.forEach { dic in
                        let tempModel = PQVideoListModel(jsonDict: dic)
                        let userInfo = PQUserInfoModel()
                        userInfo.nickName = BFLoginUserInfo.shared.nickName
                        tempModel.userInfo = userInfo
                        videoLists.append(tempModel)
                    }
                    completeHander(videoLists, error?.msg)
                } else {
                    completeHander(nil, error?.msg)
                }
            }
        }
    }

    /// 获取用户信息
    /// - Parameter completeHander: <#completeHander description#>
    class func userInfoData(completeHander: @escaping (_ videoCount: Int?, _ avatarUrl: String?, _ nickName: String?, _ msg: String?) -> Void) {
        BFNetRequestAdaptor.postRequestData(url: PQENVUtil.shared.longvideoapi + userInfoUrl, parames: nil, commonParams: commonParams()) { response, _, error, _ in
            if response is NSNull || response == nil {
                completeHander(nil, nil, nil, error?.msg ?? "加载失败")
            } else {
                let videoCount: Int? = Int("\((response as? [String: Any])?["videoCount"] ?? "0")")
                let avatarUrl: String? = "\((response as? [String: Any])?["avatarUrl"] ?? "")"
                let nickName: String? = "\((response as? [String: Any])?["nickName"] ?? "")"
                completeHander(videoCount, avatarUrl, nickName, nil)
            }
        }
    }
}
