//
//  AppDelegate.swift
//  KittensTravelNotes
//
//  Created by 100Years on 2025/6/30.
//


import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    static var tabbar:CustomTabBarController?
    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()
        initPlatform()
        return true
    }

    func goToLoadVC() {
//        let luanch = TSLaunchVC()
//        luanch.dismissHandler = { [weak self] in
//            guard let self = self else { return }
            JudgmentSkipPage()
//        }
//        window?.rootViewController = luanch
    }
    
    func JudgmentSkipPage() {
//        if AppDelegate.isFirstInstallApp() {
//            let bootPageVC = TSBootVC { [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 goToTab() {
        Self.tabbar = CustomTabBarController()
        window?.rootViewController = Self.tabbar
    }
    
}

extension AppDelegate {
    
    static func isFirstInstallApp() -> Bool {
        return UserDefaults.standard.string(forKey: "isFirstInstallApp") == nil
    }

    func initPlatform() {
//        TSRTLManage.setUpInit() //阿拉伯语适配
//        dePrint("TSRMShared.aiListDB.listModels.count=\(TSRMShared.aiListDB.listModels.count)")//提前预热数据库
//        TSColorConfigShared.themeColor = .themeColor
//        TSColorConfigShared.naviMianTextColor = .white//设置基类颜色
//
//        //设置Kingfisher缓存规则
//        let cache = ImageCache(name: "permanent_image_cache")
//        cache.diskStorage.config.expiration = .never    // 永不过期
//        cache.diskStorage.config.sizeLimit = 0          // 无大小限制
//        KingfisherManager.shared.cache = cache
//        
//        
//        checkAppConfig()
//        
        kHandleDBHistoryOperation()
    }
    
    
//    func checkAppConfig() {
//        TSAppUpdateManager.checkAppConfig()
//    }
    
    
    func kHandleDBHistoryOperation(){
        let listModels = TSRMShared.aiListDB.listModels
        let filteredResults = listModels.filter("status == %@ AND id > 0", "running")//直接过滤List（返回Results类型）

        for model in filteredResults {
            let generatePTPOperation = TSGenerateBasePhotoOperationQueue.shared.creatOperation(uuid: model.uuid)
            generatePTPOperation.isSaveProcessToDB = true
            generatePTPOperation.getActionInfo(oldModel: model.getModel())
        }
    }

}
