package com.tencent.trtc;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tencent.rtmp.ui.TXCloudVideoView;
import com.tencent.trtc.event.Events;
import com.tencent.trtc.videocall.UserInfo;

public class CustomVideoView extends RelativeLayout {

    private static final String TAG = "CustomVideoView";

    private Context mContext;
    private View mView;

    private TXCloudVideoView videoView;
    private TextView titleView;

    private UserInfo userInfo;

    private boolean alwaysHide = false;
    private boolean mainView;

    private TRTCCloud mTRTCCloud;


    public CustomVideoView(Context context) {
        this(context,null);
    }

    public CustomVideoView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(getId("custom_video_view_layout", "layout"), this, true);
        videoView = (TXCloudVideoView) findViewById(getId("video", "id"));
        titleView = (TextView)  findViewById(getId("title","id"));
    }


    private int getId(String idName, String type) {
        return getResources().getIdentifier(idName, type, mContext.getPackageName());
    }

    public void changeUser(UserInfo userInfo) {
        if(this.mTRTCCloud == null){
            this.mTRTCCloud = TRTCCloud.sharedInstance(mContext);
        }
        Log.d(TAG,"TRTC - changeUser: main?:"+mainView+",alwaysHide?:"+alwaysHide+",user:" + (userInfo == null ? null : userInfo.getPersonid()));
        this.setBackgroundColor(Color.TRANSPARENT);
        if(userInfo == null){
            setVisibility(View.GONE);
        }else {
            setVisibility(alwaysHide?INVISIBLE:VISIBLE);
        }
        if(isChanged(userInfo)){
            this.titleView.setVisibility(INVISIBLE);
            this.userInfo = userInfo;
            if(this.userInfo != null){
                TRTCCloudDef.TRTCRenderParams params = new TRTCCloudDef.TRTCRenderParams();
                params.rotation = 0;
                params.fillMode = this.userInfo.isShareUser() ? TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FIT : TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FILL;
                if(this.userInfo.isLocal()){
                    mTRTCCloud.startLocalPreview(userInfo.isFrontCamera(), videoView);
                    mTRTCCloud.startLocalAudio(TRTCCloudDef.TRTC_AUDIO_QUALITY_SPEECH);
                    mTRTCCloud.setLocalRenderParams(params);
                } else {

                    mTRTCCloud.setRemoteRenderParams(this.userInfo.getPersonid(),mainView ? TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_BIG : TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_SUB,params);
                    try {
                        if(this.userInfo.isShareUser()){
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mTRTCCloud.startRemoteView(this.userInfo.getPersonid(), mainView ? TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_BIG : TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_SMALL, videoView);
                }
                if(this.userInfo.getDisplayName() == null || this.userInfo.getDisplayName().length() == 0){
                    this.titleView.setText(this.userInfo.getPersonid() + (this.userInfo.isShareUser() ? "的屏幕分享": ""));
                }else {
                    this.titleView.setText(this.userInfo.getDisplayName() + (this.userInfo.isShareUser() ? "的屏幕分享": ""));
                }
                if(this.userInfo.isLocal()){
                    this.titleView.setText("我");
                }
                this.titleView.setVisibility(VISIBLE);
                if(!alwaysHide){
                    if(!mainView){
                        this.setBackgroundColor(Color.BLACK);
                    }
                }
            }
        }
        Log.d(TAG,"TRTC - changeUser >>>>"+(userInfo == null ? null : userInfo.getPersonid())+"|"+ ((ColorDrawable)getBackground()).getColor());
    }


    private boolean isChanged(UserInfo userInfo){
        return (this.userInfo == null && userInfo !=null) ||
                (this.userInfo != null && !this.userInfo.equals(userInfo));
    }

    public boolean isAlwaysHide() {
        return alwaysHide;
    }

    public CustomVideoView setAlwaysHide(boolean alwaysHide) {
        this.alwaysHide = alwaysHide;
        return this;
    }

    public boolean isMainView() {
        return mainView;
    }

    public CustomVideoView setMainView(boolean mainView) {
        Log.d(TAG,"TRTC - setMainView mainView:"+mainView);
        if(this.mTRTCCloud == null){
            this.mTRTCCloud = TRTCCloud.sharedInstance(mContext);
        }
        this.mainView = mainView;
        if(!mainView){
            Events.addListener("subview.always.hide",(extra)->{
                Log.d(TAG,"TRTC - event listener (subview.always.hide):"+extra);
                this.alwaysHide = extra.getBoolean("alwaysHide",false);
                if(this.alwaysHide){
                    this.setVisibility(INVISIBLE);
                    if(this.getUserInfo() != null){
                        if(!this.getUserInfo().isLocal()){
                            mTRTCCloud.stopRemoteView(this.getUserInfo().getPersonid(),TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_SUB);
                        }
                    }
                } else {
                    if(this.getUserInfo() != null){
                        this.setVisibility(VISIBLE);
                        if(!this.getUserInfo().isLocal()){
                            mTRTCCloud.startRemoteView(
                                    this.getUserInfo().getPersonid(),
                                    this.mainView ? TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_BIG : TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_SMALL,
                                    getVideoView());
                        }
                    }
                }
            });
        }
        Events.addListener("userinfo.update",(extra) -> {
            Log.d(TAG,"TRTC - event listener (userinfo.update):"+extra + ",userinfo:"+this.userInfo);
            if(this.userInfo !=null && !this.userInfo.isLocal() && extra.getString("userId").equals(this.userInfo.getPersonid())){
                this.userInfo.setDisplayName(extra.getString("displayName"));
                this.titleView.setText(this.userInfo.getDisplayName() + (this.userInfo.isShareUser() ? "的屏幕分享": ""));
            }
        });
        return this;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public TXCloudVideoView getVideoView() {
        return videoView;
    }
}
