package app.alan.reactmodule;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import com.alan.alansdk.Alan;
import com.alan.alansdk.AlanCallback;
import com.alan.alansdk.AlanConfig;
import com.alan.alansdk.AlanState;
import com.alan.alansdk.button.AlanButton;
import com.alan.alansdk.events.EventCommand;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.annotations.ReactPropGroup;
import org.json.JSONException;

public class AlanButtonViewManager extends SimpleViewManager<View> {

    private DummyView container;

    public static final String REACT_CLASS = "RNTAlanButton";
    ReactApplicationContext mCallerContext;

    public AlanButtonViewManager(ReactApplicationContext reactContext) {
        mCallerContext = reactContext;
    }

    @Override
    public void updateExtraData(View root, Object extraData) {
        super.updateExtraData(root, extraData);
    }

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

    @Override
    public void receiveCommand(View root, int commandId, @javax.annotation.Nullable ReadableArray args) {
        super.receiveCommand(root, commandId, args);
    }

    @Override
    public View createViewInstance(ThemedReactContext context) {
        LifecycleEventListener listener = new LifecycleEventListener() {
            @Override
            public void onHostResume() {
            }

            @Override
            public void onHostPause() {
            }

            @Override
            public void onHostDestroy() {
                if (AlanManager.INSTANCE.alanButton != null) {
                    AlanManager.INSTANCE.removeButton();
                }
            }
        };
        context.addLifecycleEventListener(listener);

        AlanManager.INSTANCE.createButton(mCallerContext);
        // AlanManager.INSTANCE.requestAudioPermissions();

        container = new DummyView(context);
        return container;
    }

    private String host;
    private String projectId;
    private String authData;

    @ReactProp(name = "params")
    public void setParams(View v, @Nullable ReadableArray params) {
        AlanButton alanButton = AlanManager.alanButton;
        String projectParam = params.getString(0);
        if (projectParam != null) {
            this.projectId = projectParam;
        }
        else {
            return;
        }
        String hostParam = params.getString(1);
        if (hostParam != null) {
            this.host = "wss://" + hostParam;
        }
        else {
            this.host = "wss://v1.alan.app";
        }
        try {
            if (!params.isNull(2)) {
                String dataString = JsonConverter.reactToJSON(params.getMap(2)).toString();
                this.authData = dataString;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String version = params.getString(3);
        if (alanButton != null) {
            AlanConfig config = AlanConfig.builder()
                    .setProjectId(projectId)
                    .setServer(host)
                    .setDataObject(authData)
                    .build();
            alanButton.initWithConfig(config);
            alanButton.hideButton();
            alanButton.hideHintPanel();
            AlanManager.INSTANCE.registerCallbacks();
        }
    }

}