package com.tencent.trtc;

import android.content.Context;
import android.util.AttributeSet;
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 Context mContext;
    private View mView;

    private TXCloudVideoView videoView;
    private TextView titleView;

    private UserInfo userInfo;

    private boolean alwaysHide = false;
    private boolean mainView;


    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(TRTCCloud mTRTCCloud, UserInfo userInfo) {
        if(userInfo == null){
            setVisibility(View.GONE);
        }else {
            setVisibility(alwaysHide?INVISIBLE:VISIBLE);
        }
        if(isChanged(userInfo)){
            this.userInfo = userInfo;
            if(this.userInfo != null){
                if(this.userInfo.isLocal()){
                    mTRTCCloud.startLocalPreview(userInfo.isFrontCamera(), videoView);
                    mTRTCCloud.startLocalAudio(TRTCCloudDef.TRTC_AUDIO_QUALITY_SPEECH);
                } else {
                    mTRTCCloud.startRemoteView(this.userInfo.getPersonid(), TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_SMALL, videoView);
                }
                if(this.userInfo.getDisplayName() == null || this.userInfo.getDisplayName().length() == 0){
                    this.titleView.setText(this.userInfo.getPersonid());
                }else {
                    this.titleView.setText(this.userInfo.getDisplayName());
                }
                this.titleView.setVisibility(VISIBLE);
            }else {
                this.titleView.setVisibility(INVISIBLE);
            }
        }
    }

    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) {
        this.mainView = mainView;
        if(!mainView){
            Events.addListener("subview.always.hide",(extra)->{
                this.alwaysHide = extra.getBoolean("alwaysHide",false);
                this.setVisibility(alwaysHide?INVISIBLE:VISIBLE);
            });
        }
        Events.addListener("userinfo.update",(extra -> {
            if(this.userInfo !=null && this.userInfo.getDisplayName() == null
                    && this.userInfo.getDisplayName().length() == 0
                    && extra.getString("userId").equals(this.userInfo.getPersonid())){
                this.titleView.setText(extra.getString("displayName",this.userInfo.getDisplayName()));
            }
        }));
        return this;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }
}
