//
//  AppDelegate.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/19.i
//

import AppTrackingTransparency
import GoogleMobileAds
import TSVideoKit
import UIKit

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

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        window?.makeKeyAndVisible()
        goToLoadVC()
        initBaseDatas()
        return true
    }

    func goToTab() {
        window?.rootViewController = TSTabBarController()
    }

    func goToLoadVC() {
        _ = TSImageDataCenter.shared
        window?.rootViewController = TSLaunchVC()
    }

    func initBaseDatas() {
        var config = TSConfiguration.default
        config.configurePath = "http: // p.100yearslater.com/live/config"
        TSVideoOperator.shared.loadWithConfiguration(config: config, needJs: true)
//        if UserDefaults.standard.string(forKey: "InitExampleData") == nil {
//            if let path = Bundle.main.path(forResource: "CandyTown", ofType: ".mp3") {
//                let fileUrl = URL(fileURLWithPath: path)
//                ImportFilesManager.shared.copyFileToUrl(url: fileUrl)
//                UserDefaults.standard.set("1", forKey: "InitExampleData")
//            }
//
//            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
//                if let firstVideo = TSVideoOperator.shared.dataManager.fetchAllVideos().first {
//                    TSVideoOperator.shared.playerViewModel.currentVideo = firstVideo
//                    PlayerManager.shared.miniBar.updateVideoInfo(video: firstVideo, state: .pause)
//                }
//            }
//        }

        if let videoId = UserDefaults.standard.string(forKey: "lastedVideoId"),
           !videoId.isEmpty,
           let lastVideo = TSVideoOperator.shared.dataManager.fetchVideo(videoId: videoId) {
            TSVideoOperator.shared.playerViewModel.currentVideo = lastVideo
            TSVideoOperator.shared.playerViewModel.currentVideos = TSVideoOperator.shared.dataManager.fetchCachedVideos()
            PlayerManager.shared.miniBar.updateVideoInfo(video: lastVideo, state: .pause)
        }

        if let loopMode = UserDefaults.standard.string(forKey: "lastedPlayMode") {
            let mMode = LoopMode(rawValue: loopMode) ?? .cyclic
            TSVideoOperator.shared.playerViewModel.loopMode = mMode
        }
    }

    static func requestAdTrack() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
                // 这里可以处理用户的选择
                switch status {
                case .authorized:
                    print("授权成功，可以开始跟踪")
                case .denied:
                    print("用户拒绝了授权")
                case .restricted:
                    print("该设备的用户无法授权")
                case .notDetermined:
                    print("用户尚未做出选择")
                @unknown default:
                    print("未知状态")
                }
            })
        }
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        AppDelegate.requestAdTrack()
    }
}

extension AppDelegate {
}
