
import StoreKit
class TSSetingVM {
    
    var target:UIViewController
    init(target: UIViewController) {
        self.target = target
    }
    
    
    lazy var dataArray: [TSSetingModel] = {
        var dataArray = [TSSetingModel]()
        
        
        dataArray.append(TSSetingModel(leftImage: .settingShareUs,
                                       leftTitle:  "Share us".localized,
                                       rectCorner: [.topLeft,.topRight],
                                       tapBlock: { [weak self] _, _, _ in
            guard let self = self else { return }
            
            let httpAppStoreLink = "https://apps.apple.com/app/id\(appid)"
            let text = ""
            let url = URL(string: httpAppStoreLink)!
//            let image = UIImage(resource: .appIcon)
            let vc = UIActivityViewController(activityItems: [text, url], applicationActivities: nil)
//            let vc = UIActivityViewController(activityItems: [image, text, url], applicationActivities: nil)
            vc.completionWithItemsHandler = { activity, _, _, _ in
                if let type = activity, type == .copyToPasteboard {
                    UIPasteboard.general.string = httpAppStoreLink
                }
            }
            
            if UIDevice.current.userInterfaceIdiom == .pad {
                if let popover = vc.popoverPresentationController {
                    popover.sourceView = self.target.view // 设置锚点视图
                    popover.sourceRect = CGRect(x: self.target.view.bounds.midX, y: self.target.view.bounds.midY, width: 0, height: 0) // 设置弹窗位置为屏幕中心
                    popover.permittedArrowDirections = [] // 禁止箭头指向
                }
            }
            
            self.target.present(vc, animated: true)
        }))
        
        
        dataArray.append(TSSetingModel(leftImage: .settingRateUs,
                                       leftTitle:  "Rate us".localized,
                                       tapBlock: { [weak self] _, _, _ in
            guard let self = self else { return }
            Self.kRateAction()
        }))
        
        
        dataArray.append(TSSetingModel(leftImage: .settingUser,
                                       leftTitle:  "Terms of service".localized,
                                       tapBlock: { [weak self] _, _, _ in
            guard let self = self else { return }
            let vc = TSBusinessWebVC(urlType: .terms)
            vc.hidesBottomBarWhenPushed = true
            self.target.navigationController?.pushViewController(vc, animated: true)
        }))
        
        dataArray.append(TSSetingModel(leftImage: .settingPrivacy,
                                       leftTitle:  "Privacy policy".localized,
                                       tapBlock: { [weak self] _, _, _ in
            guard let self = self else { return }
            let vc = TSBusinessWebVC(urlType: .privacy)
            vc.hidesBottomBarWhenPushed = true
            self.target.navigationController?.pushViewController(vc, animated: true)
        }))
        
        dataArray.append(updateItem)
        
        return dataArray
    }()
    
    
//    lazy var updateItem: TSSetingModel = {
//        return TSSetingModel(leftImage: .settingAboutUs,
//                             leftTitle:  "Update".localized,
//                             rightString: "V"+appShortVersion(),
//                             rightViewStyle: 1,
//                             rectCorner: [.bottomLeft,.bottomRight],
//                             tapBlock: { [weak self] _, _, _ in
//            guard let self = self else { return }
//            if let url = URL(string: "https://apps.apple.com/app/id\(appid)"),
//               UIApplication.shared.canOpenURL(url) {
//                UIApplication.shared.open(url)
//            }
//        })
//    }()
    
    lazy var updateItem: TSSetingModel = {
        return TSSetingModel(leftImage: .settingAboutUs,
                             leftTitle:  "About us".localized,
                             rightString: "V"+appShortVersion(),
                             rightViewStyle: 1,
                             rectCorner: [.bottomLeft,.bottomRight],
                             tapBlock: { [weak self] _, _, _ in
            guard let self = self else { return }
            if let url = URL(string: "https://apps.apple.com/app/id\(appid)"),
               UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url)
            }
        })
    }()
}

extension TSSetingVM {
    
    //评价 App
    static func kRateAction() {
        let countKey = "kProcessCompletedCountKey"
        // 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) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        } else {
            SKStoreReviewController.requestReview()
        }
    }
    
    //首次点击保存弹出系统评分弹窗
    static func kFirstSaveRateAction(){
        if UserDefaults.standard.string(forKey: "iskFirstSaveRateAction") == nil {
            kMainAfter(0.5) {
                Self.kRateAction()
            }
            UserDefaults.standard.set("1", forKey: "iskFirstSaveRateAction")
            UserDefaults.standard.synchronize()
        }
    }
}
