package com.rnenxrtc;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.ThemedReactContext;

import java.util.concurrent.ConcurrentHashMap;

import enx_rtc_android.Controller.EnxPlayerView;
import enx_rtc_android.Controller.EnxStream;

import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;
import android.widget.Toast;

public class EnxPlayerLayout extends FrameLayout {

    private Activity activity;
    private EnxRN sharedState;
    private ThemedReactContext mReactContext;
    private String mStreamId;

    public EnxPlayerLayout(ThemedReactContext reactContext) {
        super(reactContext);
        this.sharedState = EnxRN.getSharedState();
        this.mReactContext = reactContext;
        this.activity = ((ReactContext) getContext()).getCurrentActivity();
    }

    private EnxPlayerView getPlayerView() {
        return new EnxPlayerView(activity, EnxPlayerView.ScalingType.SCALE_ASPECT_FIT, false);
    }

    public void createPublisherView(final String streamId) {
        this.mStreamId = streamId;
        try {
            ConcurrentHashMap<String, EnxPlayerView> mPlayers = sharedState.getPlayerView();

            if (mStreamId.length() > 4) {
                EnxPlayerView playerView = getPlayerView();
                ConcurrentHashMap<String, EnxStream> mEnxStream = sharedState.getLocalStream();
                EnxStream localStream = mEnxStream.get(mStreamId);

                if (localStream != null) {
                    playerView.setZOrderMediaOverlay(true);
                    localStream.attachRenderer(playerView);
                    addView(playerView);
                }
                mPlayers.put(mStreamId, playerView);

            } else {
                ConcurrentHashMap<String, EnxStream> mEnxStream = sharedState.getRemoteStream();
                EnxStream remoteStream = mEnxStream.get(mStreamId);

                if (remoteStream != null) {
                    addPlayer(remoteStream.mEnxPlayerView);
                }
                mPlayers.put(mStreamId, remoteStream.mEnxPlayerView);
            }
            invalidate();
            requestLayout();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void addPlayer(EnxPlayerView enxPlayerView) {
        try {
            ViewGroup parent = (ViewGroup) enxPlayerView.getParent();
            if (parent == null) {
                enxPlayerView.setScalingType(EnxPlayerView.ScalingType.SCALE_ASPECT_FILL);
                addView(enxPlayerView);
            } else {
                parent.removeView(enxPlayerView);
                enxPlayerView.setScalingType(EnxPlayerView.ScalingType.SCALE_ASPECT_FILL);
                addView(enxPlayerView);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

