import UIKit
import Flutter

// Add these two import lines
import TIMPush
import tencent_cloud_chat_push

// 官方微信 SDK
import WechatOpenSDK


// Add `, TIMPushDelegate` to the following line
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, TIMPushDelegate {
    private let CHANNEL = "wechat_business_view"

    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        // 集成微信商家转账用户确认收款
        let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
        let methodChannel = FlutterMethodChannel(name: CHANNEL, binaryMessenger: controller.binaryMessenger)

        methodChannel.setMethodCallHandler { [weak self] (call, result) in
            if call.method == "openBusinessView" {
                if let args = call.arguments as? [String: Any],
                   let packageInfo = args["package"] as? String {
                    self?.openBusinessView(packageInfo: packageInfo)
                    result(true)
                } else {
                    result(FlutterError(code: "BAD_ARGS", message: "Missing package info", details: nil))
                }
            } else {
                result(FlutterMethodNotImplemented)
            }
        }
        //end
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }

    private func openBusinessView(packageInfo: String) {
        let req = WXOpenBusinessViewReq()
        req.businessType = "requestMerchantTransfer"
        req.query = packageInfo 
        WXApi.send(req) { success in
            print("WXOpenBusinessViewReq send result: \(success)")
        }
    }
    
    // To be deprecated，please use the new field businessID below.
    @objc func offlinePushCertificateID() -> Int32 {
        return TencentCloudChatPushFlutterModal.shared.offlinePushCertificateID();
    }

    // Add this function
    @objc func businessID() -> Int32 {
        return TencentCloudChatPushFlutterModal.shared.businessID();
    }

    // Add this function
    @objc func applicationGroupID() -> String {
        return TencentCloudChatPushFlutterModal.shared.applicationGroupID()
    }
    
    // Add this function
    @objc func onRemoteNotificationReceived(_ notice: String?) -> Bool {
        TencentCloudChatPushPlugin.shared.tryNotifyDartOnNotificationClickEvent(notice)
        return true
    }
}