package com.bokecc.livemodule.live.morefunction.rtc;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bokecc.dwlivedemo.BuildConfig;
import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.live.DWLiveCoreHandler;
import com.bokecc.livemodule.utils.AppRTCAudioManager;
import com.bokecc.livemodule.view.BaseRelativeLayout;
import com.bokecc.sdk.mobile.live.common.log.ELog;
import com.bokecc.sdk.mobile.live.stream.RemoteStreamInfo;
import com.bokecc.sdk.mobile.live.stream.live.rtc.HDMediaView;


/**
 * CC 直播连麦视频展示控件
 */
public class RTCVideoLayout extends BaseRelativeLayout {

    private static final String TAG = "RTCVideoLayout";


    private AppRTCAudioManager mAudioManager;

    // 是否重新计算大小
    private boolean needAdjust = false;
    private ViewGroup rtcRoot;
    private String mUserId;  //老师流id

    public RTCVideoLayout(Context context) {
        super(context);
    }

    private TextView testTv;

    @Override
    public void initViews() {
        rtcRoot = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.live_portrait_rtc_video, this, true);
        if (BuildConfig.DEBUG){
            testTv = new TextView(mContext);
            testTv.setText("RTC Layout");
            testTv.setTextColor(Color.parseColor("#ff0000"));
            addView(testTv);
        }
    }

    // 连麦断开
    public void disconnectSpeak() {
        ELog.d(TAG, "[demo_bokecc.RTCVideoLayout.disconnectSpeak]  ");
        post(new Runnable() {
            @Override
            public void run() {
//                toastOnUiThread("连麦中断");
                if (!DWLiveCoreHandler.getInstance().isMultiStreamMode()){
                    setVisibility(GONE);
                    try{
                        destroy();
                    }catch (Exception e){
                         e.printStackTrace();
                    }
                }

            }
        });
    }

    // 连麦错误
    public void speakError(final String msg,final int toastSeconds) {
        post(new Runnable() {
            @Override
            public void run() {
                toastOnUiThread("连麦错误 ：" + msg,toastSeconds*1000);
                setVisibility(GONE);
                if (mAudioManager != null) {
                    mAudioManager.stop();
                }
                destroy();
            }
        });
    }

    public void destroy() {
        if (mAudioManager != null) {
            mAudioManager.stop();
        }
    }




    public void enterSpeak(boolean mIsVideoRtc) {
        if (DWLiveCoreHandler.getInstance().isMultiRtc()){
            ELog.d(TAG, "[demo_bokecc.RTCVideoLayout.enterSpeak]  [mIsVideoRtc="+mIsVideoRtc+"]  ");
            setVisibility(VISIBLE);
        }else {
            if (mIsVideoRtc) {
                setVisibility(VISIBLE);
            } else {
                setVisibility(GONE);
            }
            try{
                mAudioManager = AppRTCAudioManager.create(mContext);
                mAudioManager.start(null);
            }catch (Exception e){
                 e.printStackTrace();
            }

        }
    }

    /**
     * 多人连麦调用添加老师布局
     * @param hdMediaView
     * @param userId
     * @param role
     */
    public void addVideo(final HDMediaView hdMediaView, final String userId,String role, final RemoteStreamInfo info){
        ELog.d(TAG, "[demo_bokecc.RTCVideoLayout.addVideo]  [HDMediaView, userId="+userId+", role="+role+", info]  ");
        post(new Runnable() {
            @Override
            public void run() {
                mUserId = userId;
                rtcRoot.removeAllViews();
                rtcRoot.addView(hdMediaView);
                if (BuildConfig.DEBUG){
                    removeView(testTv);
                    addView(testTv);
                }
            }
        });
    }

    /**
     * 移除老师流
     * @param userId
     */
    public void removeVideo(final String userId) {
        ELog.d(TAG, "[demo_bokecc.RTCVideoLayout.removeVideo]  [userId="+userId+"]  ");
        post(new Runnable() {
            @Override
            public void run() {
                if (userId.equals(mUserId)){
                    rtcRoot.removeAllViews();
                }
            }
        });

    }
}
