package com.videogo.ui.realplay.popWindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.videogo.openapi.EZConstants;
import com.videogo.util.LogUtil;
import com.videogo.widget.PtzControlAngleView;

import ezviz.ezopensdk.R;

/**
 * Copyright (C) 2025 HIKVISION Inc.
 * Comments:
 *
 * @author ChengJun9
 * @date 2025/9/19 14:53
 */
public class PTZPopWindow {

    private static final String TAG = PTZPopWindow.class.getSimpleName();

    // 上下文
    private Context context;
    // pop窗口父控件
    private View parent;
    // pop窗口高度
    private int windowHeight;

    // 云台水平比例尺
    private PtzControlAngleView ptzControlAngleViewHor;
    // 云台垂直比例尺
    private PtzControlAngleView ptzControlAngleViewVer;

    // 云台pop
    private PopupWindow mPtzPopupWindow;

    private OnPTZPopupWindowListener ptzPopupWindowListener;

    /**
     * @param context         上下文
     * @param parent          pop窗口父控件
     * @param windowHeight    pop窗口高度
     * @param ptzControlAngleViewHor 云台水平比例尺
     * @param ptzControlAngleViewVer 云台垂直比例尺
     */
    public PTZPopWindow(Context context, View parent, int windowHeight,
                        PtzControlAngleView ptzControlAngleViewHor, PtzControlAngleView ptzControlAngleViewVer) {
        this.context = context;
        this.parent = parent;
        this.windowHeight = windowHeight;
        this.ptzControlAngleViewHor = ptzControlAngleViewHor;
        this.ptzControlAngleViewVer = ptzControlAngleViewVer;
    }

    public void setPTZPopupWindowListener(OnPTZPopupWindowListener ptzPopupWindowListener) {
        this.ptzPopupWindowListener = ptzPopupWindowListener;
    }

    private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionevent) {
            int action = motionevent.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:// 手指按下
                    view.setPressed(true);
                    int id = view.getId();
                    if (id == R.id.ptz_top_btn) {// 云台-上
                        ptzControlAngleViewVer.setVisibility(View.VISIBLE);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandUp, EZConstants.EZPTZAction.EZPTZActionSTART);
                    } else if (id == R.id.ptz_bottom_btn) {// 云台-下
                        ptzControlAngleViewVer.setVisibility(View.VISIBLE);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandDown, EZConstants.EZPTZAction.EZPTZActionSTART);
                    } else if (id == R.id.ptz_left_btn) {// 云台-左
                        ptzControlAngleViewHor.setVisibility(View.VISIBLE);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandLeft, EZConstants.EZPTZAction.EZPTZActionSTART);
                    } else if (id == R.id.ptz_right_btn) {// 云台-右
                        ptzControlAngleViewHor.setVisibility(View.VISIBLE);
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandRight, EZConstants.EZPTZAction.EZPTZActionSTART);
                    }
                    break;
                case MotionEvent.ACTION_UP:// 手指抬起
                    view.setPressed(false);
                    id = view.getId();
                    if (id == R.id.ptz_top_btn) {
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandUp, EZConstants.EZPTZAction.EZPTZActionSTOP);
                    } else if (id == R.id.ptz_bottom_btn) {
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandDown, EZConstants.EZPTZAction.EZPTZActionSTOP);
                    } else if (id == R.id.ptz_left_btn) {
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandLeft, EZConstants.EZPTZAction.EZPTZActionSTOP);
                    } else if (id == R.id.ptz_right_btn) {
                        ptzOption(EZConstants.EZPTZCommand.EZPTZCommandRight, EZConstants.EZPTZAction.EZPTZActionSTOP);
                    }
                    break;
                case MotionEvent.ACTION_CANCEL:// 手指取消
                    view.setPressed(false);
                    break;
                default:
                    break;
            }
            return true;
        }
    };

    /**
     * 打开云台操作弹出框
     */
    public void openPtzPopupWindow() {
        closePtzPopupWindow();
        if (ptzPopupWindowListener != null) {
            ptzPopupWindowListener.onPtzPopWindowOpen(true);
        }

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewGroup layoutView = (ViewGroup) layoutInflater.inflate(R.layout.realplay_ptz_wnd, null, true);

        layoutView.findViewById(R.id.ptz_close_btn).setOnClickListener(v -> closePtzPopupWindow());
        layoutView.findViewById(R.id.ptz_top_btn).setOnTouchListener(mOnTouchListener);
        layoutView.findViewById(R.id.ptz_bottom_btn).setOnTouchListener(mOnTouchListener);
        layoutView.findViewById(R.id.ptz_left_btn).setOnTouchListener(mOnTouchListener);
        layoutView.findViewById(R.id.ptz_right_btn).setOnTouchListener(mOnTouchListener);

        mPtzPopupWindow = new PopupWindow(layoutView, RelativeLayout.LayoutParams.MATCH_PARENT, windowHeight, true);
        mPtzPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPtzPopupWindow.setAnimationStyle(R.style.popwindowUpAnim);
        mPtzPopupWindow.setFocusable(false);
        mPtzPopupWindow.setOutsideTouchable(false);
        mPtzPopupWindow.setTouchable(true);
        mPtzPopupWindow.showAsDropDown(parent);
        mPtzPopupWindow.setOnDismissListener(() -> {
            LogUtil.i(TAG, "KEYCODE_BACK DOWN");
            closePtzPopupWindow();
        });
        mPtzPopupWindow.update();
    }

    /**
     * 关闭云台操作弹出框
     */
    public void closePtzPopupWindow() {
        if (ptzPopupWindowListener != null) {
            ptzPopupWindowListener.onPtzPopWindowOpen(false);
        }
        if (mPtzPopupWindow != null) {
            dismissPopWindow(mPtzPopupWindow);
            mPtzPopupWindow = null;
        }
    }

    private void dismissPopWindow(PopupWindow popupWindow) {
        if (popupWindow != null) {
            try {
                popupWindow.dismiss();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

    private void ptzOption(EZConstants.EZPTZCommand command, EZConstants.EZPTZAction action) {
        if (ptzPopupWindowListener != null) {
            ptzPopupWindowListener.onPtzOption(command, action);
        }
    }

    /**
     * 云台操作弹出框监听器
     */
    public interface OnPTZPopupWindowListener {

        /**
         * 云台操作弹出框是否打开
         *
         * @param open
         */
        void onPtzPopWindowOpen(boolean open);

        /**
         * 云台操作
         *
         * @param command 云台控制命令
         * @param action  云台控制动作
         */
        void onPtzOption(EZConstants.EZPTZCommand command, EZConstants.EZPTZAction action);
    }
}
