//
//  AppDelegate.swift
//  MusicVideoPlus
//
//  Created by ak on 2021/5/28.
//

import BFFramework
import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)

        let rootViewController = MVHomeController()
        let navigationController = UINavigationController(rootViewController: rootViewController)

        window?.rootViewController = navigationController

        window?.makeKeyAndVisible()

        let wxappInfo = WXApiInfo()
        wxappInfo.state = "com.piaoquan.shanyin"
        wxappInfo.appid = "wx0e8234aac576d1e0"
        wxappInfo.secret = "a6c35e4e8adf60039f94f4c18e2aabdb"
        wxappInfo.universalLink = "https://shanyin.piaoquantv.com/.well-known/apple-app-site-association"
        wxappInfo.scope = "snsapi_userinfo"
        PQSingletoWXApiUtil.shared.registerApp(appInfo: wxappInfo)
        
        PQSingletoBuglyUtil.shared.registerBugly(appID: "6c481442-aeb4-49ec-a2ae-21359bfef44a")
        
        PQBFConfig.shared.enableBFLog = DEVELOPMENT_ENVIRONMENT == 0
        
        // 注册友盟
        MVThirdApiUtil.registerUM()
        // 注册bugly
        MVThirdApiUtil.registerBugly()
        return true
    }

    func application(_: UIApplication, continue userActivity: NSUserActivity, restorationHandler _: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        return PQSingletoWXApiUtil.shared.handleOpenUniversalLink(userActivity: userActivity)
    }

    func application(_: UIApplication, open url: URL, options _: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        return PQSingletoWXApiUtil.shared.handleOpen(url: url)
    }

    func application(_: UIApplication, handleOpen url: URL) -> Bool {
        return PQSingletoWXApiUtil.shared.handleOpen(url: url)
    }

    func application(_: UIApplication, open url: URL, sourceApplication _: String?, annotation _: Any) -> Bool {
        return PQSingletoWXApiUtil.shared.handleOpen(url: url)
    }

    func applicationDidEnterBackground(_: UIApplication) {
        BFLog(message: "applicationDidEnterBackground")
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 1) {
            // 继续播放其他音乐
            do {
                try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
                BFLog(message: "关闭声音活跃继续播放其他音乐成功")
            } catch {
                BFLog(message: "关闭声音活跃继续播放其他音乐失败：\(error)")
            }
        }
    }

    func applicationDidBecomeActive(_: UIApplication) {
        BFLog(message: "applicationDidBecomeActive")
        DispatchQueue.global().async {
            let session = AVAudioSession.sharedInstance()
            do {
                try session.setCategory(.soloAmbient)
                BFLog(message: "打开声音活跃关闭其他音乐播放成功")
            } catch {
                BFLog(message: "打开声音活跃关闭其他音乐播放失败：\(error)")
            }
            do {
                try session.setActive(true, options: .notifyOthersOnDeactivation)
                BFLog(message: "打开声音活跃关闭其他音乐播放成功")
            } catch {
                BFLog(message: "打开声音活跃关闭其他音乐播放失败：\(error)")
            }
        }
    }
}
