/**
 * Surface 生命周期管理 Helper
 * 从 EZRealPlayActivity 拆分出来，负责 SurfaceView 初始化、SurfaceHolder.Callback 回调、封面管理
 */
package com.videogo.ui.realplay.helper;

import android.graphics.Bitmap;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;

import com.videogo.realplay.RealPlayStatus;
import com.videogo.ui.realplay.EZRealPlayActivity;
import com.videogo.widget.CheckTextButton;
import com.videogo.widget.CustomTouchListener;

import ezviz.ezopensdk.R;

/**
 * Surface 生命周期管理 Helper
 * 管理 SurfaceView 的初始化、SurfaceHolder.Callback 三个回调、封面的显示/隐藏
 *
 * 直接持有 Activity 引用，生命周期与 Activity 一致
 */
public class SurfaceHelper implements SurfaceHolder.Callback {

    private static final String TAG = "SurfaceHelper";

    public SurfaceView mRealPlaySv;// 主 SurfaceView
    public CustomTouchListener mRealPlayTouchListener;// 播放器手势监听器
    public CustomTouchListener mRealPlayTouchListener2;// 双目设备第二个播放器手势监听器
    private SurfaceHolder mRealPlaySh;// 主 SurfaceHolder
    private SurfaceHolder mRealPlaySecondSh;// 双目设备第二个 SurfaceHolder
    private SurfaceView mRealPlaySecondSv;// 双目设备第二个 SurfaceView
    private ImageView mCoverImage;// 封面
    private ImageView mCoverImage2;// 双目设备第二个封面

    private final EZRealPlayActivity mActivity;// Activity 引用

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

    /**
     * 初始化 Surface 相关视图
     */
    private void initView() {
        mRealPlaySv = (SurfaceView) mActivity.findViewById(R.id.realplay_sv);
        mRealPlaySh = mRealPlaySv.getHolder();
        mRealPlaySh.addCallback(this);
        // 双目设备
        if (mActivity.isMultiChannelDevice) {
            mRealPlaySecondSv = (SurfaceView) mActivity.findViewById(R.id.realplay_second_sv);
            mRealPlaySecondSh = mRealPlaySecondSv.getHolder();
            mRealPlaySecondSh.addCallback(this);
            mRealPlaySecondSv.setVisibility(View.VISIBLE);
            CheckTextButton fullscreenButton = mActivity.findViewById(R.id.fullscreen_button);
            if (fullscreenButton != null) {
                fullscreenButton.setVisibility(View.INVISIBLE);
            }
        }

        mCoverImage = mActivity.findViewById(R.id.coverImage);
        if (mActivity.isMultiChannelDevice) {
            mCoverImage2 = mActivity.findViewById(R.id.coverImage2);
        }
    }

    // region --- SurfaceHolder.Callback ---

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (mRealPlaySv.getHolder() == holder) {
            if (mActivity.mEZPlayer != null) {
                mActivity.mEZPlayer.setSurfaceHold(holder);
            }
            mRealPlaySh = holder;
        } else if (mRealPlaySecondSv != null && mRealPlaySecondSv.getHolder() == holder) {
            if (mActivity.mEZPlayer != null) {
                mActivity.mEZPlayer.setSurfaceHold(holder, 1);
            }
            mRealPlaySecondSh = holder;
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (mRealPlaySv.getHolder() == holder) {
            if (mActivity.mEZPlayer != null) {
                mActivity.mEZPlayer.setSurfaceHold(holder);
            }
            mRealPlaySh = holder;
        } else if (mRealPlaySecondSv != null && mRealPlaySecondSv.getHolder() == holder) {
            if (mActivity.mEZPlayer != null) {
                mActivity.mEZPlayer.setSurfaceHold(holder, 1);
            }
            mRealPlaySecondSh = holder;
        }

        if (mActivity.mStatus == RealPlayStatus.STATUS_INIT) {
            // 开始播放
            mActivity.playerStartRealPlay();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mRealPlaySv.getHolder() == holder) {
            if (mActivity.mEZPlayer != null) {
                mActivity.mEZPlayer.setSurfaceHold(null);
            }
            mRealPlaySh = null;
        } else if (mRealPlaySecondSv != null && mRealPlaySecondSv.getHolder() == holder) {
            if (mActivity.mEZPlayer != null) {
                mActivity.mEZPlayer.setSurfaceHold(null, 1);
            }
            mRealPlaySecondSh = null;
        }
    }

    // endregion

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

    /**
     * 绑定手势监听器到 SurfaceView（必须在 GestureZoomHelper 初始化后调用）
     */
    public void bindTouchListeners() {
        mRealPlaySv.setOnTouchListener(mRealPlayTouchListener);
        if (mActivity.isMultiChannelDevice && mRealPlaySecondSv != null) {
            mRealPlaySecondSv.setOnTouchListener(mRealPlayTouchListener2);
        }
    }

    /**
     * 获取主 SurfaceHolder（用于 initEZPlayer）
     */
    public SurfaceHolder getSurfaceHolder() {
        return mRealPlaySh;
    }

    /**
     * 获取双目设备第二个 SurfaceHolder（用于 initEZPlayer）
     */
    public SurfaceHolder getSecondSurfaceHolder() {
        return mRealPlaySecondSh;
    }

    /**
     * 暂停前截取封面
     */
    public void captureCoverImage() {
        if (mActivity.mEZPlayer == null) {
            return;
        }
        Bitmap bitmap = (mActivity.mWatermarkHelper.isWaterMarkShow() && !mActivity.isMultiChannelDevice) ?
                mActivity.mEZPlayer.captureRenderPicture(mRealPlaySv.getWidth(), mRealPlaySv.getHeight()) :
                mActivity.mEZPlayer.capturePicture();
        mCoverImage.setImageBitmap(bitmap);
        if (mActivity.isMultiChannelDevice) {
            Bitmap bitmap2 = mActivity.mEZPlayer.capturePicture(1);
            mCoverImage2.setImageBitmap(bitmap2);
        }
    }

    /**
     * 设置封面可见性（播放成功隐藏，停止/失败显示）
     * @param isPlaySuccess 是否播放成功
     */
    public void setCoverVisibility(boolean isPlaySuccess) {
        mCoverImage.setVisibility(isPlaySuccess ? View.GONE : View.VISIBLE);
        if (mActivity.isMultiChannelDevice) {
            mCoverImage2.setVisibility(isPlaySuccess ? View.GONE : View.VISIBLE);
        }
    }

    // endregion
}
