/**
 * 导航栏初始化与控制 Helper
 * 从 EZRealPlayActivity 拆分出来，负责竖屏/横屏 TitleBar 初始化、控制栏显隐逻辑
 */
package com.videogo.ui.realplay.helper;

import android.content.res.Configuration;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;

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

import ezviz.ezopensdk.R;

/**
 * 导航栏初始化与控制 Helper
 * 管理竖屏/横屏 TitleBar 的初始化、控制栏与 TitleBar 的显隐切换
 *
 * 直接持有 Activity 引用，生命周期与 Activity 一致
 */
public class TitleBarHelper {

    private static final String TAG = "TitleBarHelper";

    public CheckTextButton mFullScreenTitleBarBackBtn;// 全屏返回

    private final EZRealPlayActivity mActivity;// Activity 引用

    public TitleBarHelper(EZRealPlayActivity activity) {
        this.mActivity = activity;
        initTitleBar();
    }

    /**
     * 导航栏初始化
     */
    private void initTitleBar() {
        // 竖屏TitleBar
        mActivity.mPortraitTitleBar = (TitleBar) mActivity.findViewById(R.id.title_bar_portrait);
        mActivity.mPortraitTitleBar.addBackButton(v -> {
            mActivity.mPtzHelper.closePtzPopupWindow();
            mActivity.mVoiceTalkHelper.closeTalkPopupWindow(true);
            if (mActivity.mStatus != RealPlayStatus.STATUS_STOP) {
                mActivity.playerStopRealPlay();
                mActivity.setRealPlayResult(false);
            }
            mActivity.finish();
        });
        // 横屏TitleBar
        mActivity.mLandscapeTitleBar = (TitleBar) mActivity.findViewById(R.id.title_bar_landscape);
        mActivity.mLandscapeTitleBar.setStyle(Color.rgb(0xff, 0xff, 0xff), mActivity.getResources().getDrawable(R.color.dark_bg_70p),
                mActivity.getResources().getDrawable(ezviz.ezopensdkcommon.R.drawable.common_title_back_selector));
        mActivity.mLandscapeTitleBar.setOnTouchListener((v, event) -> false);
        // 横屏返回键添加
        mFullScreenTitleBarBackBtn = new CheckTextButton(mActivity);
        mFullScreenTitleBarBackBtn.setBackground(mActivity.getResources().getDrawable(ezviz.ezopensdkcommon.R.drawable.common_title_back_selector));
        mActivity.mLandscapeTitleBar.addLeftView(mFullScreenTitleBarBackBtn);

        String title = mActivity.mCameraInfo.getCameraName();
        if (mActivity.isMultiChannelDevice) {
            title = TextUtils.isEmpty(mActivity.mDeviceInfo.getDeviceName()) ?
                    String.format("%s(%s)", mActivity.mDeviceInfo.getCategory(), mActivity.mDeviceInfo.getDeviceSerial()) : mActivity.mDeviceInfo.getDeviceName();
        }
        mActivity.mPortraitTitleBar.setTitle(title);
        mActivity.mLandscapeTitleBar.setTitle(title);
    }

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

    /**
     * 显示控制栏和横屏操作栏
     */
    public void showControlRlAndFullOperateBar() {
        if (mActivity.mRtspUrl != null || mActivity.mOrientation == Configuration.ORIENTATION_PORTRAIT) {
            mActivity.mRealPlayControlRl.setVisibility(View.VISIBLE);
            if (mActivity.mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
                if (!mActivity.mVoiceTalkHelper.isOnTalk() && !mActivity.mPtzHelper.isOnPtz()) {
                    mActivity.mLandscapeTitleBar.setVisibility(View.VISIBLE);
                }
            } else {
                mActivity.mLandscapeTitleBar.setVisibility(View.GONE);
            }
        } else {
            if (!mActivity.mVoiceTalkHelper.isOnTalk() && !mActivity.mPtzHelper.isOnPtz()) {
                mActivity.mLandscapeTitleBar.setVisibility(View.VISIBLE);
            }
        }
        mActivity.mControlDisplaySec = 0;
    }

    /**
     * 隐藏横屏时的TitleBar
     * @param excludeLandscapeTitle 是否排除横屏TitleBar（true时横屏下保留显示）
     */
    public void hideLandscapeTitleBar(boolean excludeLandscapeTitle) {
        if (excludeLandscapeTitle && mActivity.mOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            if (!mActivity.mVoiceTalkHelper.isOnTalk() && !mActivity.mPtzHelper.isOnPtz()) {
                mActivity.mLandscapeTitleBar.setVisibility(View.VISIBLE);
            }
        } else {
            mActivity.mLandscapeTitleBar.setVisibility(View.GONE);
        }
    }

    // endregion
}
