/**
 * 屏幕旋转/全屏 Helper
 * 从 EZPlayBackListActivity 拆分出来，负责屏幕方向控制、全屏切换、播放视图布局等逻辑
 */
package com.videogo.ui.playback.helper;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.videogo.constant.Constant;
import com.videogo.ui.common.ScreenOrientationHelper;
import com.videogo.ui.playback.EZPlayBackListActivity;
import com.videogo.ui.playback.RemoteListContant;
import com.videogo.util.Utils;
import com.videogo.widget.CheckTextButton;

import ezviz.ezopensdk.R;

/**
 * 屏幕旋转/全屏 Helper
 * 管理屏幕方向切换、全屏/退出全屏、播放视图布局计算
 * <p>
 * 直接持有 Activity 引用，生命周期与 Activity 一致
 */
public class OrientationHelper {

    private static final String TAG = "OrientationHelper";

    private CheckTextButton mFullscreenButton;// 全屏按钮
    private ScreenOrientationHelper mScreenOrientationHelper;// 屏幕方向控制器

    private final EZPlayBackListActivity mActivity;// Activity 引用
    public int mOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;// 屏幕方向
    public float mRealRatio = Constant.LIVE_VIEW_RATIO;// 播放分辨率

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

    /**
     * 初始化屏幕旋转相关视图
     */
    private void initView() {
        mFullscreenButton = (CheckTextButton) mActivity.findViewById(R.id.fullscreen_button);
        mScreenOrientationHelper = new ScreenOrientationHelper(mActivity, mFullscreenButton);
    }

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

    /**
     * 获取全屏按钮（供鱼眼设备等外部使用）
     */
    public CheckTextButton getFullscreenButton() {
        return mFullscreenButton;
    }

    /**
     * 双目设备隐藏全屏按钮
     */
    public void hideFullscreenButton() {
        mFullscreenButton.setVisibility(View.INVISIBLE);
    }

    /**
     * 设置双目设备画面比例
     */
    public void setMultiChannelRatio() {
        mRealRatio = Constant.LIVE_MULTI_VIEW_RATIO;
    }

    /**
     * 播放开始时更新画面比例
     */
    public void updateRealRatio(int width, int height) {
        if (width != 0) {
            mRealRatio = (float) height / width;
        }
    }

    /**
     * 开启传感器方向跟随
     */
    public void enableSensorOrientation() {
        mScreenOrientationHelper.enableSensorOrientation();
    }

    /**
     * 关闭传感器方向跟随
     */
    public void disableSensorOrientation() {
        mScreenOrientationHelper.disableSensorOrientation();
    }

    /**
     * 切换为竖屏
     */
    public void portrait() {
        mScreenOrientationHelper.portrait();
    }

    /**
     * onStart 时通知 ScreenOrientationHelper
     */
    public void postOnStart() {
        mScreenOrientationHelper.postOnStart();
    }

    /**
     * onStop 时通知 ScreenOrientationHelper
     */
    public void postOnStop() {
        mScreenOrientationHelper.postOnStop();
    }

    /**
     * 屏幕方向改变后的处理
     * 由 Activity 的 onConfigurationChanged 调用
     */
    public void onOrientationChanged() {
        setRemoteListSvLayout();
        if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
            // 显示状态栏
            fullScreen(false);
            if (mActivity.mStatus != RemoteListContant.STATUS_PLAYING) {
                // 不允许选择屏幕
                mScreenOrientationHelper.disableSensorOrientation();
            }
            // 竖屏处理
            mActivity.remoteListPage.setBackgroundColor(mActivity.getResources().getColor(R.color.white));
            mActivity.mPortraitTitleBar.setVisibility(View.VISIBLE);
            mActivity.mRecordQueryHelper.setCloudRecyclerVisibility(View.VISIBLE);
            mActivity.mControlBarRL.setVisibility(View.VISIBLE);
            mActivity.mLandscapeTitleBar.setVisibility(View.GONE);
        } else {
            // 横屏处理
            // 隐藏状态栏
            fullScreen(true);
            mActivity.remoteListPage.setBackgroundColor(mActivity.getResources().getColor(R.color.black_bg));
            mActivity.mPortraitTitleBar.setVisibility(View.GONE);
            mActivity.mRecordQueryHelper.setCloudRecyclerVisibility(View.GONE);
            mActivity.mControlBarRL.setVisibility(View.GONE);
            mActivity.mLandscapeTitleBar.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置播放器视图布局（根据屏幕方向计算宽高）
     */
    public void setRemoteListSvLayout() {
        final int screenWidth = mActivity.localInfo.getScreenWidth();
        final int screenHeight = (mOrientation == Configuration.ORIENTATION_PORTRAIT) ?
                (mActivity.localInfo.getScreenHeight() - mActivity.localInfo.getNavigationBarHeight()) : mActivity.localInfo.getScreenHeight();
        final RelativeLayout.LayoutParams realPlaySvlp = Utils.getPlayViewLp(mRealRatio, mOrientation,
                mActivity.localInfo.getScreenWidth(), (int) (mActivity.localInfo.getScreenWidth() * Constant.LIVE_VIEW_RATIO),
                screenWidth, screenHeight);

        RelativeLayout.LayoutParams svLp = new RelativeLayout.LayoutParams(realPlaySvlp.width, realPlaySvlp.height);
        svLp.addRule(RelativeLayout.CENTER_HORIZONTAL);

        ViewGroup playWindowVg = (ViewGroup) mActivity.findViewById(R.id.vg_play_window);
        playWindowVg.setLayoutParams(svLp);
    }

    /**
     * 返回键处理横屏转竖屏
     *
     * @return true-已处理（当前横屏，转为竖屏）  false-未处理（当前竖屏）
     */
    public boolean handleBackPressed() {
        if (mActivity.getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT) {
            mScreenOrientationHelper.portrait();
            return true;
        }
        return false;
    }

    /**
     * 全屏切换
     *
     * @param enable true-进入全屏  false-退出全屏
     */
    public void fullScreen(boolean enable) {
        if (enable) {
            WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            mActivity.getWindow().setAttributes(lp);
            mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            WindowManager.LayoutParams attr = mActivity.getWindow().getAttributes();
            attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mActivity.getWindow().setAttributes(attr);
            mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

    // endregion
}
