import Foundation
import Capacitor
import NativeLib

@objc(LaunchNavigatorPlugin)
public class LaunchNavigatorPlugin: CAPPlugin {
    var launchService = LaunchNavigatorService.shared()
    
    @objc func navigate(_ call: CAPPluginCall) {
        do {
            var params: Dictionary <String, String> = [:]
            
            params["dest"] = call.getString("dest") ?? nil
            params["destType"] = call.getString("dType") ?? nil
            params["destName"] = call.getString("destNickname") ?? nil
            params["start"] = call.getString("start") ?? nil
            params["startType"] = call.getString("sType") ?? nil
            params["startName"] = call.getString("startNickname") ?? nil
            params["app"] = call.getString("app") ?? nil
            params["dest"] = call.getString("dest") ?? nil
            params["transportMode"] = call.getString("transportMode") ?? nil
            params["launchMode"] = call.getString("launchMode") ?? nil
            params["extras"] = call.getString("extras") ?? nil
            params["currentLocation"] = call.getString("currentLocation") ?? nil
            
            try launchService.navigate(params)
            call.resolve()
        } catch {
            call.reject("Error open app")
        }
    }
    
    @objc func availableApps(_ call: CAPPluginCall) {
        do {
            let result = try launchService.availableApps()
            call.resolve(result as! PluginCallResultData)
        } catch {
            call.reject("Error getting apps")
        }
    }
    
    @objc func isAppAvailable(_ call: CAPPluginCall) {
        do {
            let appName = call.getString("appName", "")
            let result = try launchService.isAppAvailable(appName)
            call.resolve(["isAvailable" : result])
        } catch {
            call.reject("Error getting app")
        }
    }
}
