package com.huawei.interactivebroadcastingwithcdnstreaming.screenlive.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.huawei.hwinteractivesdk.HWConstants;
import com.huawei.hwinteractivesdk.HWRtcEngine;
import com.huawei.hwinteractivesdk.HWRtcEngineEventHandler;
import com.huawei.interactivebroadcastingwithcdnstreaming.R;

import static com.huawei.interactivebroadcastingwithcdnstreaming.MainActivity.CLIENT_ROLE_BROADCASTER;


public class AudienceActivity extends Activity {

    private static final String TAG = "audience";

    private FrameLayout mFlRoot;
    private HWRtcEngine mRtcEngine;

    private String mChannelName;
    private String mPublishUrl = "";
    private String mAPPid = "";
    private String mTempToken;
    private String mUid;
    private int mRole;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audience);

        mFlRoot = (FrameLayout) findViewById(R.id.fl_root);

        Intent i = getIntent();
        mChannelName = i.getStringExtra("CHANNEL");
        mPublishUrl = i.getStringExtra("URL");
        mAPPid = i.getStringExtra("APPID");
        mTempToken = i.getStringExtra("TOKEN");
        mUid = i.getStringExtra("UID");
        mRole = i.getIntExtra("ROLE",CLIENT_ROLE_BROADCASTER);

        initEngineAndJoin();
    }

    private void initEngineAndJoin() {
        try {
            mRtcEngine = HWRtcEngine.getInstance().create(getApplicationContext(), getString(R.string.huawei_app_id), new HWRtcEngineEventHandler() {

                @Override
                public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
                    Log.d(TAG, "onJoinChannelSuccess: " + (uid&0xFFFFFFL));
                }

                @Override
                public void onUserJoined(final int uid, int elapsed) {
                    Log.d(TAG, "onUserJoined: " + (uid&0xFFFFFFL));
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            setupRemoteView(uid);
                        }
                    });
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        mRtcEngine.enableVideo();
        mRtcEngine.setClientRole(HWConstants.CLIENT_ROLE_AUDIENCE);

        mRtcEngine.joinChannel(mTempToken, mChannelName, "", mUid,"", mRole);
    }

    private void setupRemoteView(int uid) {
        SurfaceView surfaceV = HWRtcEngine.CreateRendererView(getApplicationContext());
        surfaceV.setZOrderOnTop(true);
        surfaceV.setZOrderMediaOverlay(true);
        mFlRoot.addView(surfaceV, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        mRtcEngine.setupRemoteVideo(surfaceV, HWRtcEngine.RENDER_MODE_HIDDEN,uid);
    }

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

        mRtcEngine.leaveChannel();

        mRtcEngine.release();
        mRtcEngine = null;
    }

}
