/**
 * 水印功能 Helper
 * 从 EZRealPlayActivity 拆分出来，负责水印配置、水印弹窗管理、水印状态维护
 */
package com.videogo.ui.realplay.helper;

import android.widget.ImageButton;

import com.videogo.openapi.EZPlayer;
import com.videogo.stream.EZPlayWaterMarkConfig;
import com.videogo.ui.realplay.EZRealPlayActivity;
import com.videogo.ui.realplay.popWindow.WatermarkPopWindow;

import ezviz.ezopensdk.R;

/**
 * 水印功能 Helper
 * 管理水印的配置初始化、弹窗打开/关闭、水印显示/清除、按钮状态
 * <p>
 * 直接持有 Activity 引用，生命周期与 Activity 一致，无内存泄漏风险
 */
public class WatermarkHelper {

    private static final String TAG = "WatermarkHelper";

    private ImageButton mRealPlayWatermarkBtn;// 水印按钮

    private final EZRealPlayActivity mActivity;// Activity 引用
    private boolean mIsWaterMarkShow = false;// 是否显示水印
    private EZPlayWaterMarkConfig mWaterMarkConfig;// 水印配置

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

    /**
     * 初始化水印按钮
     */
    private void initView() {
        mRealPlayWatermarkBtn = (ImageButton) mActivity.findViewById(R.id.realplay_watermark_btn);
        mRealPlayWatermarkBtn.setOnClickListener(v -> openWatermarkPopupWindow());
    }

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

    /**
     * 打开水印设置弹窗
     */
    public void openWatermarkPopupWindow() {
        if (mWaterMarkConfig == null) {
            mWaterMarkConfig = new EZPlayWaterMarkConfig();
            mWaterMarkConfig.fontArray = new String[]{"水印信息：", "杭州萤石网络有限公司", "股票代码：688475"};
            mWaterMarkConfig.fontAlignment =
                    EZPlayWaterMarkConfig.EZWaterMarkTextAlignment.EZWaterMarkTextAlignmentCenter;
            mWaterMarkConfig.fontRotateAngle = 45f;
            mWaterMarkConfig.fontColorAdapt = true;
            mWaterMarkConfig.windowAdaptMode =
                    EZPlayWaterMarkConfig.EZWaterMarkWindowAdaptMode.EZWaterMarkWindowAdaptModeFontRowColumn;
        }
        int height =
                mActivity.mLocalInfo.getScreenHeight() - mActivity.mPortraitTitleBar.getHeight() - mActivity.mRealPlayPlayRl.getHeight() - (mActivity.mRealPlayRect != null ? mActivity.mRealPlayRect.top : mActivity.mLocalInfo.getNavigationBarHeight());
        WatermarkPopWindow watermarkPopWindow = new WatermarkPopWindow(mActivity, mActivity.mRealPlayPlayRl, height,
                mActivity.mSurfaceHelper.mRealPlaySv, mWaterMarkConfig);
        watermarkPopWindow.setWatermarkPopupWindowListener(new WatermarkPopWindow.OnWatermarkPopupWindowListener() {
            @Override
            public void onWaterMarkInfoChanged() {
                mIsWaterMarkShow = true;
                mActivity.mEZPlayer.setWaterMarkFont(mWaterMarkConfig);
            }

            @Override
            public void onClearWaterMarkInfo() {
                mIsWaterMarkShow = false;
                mActivity.mEZPlayer.clearWaterMarkFont();
            }
        });
        watermarkPopWindow.openWatermarkPopupWindow();
    }

    /**
     * 播放成功后恢复水印（如果之前已设置水印）
     */
    public void restoreWatermarkIfNeeded(EZPlayer player) {
        if (mIsWaterMarkShow && mWaterMarkConfig != null) {
            player.setWaterMarkFont(mWaterMarkConfig);
        }
    }

    /**
     * 设置水印按钮可用状态
     */
    public void setWatermarkBtnEnabled(boolean enabled) {
        mRealPlayWatermarkBtn.setEnabled(enabled);
    }

    // endregion

    // region --- 状态查询 ---

    /**
     * 是否正在显示水印
     */
    public boolean isWaterMarkShow() {
        return mIsWaterMarkShow;
    }

    /**
     * 获取水印配置
     */
    public EZPlayWaterMarkConfig getWaterMarkConfig() {
        return mWaterMarkConfig;
    }

    // endregion
}
