//
//  AppDelegateEx.swift
//  PhoneManager
//
//  Created by edy on 2025/5/19.
//

import Foundation
import UIKit
import AdjustSdk

extension AppDelegate{
    
    func setupDynamicShortcuts() {
        let shortcutItem = UIApplicationShortcutItem(
            type: "com.app.phonemanager.iap.distance",
            localizedTitle: "🎁 Unlock Special Offers",
            localizedSubtitle: "A Special Offer Just for You!",
            icon: UIApplicationShortcutIcon(systemImageName: "chevron.right"), //UIApplicationShortcutIcon(templateImageName: "icon_gift_sa"), //UIApplicationShortcutIcon(systemImageName: "star.fill"),
            userInfo: nil
        )
        UIApplication.shared.shortcutItems = [shortcutItem]
    }
    
    
    // 处理快捷方式点击
    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        // 处理快捷方式点击事件
        switch shortcutItem.type {
        case "com.app.phonemanager.iap.distance":
            // 执行相应操作
            IAPManager.share.config()
            IAPManager.share.showYearPage = true
            if IAPManager.share.isHotLaunch{
                // 热启动走这里
                switch IAPManager.share.subscriptionType {
                case .none,.week:
                    let vc:PayDistanceViewController = PayDistanceViewController()
                    let nav:BaseNavViewController = BaseNavViewController(rootViewController: vc)
                    nav.modalPresentationStyle = .fullScreen
                    guard let rt = UIViewController.topMostViewController() else { return }
                    rt.present(nav, animated: true)
                default:
                    let vc : PayCompletedViewController =  PayCompletedViewController()
                    guard let rt = UIViewController.topMostViewController() else { return }
                    vc.modalPresentationStyle = .fullScreen
                    rt.present(vc, animated: true)
                }
            }
            completionHandler(true)
        default:
            completionHandler(false)
        }
    }
    
}


extension AppDelegate:AdjustDelegate{
    
    // 因变化时触发函数
    func adjustAttributionChanged(_ attribution: ADJAttribution?) {
        
        guard let attribution = attribution else { return }

        Print("归因变化数据",attribution.jsonResponse as Any)
        if let dic = attribution.jsonResponse{
            let pram = self.convertToSwiftDictionary(dic as NSDictionary)
            APIReportManager.shared.startReport(type: .source_atrribute,ext: pram)
        }
    }
}
