package edu.mingxu.jiaowu;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.getcapacitor.BridgeActivity;
import com.getcapacitor.JSObject;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MainActivity extends BridgeActivity {
    private static final Set<String> SHORTCUT_ROUTES =
        new HashSet<>(Arrays.asList(
            "timetable",
            "exams",
            "attendance",
            "notifications"));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        registerPlugin(NativeHomePlugin.class);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        dispatchShortcut(intent);
    }

    private void dispatchShortcut(Intent intent) {
        Uri data = intent == null ? null : intent.getData();
        if (data == null ||
            !"mingxu".equals(data.getScheme()) ||
            !"open".equals(data.getHost())) {
            return;
        }

        String route = data.getLastPathSegment();
        if (route == null || !SHORTCUT_ROUTES.contains(route)) {
            return;
        }

        NativeHomePlugin.savePendingRoute(this, route);
        if (bridge != null) {
            JSObject detail = new JSObject();
            detail.put("route", route);
            bridge.triggerWindowJSEvent("mingxuShortcut", detail.toString());
        }
    }
}
