package com.onmeetingsdk;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;

import org.onmeeting.meet.sdk.OnMeetingMeetViewListener;

import java.util.Map;

@ReactModule(name = RNOnMeetingMeetViewManager.REACT_CLASS)
public class RNOnMeetingMeetViewManager extends SimpleViewManager<RNOnMeetView> implements OnMeetingMeetViewListener {
    public static final String REACT_CLASS = "OnmeetingSdk";
    private IRNOnMeetingMeetViewReference mJitsiMeetViewReference;
    private ReactApplicationContext mReactContext;

    public RNOnMeetingMeetViewManager(ReactApplicationContext reactContext, IRNOnMeetingMeetViewReference jitsiMeetViewReference) {
        mJitsiMeetViewReference = jitsiMeetViewReference;
        mReactContext = reactContext;
    }

    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    public RNOnMeetView createViewInstance(ThemedReactContext context) {
        if (mJitsiMeetViewReference.getOnMeetingMeetView() == null) {
            RNOnMeetView view = new RNOnMeetView(context.getCurrentActivity());
            view.setListener(this);
            mJitsiMeetViewReference.setOnMeetingMeetView(view);
        }
        return mJitsiMeetViewReference.getOnMeetingMeetView();
    }

    public void onConferenceJoined(Map<String, Object> data) {
        WritableMap event = Arguments.createMap();
        event.putString("url", (String) data.get("url"));
        mReactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                mJitsiMeetViewReference.getOnMeetingMeetView().getId(),
                "conferenceJoined",
                event);
    }

    public void onConferenceTerminated(Map<String, Object> data) {
        WritableMap event = Arguments.createMap();
        event.putString("url", (String) data.get("url"));
        event.putString("error", (String) data.get("error"));
        mReactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                mJitsiMeetViewReference.getOnMeetingMeetView().getId(),
                "conferenceTerminated",
                event);
    }

    public void onConferenceWillJoin(Map<String, Object> data) {
        WritableMap event = Arguments.createMap();
        event.putString("url", (String) data.get("url"));
        mReactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                mJitsiMeetViewReference.getOnMeetingMeetView().getId(),
                "conferenceWillJoin",
                event);
    }

    public Map getExportedCustomBubblingEventTypeConstants() {
        return MapBuilder.builder()
                .put("conferenceJoined", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onConferenceJoined")))
                .put("conferenceTerminated", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onConferenceTerminated")))
                .put("conferenceWillJoin", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onConferenceWillJoin")))
                .build();
    }


    @Override
    public void onConferenceWillLeave(Map<String, Object> map) {

    }

    @Override
    public void onLoadConfigError(Map<String, Object> map) {

    }

    @Override
    public void onConferenceLeft(Map<String, Object> map) {

    }

    @Override
    public void onConferenceFailed(Map<String, Object> map) {

    }
}
