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 {

    Activity activity=null;
    EnxRN sharedState;
    ThemedReactContext mReactContext;
    String mStreamId;
    public EnxPlayerLayout(ThemedReactContext reactContext) {
        super(reactContext);
        sharedState = EnxRN.getSharedState();
        mReactContext = reactContext;
        //activity = getContext();
        activity=((ReactContext)getContext()).getCurrentActivity();
    }
    private EnxPlayerView getPlayerView(){
            EnxPlayerView enxPlayerView = new EnxPlayerView(activity, EnxPlayerView.ScalingType.SCALE_ASPECT_FIT
                         , false);
            return enxPlayerView;
    }
   
    public void createPublisherView(final String streamId) {
        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);
                
                    //addPlayer(localStream.mEnxPlayerView);
                }
                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 r = ((ViewGroup) enxPlayerView.getParent());
            if(r==null){
                enxPlayerView.setScalingType(EnxPlayerView.ScalingType.SCALE_ASPECT_FILL);
             //   enxPlayerView.bringToFront();
                addView(enxPlayerView);
            }else{
                FrameLayout layout = (FrameLayout) r;
                layout.removeView(enxPlayerView);
                enxPlayerView.setScalingType(EnxPlayerView.ScalingType.SCALE_ASPECT_FILL);
              //  enxPlayerView.bringToFront();
                addView(enxPlayerView);
            }
        }catch(Exception exception){
            exception.printStackTrace();
        }
    }
}

