package com.ez.ertc.meeting.ui;

import android.annotation.SuppressLint;
import android.content.res.Configuration;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;

import com.ez.basertc.RTCConstant;
import com.ez.baselib.utils.LogUtil;
import com.ez.ertc.meeting.ErtcEzmHelper;
import com.ez.ertc.meeting.R;
import com.ez.ertc.meeting.contract.ErtcRoomContract;
import com.ez.ertc.meeting.constant.MtSets;
import com.ez.ertc.meeting.utils.ErtcNetLevelUtils;
import com.ez.ertc.meeting.widget.ErtcTextureView;
import com.ez.ertc.meeting.widget.TextCircleImageView;
import com.ez.ertcmeeting.bean.DeviceState;
import com.ez.ertcmeeting.bean.UserAccount;


public class ErtcRoomItemViewHolder2 implements ErtcRoomContract.ItemViewHolder {
    TextCircleImageView ivAvataR;
    AppCompatImageView ivAudioState2;
    AppCompatImageView ivKpsLevel2;
    TextView tvName2;
    TextView tvAccountStreamId2;
    ErtcTextureView textureView2;
    FrameLayout parent;
    private ErtcRoomContract.Presenter mPresenter;
    private UserAccount mAccount;

    @SuppressLint("NewApi")
    ErtcRoomItemViewHolder2(FrameLayout parent, ErtcRoomContract.Presenter presenter) {
        ivAvataR = parent.findViewById(R.id.ivAvataR);
        ivAudioState2 = parent.findViewById(R.id.ivAudioState2);
        ivKpsLevel2 = parent.findViewById(R.id.ivKpsLevel2);
        tvName2 = parent.findViewById(R.id.tvName2);
        tvAccountStreamId2 = parent.findViewById(R.id.tvAccountStreamId2);
        textureView2 = parent.findViewById(R.id.textureView2);
        this.parent = parent;
        this.mPresenter = presenter;
    }

    @SuppressLint("NewApi")
    @Override
    public void bindView(UserAccount account){
        if (!mPresenter.isPInP()){
            return;
        }
        textureView2.getVideoCanvasView().setRenderType(RTCConstant.ERTC_VIDEO_RENDER_MODE_FIT);
        if(mAccount != null) {
            if(!mAccount.getUserId().equals(account.getUserId())) {
                clearPixels();
                ErtcEzmHelper.ertcEngine.setRemoteView(mAccount.getUserId(), RTCConstant.ERTC_VIDEO_STREAM_TYPE_SMALL, null);
                if(account.getVideoStatus() == DeviceState.DS_Active.getValue()) {
                    ErtcEzmHelper.ertcEngine.setRemoteView(account.getUserId(), RTCConstant.ERTC_VIDEO_STREAM_TYPE_SMALL, textureView2.getVideoCanvasView());
                }
            } else {
                ErtcEzmHelper.ertcEngine.setRemoteView(account.getUserId(), RTCConstant.ERTC_VIDEO_STREAM_TYPE_SMALL, account.getVideoStatus() == DeviceState.DS_Active.getValue() ? textureView2.getVideoCanvasView() : null);
            }
        } else {
            LogUtil.d("EZRTCImpl", "test bindView ErtcRoomItemViewHolder2 account.getVideoStatus()：" + account.getVideoStatus());
            if(account.getVideoStatus() == DeviceState.DS_Active.getValue()) {
                ErtcEzmHelper.ertcEngine.setRemoteView(account.getUserId(), RTCConstant.ERTC_VIDEO_STREAM_TYPE_SMALL, textureView2.getVideoCanvasView());
            }
        }
        if(account.getVideoStatus() == DeviceState.DS_Active.getValue()) {
            mPresenter.setLowStream(account);
        }
        mAccount = account;
        parent.setVisibility(View.VISIBLE);
        tvName2.setText(account.getUserId());
        ivAvataR.setImageDrawable(null);
        ivAvataR.setText(account.getUserId());
//        if (!TextUtils.isEmpty(MeetingUtil.getPortraitFromAccount(account)) && !mPresenter.isDestroy()){
//            Glide.with(ivAvataR)
//                    .load(MeetingUtil.getPortraitFromAccount(account))
//                    .into(ivAvataR);
//        }
        ivKpsLevel2.getDrawable().setLevel(Math.max(ErtcNetLevelUtils.getNetLevel(account.getUpNetQuality()), 0));
        if (account.getAudioStatus() == DeviceState.DS_Active.getValue()) {
            ivAudioState2.getDrawable().setLevel(0);
        } else {
            ivAudioState2.getDrawable().setLevel(ErtcRoomActivity.AUDIO_LEVEL_OFF);
        }
        //视频状态与是否正在共享有关：若正在共享屏幕，不显示关闭状态；若自己正在共享屏幕，按状态显示。
        if (account.getVideoStatus() != DeviceState.DS_Active.getValue()
                && (!mPresenter.getSharingDesktopUserId().equals(account.getUserId())
                || (mPresenter.getSharingDesktopUserId().equals(mPresenter.getMyAccount().getUserId())
                && account.getUserId().equals(mPresenter.getMyAccount().getUserId())))) {
            textureView2.setVisibility(View.GONE);
            ivAvataR.setVisibility(View.VISIBLE);
            clearPixels();
        } else {
            ivAvataR.setVisibility(View.GONE);
            textureView2.setVisibility(View.VISIBLE);
        }
        if (MtSets.isDebug()){
            tvAccountStreamId2.setVisibility(View.VISIBLE);
            tvAccountStreamId2.setText(account.getUserId());
        } else {
            tvAccountStreamId2.setVisibility(View.GONE);
        }
        textureView2.setWaterMark(mPresenter.getMyAccount().getUserId(), mPresenter.getMyAccount().getUserId());
    }
    @SuppressLint("NewApi")
    @Override
    public void unbind() {
        if(mAccount != null && ErtcEzmHelper.ertcEngine != null) {
            ErtcEzmHelper.ertcEngine.setRemoteView(mAccount.getUserId(), RTCConstant.ERTC_VIDEO_STREAM_TYPE_SMALL, null);
        }
        textureView2.cleanLastFrame();
        parent.setVisibility(View.INVISIBLE);
        mAccount = null;
    }

    @Override
    public void clearPixels() {
        textureView2.cleanLastFrame();
    }
    @Override
    public ErtcTextureView getTextureView() {
        return textureView2;
    }
    @Override
    public UserAccount getAccount() {
        return mAccount;
    }
    @Override
    public void updateSelfDelayLevel(int level) {
        ivKpsLevel2.getDrawable().setLevel(level);
    }
    @Override
    public void setAudioLevel(int level) {
        if (mAccount != null) {
            if (mAccount.getAudioStatus() == DeviceState.DS_Active.getValue()) {
                ivAudioState2.getDrawable().setLevel(level);
            } else {
                ivAudioState2.getDrawable().setLevel(ErtcRoomActivity.AUDIO_LEVEL_OFF);
            }
        } else {
            ivAudioState2.getDrawable().setLevel(ErtcRoomActivity.AUDIO_LEVEL_OFF);
        }
    }
    @Override
    public void updateView(int orientation,int landscapeTopMargin) {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) ivAvataR.getLayoutParams();
        if (orientation == Configuration.ORIENTATION_PORTRAIT){
            layoutParams.gravity = Gravity.CENTER;
            layoutParams.topMargin = 0;
        } else {
            layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
            layoutParams.topMargin = landscapeTopMargin;
        }
    }
    @Override
    public void showWaterMark(boolean show) {
        textureView2.showWaterMark(show);
    }

    @Override
    public void updateWaterMark() {
        textureView2.setWaterMark(mPresenter.getMyAccount().getUserId(), mPresenter.getMyAccount().getUserId());
    }
}
