//
//  MP_WebVisitorDataManager.swift
//  relax.offline.mp3.music
//
//  Created by Mr.Zhou on 2024/7/26.
//

import UIKit
///对用户数据管理器
class MP_WebVisitorDataManager: NSObject {
    static var shared = MP_WebVisitorDataManager()
    ///加载web(调用之后记得销毁)
    private var webView:WKWebView?
    ///油管视频播放页
    private lazy var homePath = "https://music.youtube.com/watch?"
    override init() {
        let webConfiguration = WKWebViewConfiguration()
        //实例化WebView
        webView = .init(frame: .zero, configuration: webConfiguration)
        super.init()
        //注册代理
        webView?.navigationDelegate = self
        webView?.uiDelegate = self
    }
    deinit {
        webView = nil
    }
    
    /// 通过web加载指定的youtube视频网址
    /// - Parameters:
    ///   - videoId: 媒体资源id
    ///   - list: 媒体资源列表
    func getYoutubeWatch(_ videoId:String, list:String?) {
        var path = homePath+"?"+"v=\(videoId)"
        if let l = list, l.isEmpty == false {
            path = path+"&list="+l
        }
        guard let watchUrl = URL(string: path) else {return}
        let request = URLRequest(url: watchUrl)
        //web加载网址
        webView?.load(request)
    }
}
extension MP_WebVisitorDataManager: WKNavigationDelegate, WKUIDelegate {

}
