/**
 * Surface 生命周期管理 Helper
 * 从 EZPlayBackListActivity 拆分出来，负责 TextureView 初始化、SurfaceTextureListener 回调
 */
package com.videogo.ui.playback.helper;

import android.graphics.SurfaceTexture;
import android.view.TextureView;
import android.view.View;

import com.videogo.ui.playback.EZPlayBackListActivity;

import ezviz.ezopensdk.R;

/**
 * Surface 生命周期管理 Helper
 * 管理 TextureView 的初始化、SurfaceTextureListener 四个回调、双目设备第二个 TextureView
 *
 * 直接持有 Activity 引用，生命周期与 Activity 一致
 */
public class SurfaceHelper implements TextureView.SurfaceTextureListener {

    private static final String TAG = "SurfaceHelper";

    private TextureView mTextureView;// 主 TextureView
    private TextureView mSecondTextureView;// 双目设备第二个 TextureView

    private final EZPlayBackListActivity mActivity;// Activity 引用

    public SurfaceHelper(EZPlayBackListActivity activity) {
        this.mActivity = activity;
        initView();
    }

    /**
     * 初始化 TextureView 相关视图
     */
    private void initView() {
        mTextureView = mActivity.findViewById(R.id.remote_playback_wnd_sv);
        mTextureView.setSurfaceTextureListener(this);
        // 双目设备
        if (mActivity.isMultiChannelDevice) {
            mSecondTextureView = mActivity.findViewById(R.id.remote_playback_wnd_second_sv);
            mSecondTextureView.setSurfaceTextureListener(this);
            mSecondTextureView.setVisibility(View.VISIBLE);
            mActivity.mOrientationHelper.hideFullscreenButton();
        }
    }

    // region --- SurfaceTextureListener 回调 ---

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        if (mActivity.mPlaybackPlayer != null) {
            if (mTextureView.getSurfaceTexture() == surface) {
                mActivity.mPlaybackPlayer.setSurfaceEx(surface);
            } else if (mSecondTextureView != null && mSecondTextureView.getSurfaceTexture() == surface) {
                mActivity.mPlaybackPlayer.setSurfaceEx(surface, 1);
            }
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        if (mActivity.mPlaybackPlayer != null) {
            if (mTextureView.getSurfaceTexture() == surface) {
                mActivity.mPlaybackPlayer.setSurfaceEx(null);
            } else if (mSecondTextureView != null && mSecondTextureView.getSurfaceTexture() == surface) {
                mActivity.mPlaybackPlayer.setSurfaceEx(null, 1);
            }
        }
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

    // endregion

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

    /**
     * 获取主 TextureView（供 onResume 隐藏键盘等场景使用）
     */
    public TextureView getTextureView() {
        return mTextureView;
    }

    /**
     * 获取主 TextureView 的 SurfaceTexture（供 initEZPlayer 绑定播放器使用）
     */
    public SurfaceTexture getSurfaceTexture() {
        return mTextureView.getSurfaceTexture();
    }

    /**
     * 获取双目设备第二个 TextureView 的 SurfaceTexture（供 initEZPlayer 绑定播放器使用）
     */
    public SurfaceTexture getSecondSurfaceTexture() {
        if (mSecondTextureView != null) {
            return mSecondTextureView.getSurfaceTexture();
        }
        return null;
    }

    // endregion
}
