//
//  myWalletConnect.swift
//  Runner
//
//  Created by 雨川 on 2022/8/31.
//

import Foundation
import WalletConnectSwift


@objcMembers class MyWalletConnect:NSObject {
    var walletConnect:WalletConnect!
    var client: Client!
    var session: Session!
    var cr:String!
    ///初始化
    func initWalleConnect(){
        walletConnect = WalletConnect(delegate: self)
        walletConnect.reconnectIfNeeded()
        
        NSLog("开始初始化")
        cr = walletConnect.connect()
        client = walletConnect.client
        session = walletConnect.session
    }
    
    ///链接钱包
    func  walleConnectFun(urlTypeStr:String)->Void{
        
        let deepLinkUrl = "wc"+"://wc?uri=\(cr!)"
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            if let url = URL(string: deepLinkUrl), UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } else {
                print(deepLinkUrl);
                NSLog("开始初始化检测不支持")
            }
        }
    }
    
    func customRequests(jsonStr:String){
        let jsonData:Data = jsonStr.data(using: .utf8)!
        
        let dict =  try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as? Dictionary<String, Any>

        try? client.send(.eth_custom(url: walletConnect.session.url,methods:dict?["method"] as! String,param:dict?["params"] as! Array<String>)) { [weak self] response in
            self?.handleReponse(response, expecting: "Gas Price")
        }

    }
    func contractRequest(jsonStr:String){
        let jsonData:Data = jsonStr.data(using: .utf8)!
        print("进入了")
        let dict =  try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as? Dictionary<String, Any>
        let optionsData:Data = (dict?["params"] as! String).data(using: .utf8)!
        let data = try? JSONSerialization.jsonObject(with: optionsData, options: .mutableContainers) as? Dictionary<String, Any>
        var options =         ContractStruct(from: data?["from"] as! String,to:data?["to"] as! String,value: "",data: data?["data"] as! String)
        try? client.send(.eth_custom(url: walletConnect.session.url,methods:dict?["method"] as! String,contractOptions:[options])) { [weak self] response in
            self?.handleReponse(response, expecting: "Gas Price")
        }
        
    }
    func cooidn(json:String,id:String){}
    
    private func handleReponse(_ response: Response, expecting: String) {
        DispatchQueue.main.async {
            if let error = response.error {
                self.show(UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert))
                return
            }
            do {
                let result = try response.result(as: String.self)
                self.show(UIAlertController(title: expecting, message: result, preferredStyle: .alert))
            } catch {
                self.show(UIAlertController(title: "Error",
                                            message: "Unexpected response type error: \(error)",
                                            preferredStyle: .alert))
            }
        }
    }
    private func show(_ alert: UIAlertController) {
        alert.addAction(UIAlertAction(title: "Close", style: .cancel))
//        self.present(alert, animated: true)
    }
    func onMainThread(_ closure: @escaping () -> Void) {
        if Thread.isMainThread {
            closure()
        } else {
            DispatchQueue.main.async {
                closure()
            }
        }
    }
}

struct ContractStruct: Codable, Equatable {
    var from: String;
    var to:String;
    var value:String;
    var data:String;
    
}


extension Request {
    static func eth_getTransactionCount(url: WCURL, account: String) -> Request {
        return try! Request(url: url, method: "eth_getTransactionCount", params: [account, "latest"])
    }

    static func eth_gasPrice(url: WCURL) -> Request {
        return Request(url: url, method: "eth_gasPrice")
    }
    //[{from:"abc"}]
//    ["0x4ede150f62dc21adc0f39a2a02c95a5cc1fd7b2c","0x62cC9fd83d48eFCe313695bA6a3245bCCC7f196e","0x3c3284d10000000000000000000000002eb535d54382ea5ced9183899916a9d39e093877"]
    static func eth_custom(url:WCURL,methods:String,param:Array<String>) -> Request{
        return try! Request(url: url, method: methods, params:param )
    }
    static func eth_custom(url:WCURL,methods:String,contractOptions:Array<ContractStruct>) -> Request{
        return try! Request(url: url, method: methods, params:contractOptions )
    }
}


extension MyWalletConnect: WalletConnectDelegate {
    func failedToConnect() {
        onMainThread { [unowned self] in
            NSLog("didConnect")        }
    }

    func didConnect() {
        onMainThread { [unowned self] in
            print("session+++++==============\(walletConnect.session.url)")
            NSLog("didConnect")
        }
    }

    func didDisconnect() {
        onMainThread { [unowned self] in
            NSLog("didConnect")
        }
    }
}
