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.baselib.utils.LogUtil;
import com.ez.basertc.RTCConstant;
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;

/**
 * @author xiezuoyuan
 * @time 2020/5/21 10:08
 */
public class ErtcRoomItemViewHolderSelf implements ErtcRoomContract.ItemViewHolder {
    TextCircleImageView ivAvatarSelf;
    AppCompatImageView ivAudioStateSelf;
    AppCompatImageView ivKpsLevelSelf;
    TextView tvNameSelf;
    TextView tvAccountStreamIdSelf;
    ErtcTextureView textureViewSelf;
    FrameLayout parent;
    private ErtcRoomContract.Presenter mPresenter;
    private UserAccount mAccount;
    private boolean isBindLocalView = false;
    private int isBindVideoState = DeviceState.DS_Closed.getValue();

    @SuppressLint("NewApi")
    ErtcRoomItemViewHolderSelf(FrameLayout parent, ErtcRoomContract.Presenter presenter) {
        ivAvatarSelf = parent.findViewById(R.id.ivAvatarSelf);
        ivAudioStateSelf = parent.findViewById(R.id.ivAudioStateSelf);
        ivKpsLevelSelf = parent.findViewById(R.id.ivKpsLevelSelf);
        tvNameSelf = parent.findViewById(R.id.tvNameSelf);
        tvAccountStreamIdSelf = parent.findViewById(R.id.tvAccountStreamIdSelf);
        textureViewSelf = parent.findViewById(R.id.textureViewSelf);
        this.parent = parent;
        this.mPresenter = presenter;
    }

    @SuppressLint("NewApi")
    @Override
    public void bindView(UserAccount account) {
        LogUtil.d("EZRTCImpl", "self bindView account:" + account.getUserId());
        textureViewSelf.getVideoCanvasView().setRenderType(RTCConstant.ERTC_VIDEO_RENDER_MODE_FIT);
        if(mAccount == null) {
            if(account.getVideoStatus() == DeviceState.DS_Active.getValue()) {
                ErtcEzmHelper.ertcEngine.setLocalView(textureViewSelf.getVideoCanvasView());
                isBindVideoState = account.getVideoStatus();
                isBindLocalView = true;
                textureViewSelf.hideProgress();
            }
        } else {
            if(!isBindLocalView || isBindVideoState != account.getVideoStatus()) {
                ErtcEzmHelper.ertcEngine.setLocalView(account.getVideoStatus() == DeviceState.DS_Active.getValue() ? textureViewSelf.getVideoCanvasView() : null);
                isBindVideoState = account.getVideoStatus();
                textureViewSelf.hideProgress();
            }
        }
        mAccount = account;
        //mPresenter.setLowStream(account);
        parent.setVisibility(View.VISIBLE);
        tvNameSelf.setText(account.getUserId());
        ivAvatarSelf.setImageDrawable(null);
        ivAvatarSelf.setText(account.getUserId());
//        String portrait = MeetingUtil.getPortraitFromAccount(account);
//        if (!TextUtils.isEmpty(portrait) && !mPresenter.isDestroy()) {
//            Glide.with(ivAvatarSelf)
//                    .load(portrait)
//                    .into(ivAvatarSelf);
//        }
        ivKpsLevelSelf.getDrawable().setLevel(Math.max(ErtcNetLevelUtils.getNetLevel(account.getUpNetQuality()), 0));
        if (account.getAudioStatus() == DeviceState.DS_Active.getValue()) {
            ivAudioStateSelf.getDrawable().setLevel(0);
        } else {
            ivAudioStateSelf.getDrawable().setLevel(ErtcRoomActivity.AUDIO_LEVEL_OFF);
        }
        if (account.getVideoStatus() != DeviceState.DS_Active.getValue()) {
            clearPixels();
            textureViewSelf.setVisibility(View.GONE);
            ivAvatarSelf.setVisibility(View.VISIBLE);
        } else {
            ivAvatarSelf.setVisibility(View.GONE);
            textureViewSelf.setVisibility(View.VISIBLE);
        }
        if (MtSets.isDebug()) {
            tvAccountStreamIdSelf.setVisibility(View.VISIBLE);
            tvAccountStreamIdSelf.setText(account.getUserId());
        } else {
            tvAccountStreamIdSelf.setVisibility(View.GONE);
        }
        textureViewSelf.setWaterMark(mPresenter.getMyAccount().getUserId(), mPresenter.getMyAccount().getUserId());
    }

    @SuppressLint("NewApi")
    @Override
    public void unbind() {
        if(ErtcEzmHelper.ertcEngine != null) {
            ErtcEzmHelper.ertcEngine.setLocalView(null);
        }
        textureViewSelf.cleanLastFrame();
        parent.setVisibility(View.INVISIBLE);
        mAccount = null;
    }

    @Override
    public void clearPixels() {
        textureViewSelf.cleanLastFrame();
    }

    @Override
    public ErtcTextureView getTextureView() {
        return textureViewSelf;
    }

    @Override
    public UserAccount getAccount() {
        return mAccount;
    }

    @Override
    public void updateSelfDelayLevel(int level) {
        ivKpsLevelSelf.getDrawable().setLevel(level);
    }

    @Override
    public void setAudioLevel(int level) {
        ivAudioStateSelf.getDrawable().setLevel(level);
    }

    @Override
    public void updateView(int orientation, int landscapeTopMargin) {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) ivAvatarSelf.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) {
        textureViewSelf.showWaterMark(show);
    }

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