//
//  AppDelegate.swift
//  AIRingtone
//
//  Created by 100Years on 2025/2/27.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

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

    func goToLoadVC() {
        let launchVC = TSLaunchVC()
        launchVC.dismissHandler = { [weak self]  in
            guard let self = self else { return }
            JudgmentSkipPage()
        }
        window?.rootViewController = TSBaseNavigationC(rootViewController: launchVC)
    }

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

    func JudgmentSkipPage() {
        //去掉引导页
        if UserDefaults.standard.string(forKey: "isFirstInstallApp") == nil {
            let bootPageVC  = TSBootPageVC { [weak self]  in
                guard let self = self else { return }
                UserDefaults.standard.set("1", forKey: "isFirstInstallApp")
                UserDefaults.standard.synchronize()
                goToTab()
            }
            let navi = TSBaseNavigationC(rootViewController: bootPageVC)
            window?.rootViewController = navi
        }else{
            goToTab()
        }
    }
    

    func initPlatform() {
        TSColorConfigShared.naviMianTextColor = .white
        purchaseDidChanged()
    }

}

extension AppDelegate {

    func applicationDidEnterBackground(_ application: UIApplication) {
        TSBusinessAudioPlayer.shared.stop()
        beginBackgroundTask()
    }
    
    func beginBackgroundTask() {
        backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask { [weak self] in
            self?.endBackgroundTask()
        }
    }
    
    func endBackgroundTask() {
        if backgroundTaskIdentifier != .invalid {
            UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
            backgroundTaskIdentifier = .invalid
        }
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        
    }
    
}

extension AppDelegate {
    
    
    func purchaseDidChanged(){
//        NotificationCenter.default.addObserver(forName: .kPurchasePrepared, object: nil, queue: OperationQueue.main) {notification in
//            self.showEveryDayPopPurchase()
//        }
    }
    
    func showEveryDayPopPurchase() {
            // 1. 获取当前日期
            let currentDate = Date()
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd"
            let currentDateString = dateFormatter.string(from: currentDate)
            
            // 2. 获取上次弹窗的日期
            let userDefaults = UserDefaults.standard
            let lastGreetingDateString = userDefaults.string(forKey: "kEveryDayPopPurchase")
            
            // 3. 检查是否需要显示弹窗 "2025-03-19"
            if lastGreetingDateString != currentDateString {
                
                // 4. 弹窗付费引导
                if showTSPurchaseVC() {
                    // 5. 更新上次弹窗的日期
                    userDefaults.set(currentDateString, forKey: "kEveryDayPopPurchase")
                }
            }
    }
    
    func showTSPurchaseVC() -> Bool{
        if kPurchaseDefault.products.count > 0 {
            if let vc = window?.rootViewController {
                TSPurchaseVC.show(target: vc, closePageBlock: nil)
                return true
            }
        }
        return false
    }
}
