//
//  TSSetingViewModel.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/16.
//

import StoreKit

class TSSetingViewModel: ObservableObject {
    
    @Published var settingTypes: [SettingType] = SettingType.allCases
    @Published var isViper: Bool = kPurchaseToolShared.isVip
    
    var appid = "6741563341"
    // todo.kailen-privacy
    func showPrivacy(parent: UIViewController) {
        let vc = TSBusinessWebVC(urlType: .privacy)
        vc.hidesBottomBarWhenPushed = true
        parent.navigationController?.pushViewController(vc, animated: true)
    }

    // todo.kailen-Agreement
    func showAgreement(parent: UIViewController) {
        let vc = TSBusinessWebVC(urlType: .terms)
        vc.hidesBottomBarWhenPushed = true
        parent.navigationController?.pushViewController(vc, animated: true)
    }

    // todo.kailen-logo
    func shareApp(parent: UIViewController) {
        let httpAppStoreLink = "https://apps.apple.com/app/id\(appid)"
        let text = "Space"
        let url = URL(string: httpAppStoreLink)!
        let image = UIImage(named: "App-Icon")!.compressImageSize(to: CGSize(width: 100, height: 100))
        let final = ShareActivityItemProvider(placeholderItem: image)
        let vc = UIActivityViewController(activityItems: [url, final, text], applicationActivities: nil)
        vc.completionWithItemsHandler = { activity, _, _, _ in
            if let type = activity, type == .copyToPasteboard {
                UIPasteboard.general.string = httpAppStoreLink
            }
        }

        parent.present(vc, animated: true)
        
    }
    
    func rateAction() {
        let countKey = "ProcessCompletedCountKey"
        // If the app doesn't store the count, this returns 0.
        var count = UserDefaults.standard.integer(forKey: countKey)
        count += 1
        UserDefaults.standard.set(count, forKey: countKey)
        /// 如果当前版本 弹过一次 那么下一次就走url
        /// 如果当前版本 没弹过，那就弹
        if count >= 4 {
            let openStr = "itms-apps://itunes.apple.com/app/\(appid)?action=write-review"
            if let url = URL(string: openStr), UIApplication.shared.canOpenURL(url) {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                } else {
                    UIApplication.shared.canOpenURL(url)
                }
            } else {
                print("链接出错")
            }
        } else {
            SKStoreReviewController.requestReview()
        }
    }
    
    // todo.kailen-logo
    func pushTutorials(parent: UIViewController) {
        let vc = TSThemeTutorialsVC()
        vc.hidesBottomBarWhenPushed = true
        parent.navigationController?.pushViewController(vc, animated: true)
    }
    
    // todo.kailen-logo
    func pushVipPurchase(parent: UIViewController) {
        TSPurchaseVC.show(target: parent) {[weak self]  in
            guard let self = self else { return }
            isViper = kPurchaseToolShared.isVip
        }
//        pushTutorials(parent: parent)
    }
}

extension UIImage {
    // 压缩图片到指定尺寸
    func compressImageSize(to size: CGSize) -> UIImage {
        let targetSize = size
        if self.size.width > targetSize.width || self.size.height > targetSize.height {
            return kf.resize(to: targetSize)
        }
        return self
    }
}
