package com.ttiglobal.inttilectgpchat.training.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;

import androidx.fragment.app.FragmentActivity;

import com.facebook.react.modules.core.PermissionListener;

import org.jetbrains.annotations.NotNull;
import org.jitsi.meet.sdk.JitsiMeetActivityDelegate;
import org.jitsi.meet.sdk.JitsiMeetActivityInterface;
import org.jitsi.meet.sdk.JitsiMeetConferenceOptions;
import org.jitsi.meet.sdk.JitsiMeetUserInfo;
import org.jitsi.meet.sdk.JitsiMeetView;
import org.jitsi.meet.sdk.JitsiMeetViewListener;

import java.net.URL;
import java.util.Map;

public class JitsiActivity extends FragmentActivity implements JitsiMeetActivityInterface {
    private JitsiMeetView view;
    public static Schedules SCHEDULE;
    public static boolean isBroadcaster;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        JitsiMeetActivityDelegate.onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    public void onBackPressed() {
        JitsiMeetActivityDelegate.onBackPressed();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = new JitsiMeetView(this);

        view.setListener(new JitsiMeetViewListener() {
            @SuppressLint("LogNotTimber")
            @Override
            public void onConferenceJoined(Map<String, Object> map) {
                Log.i("onConferenceJoined", map.toString());
            }

            @SuppressLint("LogNotTimber")
            @Override
            public void onConferenceTerminated(Map<String, Object> map) {
                Log.i("onConferenceTerminated", map.toString());
                finish();
            }

            @SuppressLint("LogNotTimber")
            @Override
            public void onConferenceWillJoin(Map<String, Object> map) {
                Log.i("onConferenceWillJoin", map.toString());
            }
        });

        try {
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            if (am != null) {
                am.setStreamVolume(AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        JitsiMeetUserInfo userInfo = new JitsiMeetUserInfo();
//        userInfo.setDisplayName(Utils.getName(JitsiActivity.this));
//        userInfo.setEmail(Utils.getUserId(JitsiActivity.this));

        JitsiMeetConferenceOptions options = null;

        Bundle bundle = new Bundle();
        Bundle header = new Bundle();
        header.putString("background", "rgb(20, 106, 34)");
        header.putString("icon", "rgb(255, 255, 255)");
        header.putString("statusBar", "rgba(20, 106, 34, 0.5)");
        header.putString("statusBarContent", "rgb(255, 255, 255)");
        header.putString("text", "rgb(255, 255, 255)");
        bundle.putBundle("Header", header);

        try {
            options = new JitsiMeetConferenceOptions.Builder()
                    .setServerURL(new URL("https://meet.jit.si"))
                    .setRoom(SCHEDULE.getChannel())
                    .setAudioMuted(!isBroadcaster)
                    .setVideoMuted(!isBroadcaster)
                    .setAudioOnly(false)
                    .setWelcomePageEnabled(false)
                    .setUserInfo(userInfo)
                    .setSubject(SCHEDULE.getTitle())
                    .setFeatureFlag("add-people.enabled", false)
                    .setFeatureFlag("calendar.enabled", false)
                    .setFeatureFlag("call-integration.enabled", false)
                    .setFeatureFlag("close-captions.enabled", false)
                    .setFeatureFlag("invite.enabled", false)
                    .setFeatureFlag("ios.recording.enabled", false)
                    .setFeatureFlag("live-streaming.enabled", false)
                    .setFeatureFlag("meeting-password.enabled", false)
                    .setFeatureFlag("recording.enabled", false)
                    .setColorScheme(bundle)
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        view.join(options);
        setContentView(view);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        view.dispose();
        view = null;
        JitsiMeetActivityDelegate.onHostDestroy(this);
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        JitsiMeetActivityDelegate.onNewIntent(intent);
    }

    @Override
    public void onRequestPermissionsResult(final int requestCode, @NotNull final String[] permissions, @NotNull final int[] grantResults) {
        JitsiMeetActivityDelegate.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JitsiMeetActivityDelegate.onHostResume(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        JitsiMeetActivityDelegate.onHostPause(this);
    }

    @Override
    public void requestPermissions(String[] strings, int i, PermissionListener permissionListener) {

    }
}
