package com.tasapp.capacitor;

import android.Manifest;
import android.content.pm.PackageManager;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.annotation.Permission;
import com.tasapp.android.LaunchNavigatorService;

import org.json.JSONException;
import org.json.JSONObject;

@CapacitorPlugin(
        name = "LaunchNavigatorPlugin",
        permissions = {
                @Permission(strings = { Manifest.permission.INTERNET }, alias = "Internet"),
                @Permission(strings = { Manifest.permission.ACCESS_NETWORK_STATE }, alias = "NetworkState"),
        })
public class LaunchNavigatorPlugin extends Plugin {
    private LaunchNavigatorService launchNavigator;
    private static final String MANIFEST_API_KEY = "launchnavigator.GOOGLE_API_KEY";

    @Override
    public void load() {
        super.load();
        try {
            launchNavigator = new LaunchNavigatorService(getContext());
            String googleApiKey = getActivity().getPackageManager().getApplicationInfo(getActivity().getPackageName(), PackageManager.GET_META_DATA).metaData.getString(MANIFEST_API_KEY);
            if(googleApiKey != null){
                launchNavigator.setGoogleApiKey(googleApiKey);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PluginMethod
    public void navigate(PluginCall call) {
        try {
            if (call.hasOption("enableGeolocation")) {
                launchNavigator.setGeocoding(call.getBoolean("enableGeolocation"));
            }
            JSONObject params = new JSONObject();
            params.put("app", call.getString("app"));
            params.put("dType", call.getString("dType"));
            params.put("dest", call.getString("dest"));
            params.put("destNickname", call.getString("destNickname"));
            params.put("sType", call.getString("sType"));
            params.put("start", call.getString("start"));
            params.put("startNickname", call.getString("startNickname"));
            params.put("transportMode", call.getString("transportMode"));
            params.put("launchMode", call.getString("launchMode"));
            params.put("extras", call.getString("extras"));
            String error = launchNavigator.navigate(params);
            call.resolve();
        } catch (Exception e) {
            e.printStackTrace();
            call.reject(e.getMessage());
        }
    }

    @PluginMethod
    public void discoverSupportedApps(PluginCall call) {
        try {
            JSONObject apps = launchNavigator.getGeoApps();
            JSObject result = JSObject.fromJSONObject(apps);
            call.resolve(result);
        } catch (JSONException e) {
            e.printStackTrace();
            call.reject(e.getMessage());
        }
    }

    @PluginMethod
    public void availableApps(PluginCall call) {
        try {
            JSONObject apps = launchNavigator.getAvailableApps();
            JSObject result = JSObject.fromJSONObject(apps);
            call.resolve(result);
        } catch (Exception e) {
            e.printStackTrace();
            call.reject(e.getMessage());
        }
    }

    @PluginMethod
    public void isAppAvailable(PluginCall call) {
        boolean available = launchNavigator.isAppAvailable(call.getString("appName"));
        JSObject res = new JSObject();
        res.put("isAvailable", available);
        call.resolve(res);
    }
}
