/**
 * 云台功能 Helper
 * 从 EZRealPlayActivity 拆分出来，负责云台弹窗管理、云台操作、角度比例尺更新等逻辑
 */
package com.videogo.ui.realplay.helper;

import android.content.res.Configuration;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;

import com.videogo.EzvizApplication;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZConstants.EZPTZAction;
import com.videogo.openapi.EZConstants.EZPTZCommand;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.bean.EZDevicePtzAngleInfo;
import com.videogo.ui.realplay.EZRealPlayActivity;
import com.videogo.ui.realplay.popWindow.PTZPopWindow;
import com.videogo.util.LogUtil;
import com.videogo.widget.PtzControlAngleView;

import ezviz.ezopensdk.R;

/**
 * 云台功能 Helper
 * 管理云台弹窗的打开/关闭、云台方向操作、云台角度比例尺显示
 *
 * 直接持有 Activity 引用，生命周期与 Activity 一致
 */
public class PtzHelper {

    private static final String TAG = "PtzHelper";

    private ImageButton mRealPlayPtzBtn;// 云台按钮
    private PtzControlAngleView mPtzControlAngleViewVer;// 云台垂直比例尺
    private PtzControlAngleView mPtzControlAngleViewHor;// 云台水平比例尺

    private final EZRealPlayActivity mActivity;// Activity 引用
    private PTZPopWindow mPtzPopupWindow;// 云台控制弹窗
    private boolean mIsOnPtz = false;// 云台操作中
    private boolean isDevicePtzAngleInited = false;// 设备云台比例尺是否初始化过

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

    /**
     * 初始化云台相关视图
     */
    private void initView() {
        mPtzControlAngleViewVer = mActivity.findViewById(R.id.vertical_angle);
        mPtzControlAngleViewHor = mActivity.findViewById(R.id.horizontal_angle);
        mRealPlayPtzBtn = (ImageButton) mActivity.findViewById(R.id.realplay_ptz_btn);
        mRealPlayPtzBtn.setOnClickListener(v -> openPtzPopupWindow());
    }

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

    /**
     * 打开云台控制弹窗
     */
    public void openPtzPopupWindow() {
        int height = mActivity.mLocalInfo.getScreenHeight() - mActivity.mPortraitTitleBar.getHeight()
                - mActivity.mRealPlayPlayRl.getHeight()
                - (mActivity.mRealPlayRect != null ? mActivity.mRealPlayRect.top : mActivity.mLocalInfo.getNavigationBarHeight());
        mPtzPopupWindow = new PTZPopWindow(mActivity, mActivity.mRealPlayPlayRl, height,
                mPtzControlAngleViewHor, mPtzControlAngleViewVer);
        mPtzPopupWindow.setPTZPopupWindowListener(new PTZPopWindow.OnPTZPopupWindowListener() {
            @Override
            public void onPtzPopWindowOpen(boolean open) {
                mIsOnPtz = open;
            }

            @Override
            public void onPtzOption(EZPTZCommand command, EZPTZAction action) {
                ptzOption(command, action);
            }
        });
        mPtzPopupWindow.openPtzPopupWindow();
    }

    /**
     * 关闭云台控制弹窗
     */
    public void closePtzPopupWindow() {
        if (mPtzPopupWindow != null) {
            mPtzPopupWindow.closePtzPopupWindow();
        }
    }

    /**
     * 云台操作请求
     * @param command 方向
     * @param action 开始or停止
     */
    private void ptzOption(final EZPTZCommand command, final EZPTZAction action) {
        new Thread(() -> {
            boolean ptz_result = false;
            try {
                ptz_result = EzvizApplication.getOpenSDK().controlPTZ(
                        mActivity.mCameraInfo.getDeviceSerial(), mActivity.mCameraInfo.getCameraNo(),
                        command, action, EZConstants.PTZ_SPEED_FAST);
                if (action == EZPTZAction.EZPTZActionSTOP) {
                    mActivity.mHandler.post(() -> hidePtzAngle());
                }
            } catch (BaseException e) {
                e.printStackTrace();
            }
            LogUtil.i(TAG, "controlPTZ ptzCtrl result: " + ptz_result);
        }).start();
    }

    /**
     * 云台角度比例尺更新
     */
    public void handleDevicePtzAngleInfo(Object obj) {
        if (mPtzControlAngleViewHor.getVisibility() == View.VISIBLE
                || mPtzControlAngleViewVer.getVisibility() == View.VISIBLE) {
            EZDevicePtzAngleInfo info = (EZDevicePtzAngleInfo) obj;
            mPtzControlAngleViewHor.setAngle(info.getHorStartAng(), info.getHorEndAng(), info.getHorCurAng());
            mPtzControlAngleViewVer.setAngle(info.getVerStartAng(), info.getVerEndAng(), info.getVerCurAng());
        }
        if (!isDevicePtzAngleInited) {
            isDevicePtzAngleInited = true;
            EZDevicePtzAngleInfo info = (EZDevicePtzAngleInfo) obj;
            mPtzControlAngleViewHor.setAngle(info.getHorStartAng(), info.getHorEndAng(), info.getHorCurAng());
            mPtzControlAngleViewVer.setAngle(info.getVerStartAng(), info.getVerEndAng(), info.getVerCurAng());
        }
    }

    /**
     * 更新云台UI（屏幕方向改变时调用）
     */
    public void updatePtzUI() {
        if (!mIsOnPtz) {
            return;
        }
        if (mActivity.mOrientation == Configuration.ORIENTATION_PORTRAIT) {
            mActivity.mHandler.post(() -> openPtzPopupWindow());
        } else {
            closePtzPopupWindow();
            mIsOnPtz = true;
        }
    }

    // endregion

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

    /**
     * 是否正在云台操作中
     */
    public boolean isOnPtz() {
        return mIsOnPtz;
    }

    /**
     * 隐藏云台角度比例尺
     */
    public void hidePtzAngle() {
        mPtzControlAngleViewVer.setVisibility(View.GONE);
        mPtzControlAngleViewHor.setVisibility(View.GONE);
    }

    /**
     * 设置云台按钮可用状态
     */
    public void setPtzBtnEnabled(boolean enabled) {
        mRealPlayPtzBtn.setEnabled(enabled);
    }

    // endregion
}
