/**
 * 码流信息展示 Helper
 * 从 EZRealPlayActivity 拆分出来，负责取流方式、码流速率、更多码流信息的展示
 */
package com.videogo.ui.realplay.helper;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.videogo.constant.Constant;
import com.videogo.ui.common.EZBusinessTool;
import com.videogo.ui.realplay.EZRealPlayActivity;
import com.videogo.util.LogUtil;

import ezviz.ezopensdk.R;

/**
 * 码流信息展示 Helper
 * 管理取流方式展示、码流速率每秒刷新、更多码流详情（分辨率/编解码）的展示
 *
 * 直接持有 Activity 引用，生命周期与 Activity 一致
 */
public class StreamInfoHelper {

    private static final String TAG = "StreamInfoHelper";

    private TextView streamTypeTv;// 取流方式
    private TextView mRealPlayMoreStreamInfoTv;// 更多码流信息开关
    private LinearLayout mRealPlayMoreStreamInfoLL;// 更多码流信息父视图
    private TextView mRealPlayFlowTv;// 预览每秒接收到的码流大小显示

    private final EZRealPlayActivity mActivity;// Activity 引用
    private long mStreamFlow = 0;// 播放接收到的码流总大小

    public StreamInfoHelper(EZRealPlayActivity activity) {
        this.mActivity = activity;
        initView();
    }

    /**
     * 初始化码流信息相关视图
     */
    private void initView() {
        streamTypeTv = mActivity.findViewById(R.id.tv_stream_type);
        mRealPlayMoreStreamInfoTv = mActivity.findViewById(R.id.tv_stream_info_more);
        mRealPlayMoreStreamInfoLL = mActivity.findViewById(R.id.vg_play_info_more);
        mRealPlayFlowTv = (TextView) mActivity.findViewById(R.id.realplay_flow_tv);
        mRealPlayFlowTv.setText("0k/s");
        mRealPlayMoreStreamInfoTv.setOnClickListener(v -> showStreamInfoMore());
    }

    // region --- 公开方法 ---

    /**
     * 设置取流方式展示
     */
    public void showStreamFetchType() {
        if (mActivity.mEZPlayer == null) {
            return;
        }
        int type = mActivity.mEZPlayer.getStreamFetchType();
        LogUtil.i(TAG, "getStreamFetchType: " + type);
        String streamTypeMsg = mActivity.getApplicationContext().getString(R.string.stream_type) + EZBusinessTool.getStreamType(type);
        streamTypeTv.setText(streamTypeMsg);
        streamTypeTv.setVisibility(View.VISIBLE);
    }

    /**
     * 显示更多码流信息（分辨率、编解码方式）
     */
    public void showStreamInfoMore() {
        mRealPlayMoreStreamInfoTv.setVisibility(View.GONE);
        mRealPlayMoreStreamInfoLL.setVisibility(View.VISIBLE);
        if (mActivity.mVideoQualityHelper.getDeviceDetailPublicInfo() == null) {
            return;
        }
        String ratioMsg = "resolution ratio: " + mActivity.mVideoQualityHelper.getDeviceDetailPublicInfo().videoWidth + "x" + mActivity.mVideoQualityHelper.getDeviceDetailPublicInfo().videoHeight;
        ((TextView) mActivity.findViewById(R.id.tv_resolution_ratio)).setText(ratioMsg);// 分辨率
        String encodeTypeMsg = "videoEncode type: " + EZBusinessTool.getVideoEncodeType(mActivity.mVideoQualityHelper.getDeviceDetailPublicInfo().videoEncodeType);
        ((TextView) mActivity.findViewById(R.id.tv_encode_type)).setText(encodeTypeMsg);// 视频编码格式
        String decodeTypeMsg = "decode type: " + EZBusinessTool.getDecodeType(mActivity.mEZPlayer.getDecodeType());
        ((TextView) mActivity.findViewById(R.id.tv_decode_type)).setText(decodeTypeMsg);// 解码方式
    }

    /**
     * 更新显示预览每秒接收到的码流大小
     */
    public void updateStreamFlow() {
        if (mActivity.mEZPlayer == null) {
            return;
        }
        // 更新流量数据 | Update traffic data
        long streamFlow = mActivity.mEZPlayer.getStreamFlow();
        long streamFlowUnit = streamFlow - mStreamFlow;
        if (streamFlowUnit < 0)
            streamFlowUnit = 0;
        float fKBUnit = (float) streamFlowUnit / (float) Constant.KB;
        String descUnit = String.format("%.2f k/s ", fKBUnit);
        mRealPlayFlowTv.setText(descUnit);
        mStreamFlow = streamFlow;
    }

    /**
     * 播放成功后重置码流信息展示状态
     */
    public void onPlaySucceed() {
        mRealPlayMoreStreamInfoTv.setVisibility(View.VISIBLE);
        mRealPlayMoreStreamInfoLL.setVisibility(View.GONE);
        showStreamFetchType();
    }

    /**
     * 停止播放时重置码流累计值
     */
    public void resetStreamFlow() {
        mStreamFlow = 0;
    }

    /**
     * 更多码流信息区域是否可见
     */
    public boolean isMoreInfoVisible() {
        return mRealPlayMoreStreamInfoLL.getVisibility() == View.VISIBLE;
    }

    // endregion
}
