package com.bokecc.livemodule.live.multirtc;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.view.BaseRelativeLayout;
import com.bokecc.sdk.mobile.live.common.log.ELog;
import com.bokecc.sdk.mobile.live.stream.live.rtc.HDMediaView;

/**
 * 多人连麦可滑动视频组件 RelativeLayout+ScrollView，横竖屏支持
 * 视频宽高比例124：70
 */
public class MultiRTCVideoItemLayout extends BaseRelativeLayout {

    private final String TAG = "MultiRTCVideoItemLayout";

    private HDMediaView mHdMediaView;
    private String mNickName;
    private boolean mIsVideoOn = true;


    private MultiRTCVideoItemLayout(Context context) {
        super(context);
    }

    public MultiRTCVideoItemLayout(Context context, HDMediaView hdMediaView,String nickName,boolean isVideoOn) {
        super(context);
        if (hdMediaView==null){
            ELog.d(TAG, "[demo_bokecc.MultiRTCVideoItemLayout.MultiRTCVideoItemLayout]  hdMediaView must not be null");
            return;
        }
        this.mHdMediaView = hdMediaView;
        this.mNickName = nickName;
        this.mIsVideoOn = isVideoOn;
        inflateViews();

    }

    private MultiRTCVideoItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private MultiRTCVideoItemLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
    }

    private void inflateViews() {
        removeAllViews();
        if (mIsVideoOn){
            if (mHdMediaView!=null){
                addView(mHdMediaView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }else {
                addCameraOffBgImage();
            }
        }else{
            addCameraOffBgImage();
        }
        TextView textView = new TextView(mContext);
        if (!TextUtils.isEmpty(mNickName)){
            textView.setText(mNickName);
        }
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,1);
        textView.setLayoutParams(layoutParams);
        textView.setMaxLines(1);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_live_multirtc_nickname_cover));
        textView.setPadding(5,5,5,5);
        textView.setTextColor(mContext.getResources().getColor(R.color.white));
        addView(textView);
    }
    private void addCameraOffBgImage(){
        ImageView bgImage = new ImageView(mContext);
        bgImage.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        bgImage.setImageResource(R.drawable.bg_live_multirtc_camera_off);
        bgImage.setScaleType(ImageView.ScaleType.FIT_XY);
        addView(bgImage);
    }

    public void updateState(boolean isVideoOn){
        if (mIsVideoOn!=isVideoOn){
            mIsVideoOn = isVideoOn;
            inflateViews();
        }
    }


}
